根据前一个输入文本填充第二个输入文本


Populate second input text based on previous one

我正在构建一个表单,我想在一些文本输入中插入一个数字,并在下一个输入文本中,自动填充与数据库中数字匹配的名称。我已经测试了许多"解决方案",我在这里和其他网站上看到,但没有一个有效。

我的脚本代码是:
$("#numero").blur(function () {
    $.post(convoc.php, { numero: $(this).val() }, function (data) {
        $("#nome").val(data);
    });
});

我的convoc.php代码:

if (@$_POST['numero'] != "")
        $numero = $_POST['numero'];
$query  = "SELECT Nome from ******* where Numero = $numero";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    echo json_encode($row['Nome']);

(这工作得很好,因为我已经测试了echo,它正在打印正确的名称)

第一个输入文本的id是:"numero",第二个输入文本的id是" name "。

是否有什么我做错了在脚本功能?

HTML代码为:

<body onload="load()" style="padding-top: 20px;">
    <form action="convoc.php" method="post">
        <div id="myform">
        <table align="center">
<tr>
            <td><input type="text" id="numero" name="numero"></td>
            <td><input type="text" id="nome" name="nome"></td>
        <td><input type="button" id="add" value="Convocar" onclick="Javascript:addRow()"></td>
    </tr>
</table>
</div>
</form>
</body>

js代码是:

function addRow() {
    var numero = document.getElementById("numero");
    var nome = document.getElementById("nome");
    var table = document.getElementById("myTableData");

    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    row.insertCell(0).innerHTML= '<input type="button" value = "Delete" onClick="Javacsript:deleteRow(this)">';
    row.insertCell(1).innerHTML= numero.value;
    row.insertCell(2).innerHTML= nome.value;
}
function deleteRow(obj) {
    var index = obj.parentNode.parentNode.rowIndex;
    var table = document.getElementById("myTableData");
    table.deleteRow(index);
}
function load() {
    console.log("Page load finished");
}

(不,我在浏览器中没有收到任何错误)

修改脚本代码如下:

第一个参数convoc.php应该是'convoc.php'

$(function(){
    $("#numero").blur(function () {
        //use $.ajax
    $.ajax({
        url:'convoc.php',
        dataType:'json', //The datatype should be json because you are returning in json format
        data : {numero: $(this).val()},
        success:function(data){
        $("#nome").val(data.result);
        }
    });
    });
});

同时修改php代码如下:

echo json_encode(array("result"=>$row['Nome']));

添加Json结束:

$("#numero").blur(function () {
   $.post(convoc.php, { numero: $(this).val() }, function (data) {
     $("#nome").val(data);
  });
},"json");  // This Line