javascript - Populate second input text based on previous one -
i'm building form insert in text input, number, , in next input text, auto populate name matches number in db. i've tested many "solutions" saw here , on other sites none worked.
my script code is:
$("#numero").blur(function () { $.post(convoc.php, { numero: $(this).val() }, function (data) { $("#nome").val(data); }); });
my convoc.php code:
if (@$_post['numero'] != "") $numero = $_post['numero']; $query = "select nome ******* numero = $numero"; $result = mysql_query($query); $row = mysql_fetch_array($result); echo json_encode($row['nome']);
(this working well, because i've tested echo , it's printing correct name)
the id of first input text is: "numero" , id of second input "nome".
is there i'm doing wrong in script function?
the html code is:
<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>
the js code is:
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"); }
(and no, i'm not receiving error in browser)
change script code below.
the first parameter
convoc.php
should'convoc.php'
.
$(function(){ $("#numero").blur(function () { //use $.ajax $.ajax({ url:'convoc.php', datatype:'json', //the datatype should json because returning in json format data : {numero: $(this).val()}, success:function(data){ $("#nome").val(data.result); } }); }); });
also change php code below.
echo json_encode(array("result"=>$row['nome']));
Comments
Post a Comment