我遇到的第一个问题是,我需要在一个ajax帖子中将字符串数据与字符串数据一起发送。我能用这个来解决它:
data: {name_dependents:name_dependent_array, age_dependents:age_dependent_array, dob_dependents:dob_dependent_array, dataString:dataString},
但是我无法通过简单的$_POST
保存我对dataString的数据。
现在我正在尝试将我的字符串发布数据转换为数组,因此我可以拥有统一的数据(因为前3个数据是数组,最后一个是字符串)。
但是当我这样做时,我总是会收到此错误(链接: 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.
检查文件名是否区分大小写。 Index.htm与index.htm不同!
通过重命名.htaccess文件(如果存在)暂时禁用任何重写规则。
这是Ajax传递数据:
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">×</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;
}
}
注意这些行被注释掉了,那就是我要转换为数组的数据,所以它们是统一的,但它不起作用,所以我回到了之前的数据:
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';
这是我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=a@gmail.com&sec_question_uuid=1a78e916f8a2affa1d1de00be7e41f91&sec_answer=a&civil_status=32899cec496b4b25c43c8c4444f24403-married&gender=male&command=signup
)
如何修复dataString
以便我可以将数据保存在MySQL中,类似于:
$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'];
答案 0 :(得分:-1)
在您获得已发布数组的php脚本中,您可以通过parse_str()
php function解析dataString,如下所示:
<?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
// ...