A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearRetrieving PHP variables from an AJAX form input - page 1
User InfoPosts
Retrieving PHP variables from an AJAX form input#1
I have a form in a PHP sending variables to a PHP file which duly inserts them into a MySQL table.

I currently have a div displaying the response from the PHP (which is anything that is printed by the PHP).

All works fine. The problem is I want to use variables that are created/updated during the PHP MySQL insert process. I.e. not only show what is printed in that PHP file, but USE those variables.

I have seen complicated use of the JSON Encoding to possibly cross this divide, but I(d love to know if that(s the simplest approach. And if anyone has any good links or examples on the subject.

posted date: 2009-04-14 07:16:00


Re: Retrieving PHP variables from an AJAX form input#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-14 07:16:01


Re: Retrieving PHP variables from an AJAX form input#3
What do you mean by "use" the variables? The PHP form page can't use anything at that point, it's already finished executing. Use them in Javascript?

posted date: 2009-04-14 07:18:00


Re: Retrieving PHP variables from an AJAX form input#4
I assume that you want to be able to have multiple pieces of data sent back via AJAX to your page and manipulate those.JSON is indeed the simplest way to do this. If you use PHP5, you can use json_encode() from the PHP side to send a complicated data type (such as an object or an array) back to the browser page. Then in the javascript, you use eval() on the data that is sent back (ex: var data = eval(response);) to parse it back into a usable complicated type in javascript.There are tons of tutorials out there that will show you how to do this and explain it in further detail than a response here ever could.

posted date: 2009-04-14 07:21:00


Re: Retrieving PHP variables from an AJAX form input#5
Use PrototypeJS and do it like this:Have some PHP like this $jsonHeader = array(); if($_REQUEST[(param1(]) { echo (<p>You passed ( . $_REQUEST[(param1(] . (</p>(; $jsonHeader[(status(] = (Success(; }else { $jsonHeader[(status(] = (Failed because the request was invalid(; } if(is_array($jsonHeader) and sizeof($jsonHeader) > 0) { header((X-JSON: (( . json_encode($jsonHeader) . ()(); }Then make your Ajax call like thisnew Ajax.Request((dostuff.php(, { method: (get(, parameters: {(param1(: (this is param 1(}, onSuccess: function(response, jsonHeader){ if(jsonHeader[(status(] == (Success(){ //Everything is OK, do stuff }else{ alert(jsonHeader[(status(]); } }, onFailure: function(){ alert((Fail!(); } });Prototype grabs the X-JSON header returned by PHP and automatically sets the jsonHeader argument of the onSuccess function to a Javascript array of the values that were originally set in PHP.The above scenario is good as long as the amount of data you(re returning to Javascript fits in the HTTP header.If you need to pass back lots of data, just have PHP output the JSON encoded result rather than making it part of the header. Then you can use the evalJSON() method of the response object in your Ajax call.

posted date: 2009-04-14 07:23:00


Re: Retrieving PHP variables from an AJAX form input#6
Just use the "echo" function to put put PHP variables to the standard output put.echo $myVarName;Or, I prefer the printf(), be sure to check for HTML in the input BEFORE you output to avoid XSS issues.Use something like this:printf("Your input was: %s", strip_tags(%myInputVar));Also, remember to use the %d or %f formatters when outputting number for best security.

posted date: 2009-04-14 07:24:00


Re: Retrieving PHP variables from an AJAX form input#7
Why bother including a whole framework for this? You don't need one, especially one like prototype.

posted date: 2009-04-14 07:36:00


Re: Retrieving PHP variables from an AJAX form input#8
I wouldn't call Prototype a framework. I like it because it makes the Ajax calls & JSON stuff so much easier on the Javascript-side.

posted date: 2009-04-14 07:47:00


Re: Retrieving PHP variables from an AJAX form input#9
I mean print them. Basically, I'm trying to get the variables that have been sent to the PHP to print back into the values of the form, so the form is always representative of what's on the DB... i've just thought it doesnt need to come from the PHP... I could print from the AJAX maybe?

posted date: 2009-04-14 08:00:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.