SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast ravi_k47's Avatar
    Join Date
    Feb 2009
    Location
    india
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question JSON array is empty

    Hi,

    i am doing a password reset form using Jquery and PHP

    if i try to submit an email id it should sent and email and report back the response text as success so that the user knows email has been sent.

    But i am stuck with JSON submit as i have an empty array to decode at the serverside.
    I am using minified version of json2.js from the official json.org website

    Here is the code.

    Code JavaScript:
    	var formdata = $("#log-box").serializeArray();
    	formdata = JSON.stringify(formdata);
    	var notifymsg;
    	alert(formdata);
    	$.ajax({
    			type: "POST",
    			url: "forgot-pass.php",
    			contentType: 'application/json',
    			data: formdata,
    			success: function(responsedata){
    				var some = responsedata.split("&");
    				$.each(some, function(index,value){
    					//alert("index="+index+"value="+value);
    				});
    				},
    				error: function(o, s, e){
    						alert("Form not posted \n"+e);
    				}
    			});
    formdata alerted gives:

    [{"name":"email","value":"ravi.k@gmail.com"},{"name":"acctype","value":"loginaccount"}]


    PHP forgot-pass.php

    Code PHP:
    print_r($_POST);

    gives me

    Array
    (
    )

    Can someone point out what i might be doing wrong? i am a designer and this is the first time i am trying to do JSON :P

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What happens if you comment out your second line that turns the array into a string which may be whats sending an empty $_POST array.

  3. #3
    SitePoint Enthusiast ravi_k47's Avatar
    Join Date
    Feb 2009
    Location
    india
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think the stringifier function will encode the string to the correct JSON format and that is what i used it for. i have seen threads that encoding and decoding in the correct JSON format is tricky and i saw it here the easier way is to do this

    I tried using the code from http://www.factsandpeople.com/facts-...x-json-and-php

    but it seems like the content type passed string/array/object is not recognized properly at the server side.

    thanks for the quick response, im still trying to figure out what type of content it should be. I prefer it to be array rather than string because its the best method to submit and process a form

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Usually with arrays you don't need to set a content-type yourself as jQuery usually does this for you.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •