问题描述:

The first issue I encountered, I have array data that needs to be sent together with string data in one ajax post. I was able to solve it with this:

data: {name_dependents:name_dependent_array, age_dependents:age_dependent_array, dob_dependents:dob_dependent_array, dataString:dataString},

But then I can't save the data I have for dataString through simple $_POST.

Now I'm trying to convert my string post data to array so I can have uniform data (Since the first 3 data are arrays and the last one is a string).

But when I do, I always get this error(Link: my_home_url/POST):

404: Page not found

This error is generated when there was no web page with the name you specified at the web site.

Troubleshooting suggestions:

Ensure the page you are linking to exists in the correct folder.

Check your file name for case sensitivity . Index.htm is not the same as index.htm!

Temporarily disable any rewrite rules by renaming your .htaccess file if it exists.

Here's the Ajax Passing the data:

function submitClick() {

var firstname = $("#firstname").val();

var lastname = $("#lastname").val();

var username = $("#username").val();

var mobile_no = $("#mobile_number").val();

var password = $("#password").val();

var birth_month = $("#month").val();

var birth_day = $("#day").val();

var birth_year = $("#year").val();

var email = $("#email").val();

var sec_question_uuid = $("#security_question").val();

var sec_answer = $("#security_answer").val();

var civil_status = $("#status_civil").val();

var gender = $("#gender").val();

// var a = {

// firstname : firstname,

// lastname : lastname,

// username : username,

// mobile_no : mobile_no,

// password : password,

// birth_month : birth_month,

// birth_day : birth_day,

// birth_year : birth_year,

// email : email,

// sec_question_uuid: sec_question_uuid,

// sec_answer : sec_answer,

// civil_status : civil_status,

// gender : gender,

// command : command

// };

// alert(a); return false;

var dataString = 'firstname='+ firstname + '&lastname='+ lastname +'&username='+ username + '&mobile_number=' + mobile_no + '&password=' + password + '&birth_month=' + birth_month + '&birth_day=' + birth_day + '&birth_year=' + birth_year + '&email='+ email + '&sec_question_uuid=' + sec_question_uuid + '&sec_answer=' + sec_answer + '&civil_status=' + civil_status + '&gender='+ gender + '&command=signup';

var name_dependent_array = $('input[name="name_dependent[]"]').map(function(){return $(this).val();}).get();

var age_dependent_array = $('input[name="age_dependent[]"]').map(function(){return $(this).val();}).get();

var dob_dependent_array = $('input[name="dob_dependent[]"]').map(function(){return $(this).val();}).get();

if(firstname == ''|| lastname == '' || email == '' || gender == '' ||password == ''){

alert("Please Fill All Fields"); return false;

} else {

$.ajax({

type: 'POST',

url: 'functions.php',

data: {name_dependents:name_dependent_array, age_dependents:age_dependent_array, dob_dependents:dob_dependent_array, dataString:dataString},

success: function() {

$("#message").html('<div class="alert alert-success alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>Your data has been successfully saved. You can now log on to the site by clicking the Login tab.</div>');

// $('[name="firstname"]').val('');

// $('[name="lastname"]').val('');

// $('[name="email"]').val('');

// $('[name="password"]').val('');

// $('[name="verify"]').val('');

window.setTimeout(function() {

$(".alert").fadeTo(1500, 0).slideUp(500, function(){

$(this).remove();

});

}, 5000);

}

});

return false;

}

}

Notice the lines commented out, that's the data I'm converting to array so they are uniform but it's not working so I went back to the previous data I made:

var dataString = 'firstname='+ firstname + '&lastname='+ lastname +'&username='+ username + '&mobile_number=' + mobile_no + '&password=' + password + '&birth_month=' + birth_month + '&birth_day=' + birth_day + '&birth_year=' + birth_year + '&email='+ email + '&sec_question_uuid=' + sec_question_uuid + '&sec_answer=' + sec_answer + '&civil_status=' + civil_status + '&gender='+ gender + '&command=signup';

This is the data when I do print_r($_POST):

Array (

[name_dependents] =>

Array (

[0] => asd

[1] => hfg

)

[age_dependents] =>

Array (

[0] => 6

[1] => 6

)

[dob_dependents] =>

Array (

[0] => 2010-07-25

[1] => 2010-07-02

)

[dataString] => firstname=a&lastname=a&username=a&mobile_number=09176229999&password=a&birth_month=01&birth_day=1&birth_year=1937&[email protected]&sec_question_uuid=1a78e916f8a2affa1d1de00be7e41f91&sec_answer=a&civil_status=32899cec496b4b25c43c8c4444f24403-married&gender=male&command=signup

)

How do I fix dataString so I can save the data in MySQL similar to this:

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

$username = $_POST['username'];

$mobile_number = $_POST['mobile_number'];

$password = $_POST['password'];

$birth_month = $_POST['birth_month'];

$birth_day = $_POST['birth_day'];

$birth_year = $_POST['birth_year'];

$email = $_POST['email'];

$sec_question_uuid = $_POST['sec_question_uuid'];

$sec_answer = $_POST['sec_answer'];

$civil_status = explode("-", $_POST['civil_status']);

$civil_status_uuid = $civil_status[1];

$gender = $_POST['gender'];

网友答案:

In your php script where you get the posted array you can parse the dataString via parse_str() php function like that:

<?php
    // ...

    $datastring = $_POST['datastring'];
    parse_str($datastring);
    // now you have all your vars ready
    // $firstname, $lastname, $username etc
    // you only need to split $civil_status
    // ...
相关阅读:
Top