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.

2
  • 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? Commented Apr 14, 2009 at 15:18
  • 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? Commented Apr 14, 2009 at 16:00

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 this

new 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.

2 Comments

Why bother including a whole framework for this? You don't need one, especially one like prototype.
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.
0

You do not have to just show what's 'printed in that PHP file', your PHP file could print JavaScript commends back to your page. You could then, upon receiving the response, execute those commands. I like to use the eval function for this, but many people here will discourage you from doing so :)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.