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

时间:2023-01-28 11:19:53

I have a working Ajax which retrieves up-to 10 column data. I want to put the first column data to the textbook but not succeded.

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

I have tried the following line but not displaying the data to text box:

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

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

Any help?

The 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>

The Text Box:

文本框:

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

The working Query:

工作查询:

<?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 个解决方案

#1


0  

What is your php generating for js?

你为js生成的php是什么?

You're trying to access "data.id" which will be the problem, if you don't retrieve json through your ajax call.

你试图访问“data.id”这将是问题,如果你没有通过你的ajax调用检索json。

If you send something like this: {'id':'your id'}

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

or

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

and change your js to:

并将你的js改为:

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

You will have an object and not a string.

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

Please do exactly what the guys in the comments were recommending

请完全按评论中的人推荐

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

If you don't know how to retrieve these log data, use this:

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

https://developer.chrome.com/devtools

#2


0  

The line $('#stud_id').val(data.id); is saying: find an input element with id="stud_id" and set its value to data.id.

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

Is there an element with id="stud_id", and is it an <input.../> or something else? If it is (for example) a <div.../> then you would need .text() rather than .val().

是否有一个id =“stud_id”的元素,它是还是别的?如果是(例如)

那么你需要.text()而不是.val()。

Replace $('#stud_id').val(data.id); with $('#stud_id').val('Test');. If the word Test appears in your text box then the issue is with data.id. Put a breakpoint at the start of the success handler and see what data and data.id actually are.

替换$('#stud_id')。val(data.id); with $('#stud_id')。val('Test');.如果单词Test出现在文本框中,则问题出在data.id上。在成功处理程序的开头放置一个断点,看看究竟是什么数据和data.id。

Also it looks like you may have a SQL injection vulnerability on the PHP side of things. You should read up on this. As things stand there's a good chance someone can delete all your student records just by entering ';delete from students;-- as their ID.

此外,您可能在PHP方面有一个SQL注入漏洞。你应该读一下这个。事实上,只要输入';从学生中删除; - 作为他们的ID,就有可能删除所有学生记录。

#1


0  

What is your php generating for js?

你为js生成的php是什么?

You're trying to access "data.id" which will be the problem, if you don't retrieve json through your ajax call.

你试图访问“data.id”这将是问题,如果你没有通过你的ajax调用检索json。

If you send something like this: {'id':'your id'}

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

or

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

and change your js to:

并将你的js改为:

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

You will have an object and not a string.

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

Please do exactly what the guys in the comments were recommending

请完全按评论中的人推荐

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

If you don't know how to retrieve these log data, use this:

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

https://developer.chrome.com/devtools

#2


0  

The line $('#stud_id').val(data.id); is saying: find an input element with id="stud_id" and set its value to data.id.

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

Is there an element with id="stud_id", and is it an <input.../> or something else? If it is (for example) a <div.../> then you would need .text() rather than .val().

是否有一个id =“stud_id”的元素,它是还是别的?如果是(例如)

那么你需要.text()而不是.val()。

Replace $('#stud_id').val(data.id); with $('#stud_id').val('Test');. If the word Test appears in your text box then the issue is with data.id. Put a breakpoint at the start of the success handler and see what data and data.id actually are.

替换$('#stud_id')。val(data.id); with $('#stud_id')。val('Test');.如果单词Test出现在文本框中,则问题出在data.id上。在成功处理程序的开头放置一个断点,看看究竟是什么数据和data.id。

Also it looks like you may have a SQL injection vulnerability on the PHP side of things. You should read up on this. As things stand there's a good chance someone can delete all your student records just by entering ';delete from students;-- as their ID.

此外,您可能在PHP方面有一个SQL注入漏洞。你应该读一下这个。事实上,只要输入';从学生中删除; - 作为他们的ID,就有可能删除所有学生记录。