1

我在将 dataString 发送到服务器时遇到问题。显然,它没有从表单中正确提取值。下面是我的 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));
    }
4

2 回答 2

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 数据

于 2012-08-09T00:20:52.487 回答
0

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

于 2012-08-08T23:55:45.290 回答