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

643

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.

799

Answer

Solution:

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

706

Answer

Solution:

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

$.ajax({
    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 !

606

Answer

Solution:

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: http://php.net/manual/en/function.json-encode.php

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

Source

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.