繁体   English   中英

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

[英]Ajax - Getting specific value from data and put to text-box?

我有一个工作的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
";

您的php为js生成了什么?

如果您不通过ajax调用来检索json,则您将尝试访问“ data.id”。

如果您发送以下内容:{'id':'your id'}

要么

[ {'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

$('#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'); 如果“测试”一词出现在您的文本框中,则问题出在data.id 在成功处理程序的开始处放置一个断点,并查看实际的datadata.id

而且看起来您在PHP方面可能有一个SQL注入漏洞 您应该阅读此内容。 事实证明,很可能有人只需输入';delete from students;--作为其ID,就可以删除您的所有学生记录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM