2016-08-17 75 views
2

我遇到的第一个问题是,我需要将数组数据与字符串数据一起发送到一个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文件临时禁用任何重写规则。

这里的阿贾克斯传递的数据:

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; 
    } 
} 

通知行注释掉,这让他们有统一的我转换到阵列中的数据,但它不工作,所以我又回到了以前的数据我制作:

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 protected]&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

'404:Page not found' ??? –

+0

当我替换为: '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 ='+性别+'&command =注册';' 与此: 'var a = {data:some_variable_with _data}' 错误显示。 –

+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 
    // ... 
相关问题