0

I have an HTML form, when clicked Submit, gives me an output in JSON format with the help of

$('#result').text(JSON.stringify($('form').serializeObject()));

My HTML form has action="filename.php"* and its method as post

Now I want to send/read these values in a PHP file and should be able to perform operations on them. Like, print them on screen (Echo) or insert them into database.

Please help on how to get the values in readable/accessible format in my php file? Thanks in advance.

3
  • 2
    Ah please check out .ajax() Commented Apr 29, 2013 at 13:17
  • Javascript is not needed in this case (at least, with the details currently provided in the question). Also, this is a very basic problem that can easily be solved through a quick Google search. Search for PHP form, PHP POST, PHP GET Commented Apr 29, 2013 at 13:17
  • 1
    $.post('url here', post data here eg: { id : 12 }, function(returnedPage) { this is the callback function eg: alert(returnedPage); }); Commented Apr 29, 2013 at 13:20

2 Answers 2

1

In filename.php

$formData = json_decode($_POST['fieldname']);

will decode the json string from your form post into a PHP object. If you want an array pass true as the second param to the json_decode method

You'll need to replace fieldname with the attribute name from your HTML / Javascript that holds the json data.

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

8 Comments

<?php echo json_decode($_GET['Fname']); ?>this is working.. but when I do an echo, I see nothing.
you can't echo an array or object, as such. If you want to see if for debugging then var_dump it. If you want to output it on the page you need to loop over it and echo each node
when I do echo$_GET['Fname'], I see the value entered in my HTML form
Thats because before you do the json_decode() call you have a JSON string, which can be echo'd
hmm makes sense... but when I do var_dump, I see "null" there.
|
0

You can post the variables as normal (no JQuery serialization to JSON) and then you can access the posted variables via the $_POST variable in filename.php.

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.