user109162
user109162

Reputation: 147

How do I pass a database value into my form using JQuery?

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.

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.

Please help.

Upvotes: 0

Views: 944

Answers (2)

karim79
karim79

Reputation: 342675

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();
)};

Upvotes: 1

John McCollum
John McCollum

Reputation: 5142

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

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

Simple as that!

Upvotes: 1

Related Questions