0

I have an js function that is collecting data and sending it to a php file.

I am trying to submit an array as part of the post:

function send_registration_data(){

var string = "{username : " + $('#username').val() + ", password : " + $('#pass1').val() + ", level : " + $("#userrole").val() + ", 'property[]' : [";
var c = 0;
$('input[name=property]:checked').each(function(){
    if( c == 0){
        string +="\"" +this.value+"\"";
        c=1;
    } else {
        string +=",\""+this.value+"\"";
    }
});
string+="]}";
$('#input').html( JSON.stringify(eval("(" + string + ")")) );

$.ajax({ url: './php/submit_registration.php',
         //data: { username : $('#username').val() , password : $('#pass1').val() , email : $('#email').val() , level : $("#userrole").val() },
         data: JSON.stringify(eval("(" + string + ")")) ,
         type: 'post',
         success: function(output) {
                  $('#output').html( output );

            }
});
};

On submit my php file returns an the POST array as NULL. I am not sure what I am doing wrong here.

EDIT: IT is the same weather I try to convert the string to json or not.

ALso, the inputs contain just text names.

6
  • 1
    Is all this wrapped in a form element? If so, you can serialize the form - data: $('form').serialize() Commented Dec 13, 2013 at 20:58
  • When I was not using the conversion of json string to json it was doing the same thing. Commented Dec 13, 2013 at 20:59
  • @NaNpx Yes, let me try this. Commented Dec 13, 2013 at 21:00
  • Have you examined the string you produce to see that it doesn't include anything that is causing an error? Commented Dec 13, 2013 at 21:00
  • you definitely do not want to stringify it. What you want is what you originally had, the commented out part. If that isn't working, the problem is likely on the php side. Commented Dec 13, 2013 at 21:04

2 Answers 2

1

string keyword

Do not use the "string" keyword.

eval

Eval is evil - use it with caution.

strict mode

Make sure always to work in the "strict mode" by placing this line at the beginning of your code:

'use strict'

Building your response object

You do not have to glue your post object manually. Just do it this way:

var post = {
    'username': $('#username').val(),
    'password': $('#password').val(),
    'myArray[]': ['item1', 'item2', 'item3']
};

jQuery the right way

Avoid messing up with unnecessary syntax.

$.post(url, post)
    .done(function(response){
        // your callback
    });

Conclusion

'use strict'
var url = './php/submit_registration.php'; // try to use an absolute url
var properties = {};
$('input[name="property"]:checked').each(function() {
    properties.push(this.value);
});
var data = {
    'username':   $('#username').val(),
    'password':   $('#pass1').val(),
    'level':      $('#userrole').val(),
    'property[]': properties
};

// submitting this way
$.post(url, data)
    .done(function(response) {
        // continue
    })
    .fail(function(response) {
        // handle error
    });

// or this way
$.ajax({
    type: 'POST',
    url: url,
    data: JSON.stringify(data), // you'll have to change "property[]" to "property"
    contentType: "application/json",
    dataType: 'json',
    success: function(response) { 
        // continue
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

The $_POST array will still be null in this case if the input is not taken from php://input on the server side
@user602525 yeah.. he could json encode the array part of the package. Or encode it all.
I agree with your whole post of how to send up the data. But the real problem here with the $_POST array being NULL is simply, that he was trying to read from the $_POST array with json data. That doesn't work. He's gotta either use 'php://input' or $HTTP_RAW_POST_DATA
@user602525 you're right, but he didn't specified any server side code - so it's guessing. I'm 99% sure what you're saying is the actual problem - but I can't prove it.
0

You need to get from php://input if you are not using multipart/form-data, so, application/json

$myData = file_get_contents('php://input');
$decoded = json_decode($myData);

If you're sending this up as json, your $_POST variable will continue to be NULL unless you do this.

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.