通过jquery ajax发送表单字段时出错

时间:2012-08-08 23:50:59

标签: php jquery codeigniter

我在将数据字符串发送到服务器时遇到问题。显然它没有正确地从表单中提取值。下面是我的jquery和我的php。

$(document).ready(function() {  
    $("#submitForm").live('click', function() {
        updateUserInfo();
    });

var birthdate = $("#birthdate");
var sex = $("#sex");
var interestedIn = $("#interestedIn");
var relationshipStatus = $("#relationshipStatus");
var knownLanguages = $("#knownLanguages");
var religiousViews = $("#religiousViews");
var politicalViews = $("#politicalViews");
var aboutMe = $("#aboutMe");
var mobilePhone = $("#mobilePhone");
var neighborhood = $("#neighborhood");
var website = $("#website");
var email = $("#email");

var dataString = birthdate + sex + interestedIn + relationshipStatus + knownLanguages + politicalViews + aboutMe + mobilePhone + neighborhood + website + email;

function updateUserInfo() {
    jQuery.ajax({
        type: "POST",
        dataType: "JSON",
        url: "<?=base_url()?>index.php/regUserDash/updateUserInfo",
        data: dataString,
        json: {userInfoUpdated: true},
        success: function(data) {
        if(data.userInfoUpdated == true) {
            alert("hello");
        }
      }
   });
}
});

我的PHP:

 public function updateUserInfo() {
        $userid = $this->session->userdata('userid');
        $birthdate = $this->input->post("birthdate");
        $sex = $this->input->post("sex");
        $interestedIn = $this->input->post("interestedIn");
        $relationshipStatus = $this->input->post("relationshipStatus");
        $languages = $this->input->post("languages");
        $religiousViews = $this->input->post("religiousViews");
        $politicalViews = $this->input->post("politicalViews");
        $aboutMe = $this->input->post("aboutMe");
        $mobilePhone = $this->input->post("mobilePhone");
        $neighborhood = $this->input->post("neighborhood");
        $websites = $this->input->post("websites");
        $email = $this->input->post("email");

        $this->db->query("INSERT IGNORE INTO user_info (birthdate, sex, interestedIn, relationshipStatus, Languages, religiousViews, politicalViews, aboutMe, mobilePhone, neighborhood, websites, email, userid)
                          VALUES('{$birthdate}', '{$sex}', {$relationshipStatus}', '{$languages}, {$religiousViews}', '{$politicalViews}', {$aboutMe}', '{$mobilePhone}' {$neighborhood}', '{$websites}', {$email}', '{$userid}')");

        echo json_encode(array('userInfoUpdated' => true));
    }

2 个答案:

答案 0 :(得分:1)

使用.val()获取元素的值。 并将dataString更改为:

var dataString = {'birthdate' : birthdate, 'sex' : sex, 'interestedIn' : interestedIn, 'relationshipStatus' : relationshipStatus, 'knownLanguages' : knownLanguages, 'politicalViews' : politicalViews, 'aboutMe' : aboutMe, 'mobilePhone' : mobilePhone, 'neighborhood' : neighborhood, 'website' : website, 'email' : email};

来自api.jquery.comjQuery.ajax data

答案 1 :(得分:0)

代码$("#birthdate");将返回一个id为#birthdate但不是它的值的元素。使用val函数来获取其中的值。比如var birthdate = $("#birthdate").val();