dongyue9864 2016-08-23 11:09
浏览 91
已采纳

根据下拉列表中的选定值从数据库填写表单值

I've a form like this and if the user selected any value from the dropdown then I want to fetch the details from the database based on that value. Suppose say if the user selected Name 2 from the dropdown then I should automatically fill the fields address & phone that are relevant to Name 2. Also I'm unable to get the value $_POST['name']. I've tried but no luck. Here is my code. Please suggest me if is there any other way to achieve it. Thank you.

HTML

<select id="name" name="name">
    <option value="1">Name 1</option>
    <option value="2">Name 2</option>
    <option value="3">Name 3</option>
    <option value="4">Name 4</option>
    <option value="5">Name 5</option>
</select>

<input type="text" name="address" id="address" />
<input type="text" name="phone" id="phone" />

jQuery

$(document).ready(function () {
    $('#name').change(function(){
        var name = $(this).val();
        var dataString = "name=" + name;
        alert (dataString);
        $.ajax ({
            type: "POST",
            url: "test.php",
            data: dataString,
            dataType: json,
            success: function(data) {
                $('#address').val(data.address);
                $('#phone').val(data.phone);
            }
        });
    });
});

PHP

<?php

ini_set('display_errors',1); // enable php error display for easy trouble shooting
error_reporting(E_ALL); // set error display to all

include "connect.php";

if (ISSET($_POST['name'])) {

    $ref = $_POST['name'];

    $query = $con->query("SELECT * FROM test WHERE id = '$ref' LIMIT 1");
    $row = $query->fetch_assoc();

    $address = $row['address'];
    $phone = $row['phone'];

    $json = array('address' => $address, 'phone' => $phone);
    echo json_encode($json);
}

$con->close();
?>
  • 写回答

4条回答 默认 最新

  • duanjiongzhen2523 2016-08-23 15:39
    关注

    HTML

    <form>
    <select id="name" name="name">
        <option value="1">Name 1</option>
        <option value="2">Name 2</option>
        <option value="3">Name 3</option>
        <option value="4">Name 4</option>
        <option value="5">Name 5</option>
    </select>
    </form>
    
    <input type="text" name="address" id="address" />
    <input type="text" name="phone" id="phone" />
    

    jQuery

    $('#account_head').change(function(){
        var name = $(this).val();
        var dataString = "name=" + name;
        $.ajax ({
            type: "POST",
            url: "get_results.php",
            data: dataString,
            dataType: 'json',
            success: function(data) {
                // console.debug(data);
                $('#agent').val(data.agent);
                $('#tin').val(data.tin);
                $('#address').val(data.address);
            }
        });
    });
    

    PHP

    <?php
    if (ISSET($_POST)) {
        $ref = $_POST['name'];
    
        $query = $con->query("SELECT * FROM test WHERE id = '$ref' LIMIT 1");
        $row = $query->fetch_assoc();
    
        $address = $row['address'];
        $phone = $row['phone'];
        $json = array('address' => $address, 'phone' => $phone);
        echo json_encode($json);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?