Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This piece of code gets all the form variables and sends them via AJAX to the PHP script. But I want the calculated results from the PHP script that is being returned to the javascript via a JSON encoded array to be in the form of "post":{"uname":"someNamefromForm","email":"someEmail","fname":"namefromtheform","lname":"lastnamefromform" }... The output I'm getting now is "uname=e&email=e&fname=e&lname=euname".. This is the JSON array I want to displayed at the bottom of the page for debugging purposes. Can someone tell me how to format it please

This is my HTML form

  <div id="wrapper">
      <h2> Validation with AJAX,JQuery,JSON and PHP</h2>
          <div class="form-container">    
              <span id="ajax-message"></span>

              <form id="ajax-form" onsubmit="return false;">
                  <p class="legend">All fields marked with an asterisk are required.</p>

                  <fieldset>
                      <legend>User Details</legend>
                      <div>
                        <label for="uname">Username <em>*</em></label> 
                        <input id="uname" type="text" name="uname" value=""  />
                      </div>
                      <div>
                        <label for="email">Email Address <em>*</em></label> 
                        <input id="email" type="text" name="email" value="" />
                      </div>
                      <div>
                        <label for="fname" class="error">First Name <em>*</em></label> 
                        <input id="fname" type="text" name="fname" value="" size="50" class="error"/>
                        <p class='note'>All error message go here </p> 
                      </div>
                      <div>
                        <label for="lname">Last Name <em>*</em></label> 
                        <input id="lname" type="text" name="lname" value="" size="50" />
                      </div>
                  </fieldset>

                  <div class="buttonrow">
                      <input type="submit" value="Submit This Form via AJAX" class="button" />  
                      <input type="button" value="Start Again" class="button" />
                      <a >Refresh this Page</a>
                  </div>
              </form>
          </div> 
      <h3>JSON Array</h3>
      <pre id='debug'></pre>
  </div>

This is my javascript

$("#ajax-form").submit(function(){
          var variableToSend = $(this).serialize();
          $.post(
            'ajaxformval_post.php', 
            {variable: variableToSend}, 
            function(data){$("#debug").html(data)},
            "json"
          );
      })

This is the php

<?php
    $variable = $_POST['variable'];

    echo json_encode($variable);

    /*$json = array(
        'booleanFlag' => TRUE,
        'someText' => "AJAX should be renamed AJAJ",
        'anArrayOfData' => array('name' => 'Mickey', 'ears' => 'very Big') 
    );*/

 ?>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

You can send your serialized variable directly like this:

$("#ajax-form").submit(function(){
      var variableToSend = $(this).serialize();
      $.post(
        'ajaxformval_post.php', 
         variableToSend, 
        function(data){$("#debug").html(data)},
        "json"
      );
  });

Then on the server side simply output the json_encoded post:

<?php
    $variable = $_POST;

    echo json_encode($variable);
 ?>
share|improve this answer
    
Thanks alot, It works! :D –  Fruits Mar 23 at 4:06

Did you try changing the "json"-parameter from the $.post-method?

According to documentation https://api.jquery.com/jQuery.post/

dataType
Type: String
The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).

So I guess if you give him "json" he will automatically decode the returned data.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.