Michael Grigsby
Michael Grigsby

Reputation: 12163

Error sending form fields through jquery ajax

I am having a problem sending the dataString to the server. Aparently it is not pulling the values correctly from the form. Below is my jquery and my 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");
        }
      }
   });
}
});

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

Upvotes: 1

Views: 87

Answers (2)

mintobit
mintobit

Reputation: 2383

Use .val() to get the value of the element. And change your dataString to:

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

From api.jquery.com: jQuery.ajax data

Upvotes: 1

Bishnu Paudel
Bishnu Paudel

Reputation: 2079

The code $("#birthdate"); will return an element with id #birthdate but not it's value. use val function to get the value in it. like var birthdate = $("#birthdate").val();

Upvotes: 0

Related Questions