如何使用JQuery将数据库值传递到我的表单中?

时间:2021-10-09 20:19:47

I need to pass the return value from a php script back into my html form but because the page does not refresh, I have no idea how to get this value into the form. Can someone please help? Any ideas are welcome.

我需要将PHP脚本的返回值传递回我的html表单,但由于页面没有刷新,我不知道如何将这个值放入表单中。有人可以帮忙吗?欢迎任何想法。

This is my understanding of how JQuery works, well at least the way I am using it anyhow. The user fills in the form. The form is submitted. JQ picks up the values and sends them via a query string. The JQ code points to a url which is where my php class is at that will return the value I need for the form. I'm thinking I should be able to append a hidden textbox using jquery but I have no idea how to do this. I'm not even sure this will work.

这是我对JQuery如何工作的理解,至少我无论如何使用它的方式。用户填写表格。表格已提交。 JQ获取值并通过查询字符串发送它们。 JQ代码指向一个url,这是我的php类所在的位置,它将返回表单所需的值。我想我应该能够使用jquery附加一个隐藏的文本框但我不知道如何做到这一点。我甚至不确定这会起作用。

Please help.

2 个解决方案

#1


You can use the callback function of $.get (or $.post) to inject the return value back into the form. Something like this:

您可以使用$ .get(或$ .post)的回调函数将返回值注入表单。像这样的东西:

$.get("test.php", function(data){
  $('#myFormField').val(data);
});

Simple as that!

就那么简单!

#2


You're looking for something like this:

你正在寻找这样的东西:

$.load('myScript.php', function(data) {
    //fill a hidden textbox with your script output and reveal it
    $('#textBox').val(data).show();
)};

#1


You can use the callback function of $.get (or $.post) to inject the return value back into the form. Something like this:

您可以使用$ .get(或$ .post)的回调函数将返回值注入表单。像这样的东西:

$.get("test.php", function(data){
  $('#myFormField').val(data);
});

Simple as that!

就那么简单!

#2


You're looking for something like this:

你正在寻找这样的东西:

$.load('myScript.php', function(data) {
    //fill a hidden textbox with your script output and reveal it
    $('#textBox').val(data).show();
)};