How do I pass form data to PHP and return a json string to Javascript?


Does anyone know how pass form values into PHP and still return the data to JavaScript? (For use in Google Charts if anyone is wondering.)

I have an HTML form with 4 radio boxes. I'd like to pass the value of the form so that my PHP request will be modified based on the user's selection.

The results from the PHP request need to be passed to JavaScript for processing.




In the radio button on click event place a javascript function that will perform an XMLHttpRequest to the php page and have the PHP page echo some JSON content that can be decoded in the return of the XHR in Javascript.

Example of such




May this can help you. I think that you must do an Ajax request

    method: "POST",
    url: "some.php",
    data: { name: "John", location: "Boston" }
.done(function( data) {
    alert( "Data Saved: " + data);

You send data to server form your form (here sent arename andlocation). You read this data with javascript with OnSelect and transfert it to your PHP code and, You get back data from server into your JavaScript code and you can do what you want with it. Sorry if my solution use JQuery, it is better for cross-browser with less code writing !




You could use this line to parse an array into json and use it later in javascript:

json_encode($rows); //format array named $rows into json data

More info:

People are also looking for solutions to the problem: php - using for loop on mysql table


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.