-1

i am trying to register in to my database but not able to send it in json format.I tried lot by searching and applying it on this code but no result is saving in database..How should i send data in json format .... sign_up_jump.js

// JavaScript Document

$('document').ready(function()
{ 
 /* validation */
 $("#signup").validate({
  rules:
  {
        firstname: {
        required: true,
        minlength: 3
        },
        password: {
        required: true,
        minlength: 8,
        maxlength: 15
        },
        passwordConfirmation: {
        required: true,
        equalTo: '#password'
        },
        email: {
        required: true,
        email: true
        },
   },
   messages:
   {
        firstname: "please enter user name",
        password:{
                  required: "please provide a password",
                  minlength: "password at least have 8 characters"
                 },
        email: "please enter a valid email address",
        passwordConfirmation:{
                    required: "please retype your password",
                    equalTo: "password doesn't match !"
                  }
   },
   submitHandler: submitForm    
   });  
   /* validation */

   /* form submit */
   function submitForm()
   {        
            var data = $("#signup");
            var data = JSON.stringify(data);
            $.ajax({

            type : 'post',
            url  : 'new_api.php',
            data : data,
            datatype: 'json',
            beforeSend: function()
            {   
                $("#error").fadeOut();
                $("#submit").html('<span class="glyphicon glyphicon-transfer"></span> &nbsp; sending ...');
            },
            success :  function(data)
                       {                        
                            if(data.result==1){

                                $("#error").fadeIn(1000, function(){


                                        $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; Sorry email already taken !</div>');

                                        $("#submit").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account');

                                });

                            }
                            else if(data.result =="registered")
                            {

                                $("#submit").html('<img src="btn-ajax-loader.gif" /> &nbsp; Signing Up ...');
                                setTimeout('$(".form-signin").fadeOut(500, function(){ $(".signin-form").load("login.php"); }); ',5000);

                            }
                            else{

                                $("#error").fadeIn(1000, function(){

                    $("#error").html('<div class="alert alert-danger"><span class="glyphicon glyphicon-info-sign"></span> &nbsp; '+data+' !</div>');

                                $("#submit").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account');

                                });

                            }
                       }
            });
            return false;
    }
   /* form submit */
 });

output

Mohanish
  • 269
  • 3
  • 11
  • 1
    Look in your web browser's console if there are any errors when the ajax request is fired – Goufalite Jul 29 '16 at 10:11
  • from your code i can see you are not using any json, where exacly do you want json? – madalinivascu Jul 29 '16 at 10:38
  • i want to send it by ajax..how should i send it.. – Mohanish Jul 29 '16 at 10:43
  • @Mohanish — http://stackoverflow.com/questions/5570747/jquery-posting-json – Quentin Jul 29 '16 at 10:54
  • @Quentin.. sir i had edidet some part ..now i get value as null..why..? – Mohanish Jul 29 '16 at 11:16
  • @Mohanish — You're trying to convert a jQuery object into JSON instead of extracting the data you care about from the form into an object and converting that into JSON. You've also failed to change the PHP to read a JSON formatted request. (Also: When you are trying to debug this, start by looking at what you are sending to PHP and not what PHP responds with). – Quentin Jul 29 '16 at 11:18

1 Answers1

1

There are three bits of your code which are relevant here:


var data = $("#signup").serialize();

This gets the data in form URL encoded format.


contentType:  'application/json; charset=utf-8',

This claims you are sending JSON. Since you have the data in form URL encoded format, this is a lie.


$firstname = $_POST['firstname'];

$_POST is only populated with the browser sends URL encoded (or multipart) data. Since you claim you are sending JSON, it is not populated.


The quick fix: Stop lying. Remove the line claiming you are sending JSON.


If you really want to send JSON then:

  1. Read this question about sending JSON with jQuery Ajax
  2. Read this question about reading a JSON formatted request with PHP.

You need to do both!


Re: Edit:

var data = $("#signup");
var data = JSON.stringify(data);

You're trying to convert a jQuery object into JSON instead of extracting the data you care about from the form into an object and converting that into JSON.

Look at what the first linked question does:

data: JSON.stringify({ "userName": userName, "password" : password })

It doesn't pass a jQuery object to JSON.stringify.

You have also completely ignored the changes you need to make to the PHP (as described in the second linked question).

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • i put code just to get how i can send data in json format..i dont know what u get by it..but i want solution how to send it in JSON ..if there is any fault in code please tell me how can i change it.. – Mohanish Jul 29 '16 at 10:17
  • @Mohanish — That's what this answer (and the ones I linked to) tell you. – Quentin Jul 29 '16 at 10:18
  • @Mohanish — The answers already explain everything you need to know. If there is something about them you don't understand then try asking a more specific question. – Quentin Jul 29 '16 at 10:26