tags:

views:

286

answers:

2

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.

+1  A: 

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!

John McCollum
Hi John, thanks. Gonna give this a shot now.
Hope it works for you. Make sure you read Karim's comment below - your PHP script should just echo out the value that you want to go in the form field.
John McCollum
+1  A: 

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();
)};
karim79
Hi Karim. I'm going to try this and John's suggestion if yours won't work for some reason. Thanks.
nutjob, I just edited my answer.
karim79
Karim, this looks like it will work however I am having another issue. The onlt way for me to get my return value is to instantiate my class and point to the method like this: $id = $report->getID();.............. I don't see how I can use that inside the javascript .load function. Can you please advise?
Your PHP script should simply echo out the value you want to store in your textbox. That value becomes available as 'data' from the $.load function, so you won't be needing any 'php' within your Javascript.
karim79