Ajax - 从数据中获取特定值并放入文本框?

时间:2016-05-25 11:44:37

标签: javascript jquery ajax

我有一个工作的Ajax,可以检索最多10个列数据。 我想把第一列数据放到教科书上但没有成功。

我尝试过以下行,但没有将数据显示到文本框中:

  

$( '#stud_id')VAL(data.id);

任何帮助?

Ajax:

  <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script>
    <script src="../jQueryAssets/jquery-1.11.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#button").click(function(){
        var student_id=$("#student_id").val();
        $.ajax({
        type:"post",
        url:"paymentProcess.php",
        data:"student_id="+student_id,
        beforeSend: function(){
        $("#info").html("<h1>Pleas Wait working...</h1>");
},
success:function(data){
    $('#stud_id').val(data.id);
    $("#frmPayment").css("visibility","visible");
$("#info").html(data);

}});
});
});
</script>

文本框:

 <input type="text" name="student_id" id="student_id" placeholder="Enter Student ID"/>

工作查询:

<?php
  $expected;
$stud_id=$_POST['student_id'];
$stud_payment="select st.id, st.stud_fname, st.stud_lname, st.stud_middle_name,org.Instname as Institute, dp.dep_name, st.entry_year,sum(sc.total_fee) as 'Expected Fee'
from student st inner join department dp on st.dep_id=dp.id
left join st_costshare sc on sc.stud_id=st.id
inner join college cl on cl.id=dp.college_id
inner join organization org on org.id=cl.inst_id
where st.id='{$stud_id}'
group by sc.stud_id limit 1
";

2 个答案:

答案 0 :(得分:0)

你为js创建的php是什么?

您正试图访问&#34; data.id&#34;如果您不通过ajax调用检索json,那将是问题。

如果您发送以下内容: {&#39; id&#39;:&#39;您的ID&#39;}

[ {'id':'your id'} , {'id':'your second id'} ]

并将你的js改为:

$.ajax({
        type:"post",
        dataType:"json"
});

你将拥有一个对象,而不是一个字符串。

请完全按评论中的人推荐

//in your success callback  
console.log(data)

如果您不知道如何检索这些日志数据,请使用以下命令:

https://developer.chrome.com/devtools

答案 1 :(得分:0)

$('#stud_id').val(data.id);行说:找到一个id="stud_id"的输入元素,并将其值设置为data.id

是否有id="stud_id"的元素,是<input.../>还是其他?如果它是(例如)<div.../>,那么您需要.text()而不是.val()

$('#stud_id').val(data.id);替换为$('#stud_id').val('Test');。如果单词Test显示在文本框中,则问题出在data.id。在成功处理程序的开头放置一个断点,看看datadata.id实际上是什么。

看起来你可能在PHP方面有SQL injection vulnerability。你应该读一下这个。事实上,只要输入';delete from students;--作为他们的身份证,就有可能删除所有学生记录。