Tim H
Tim H

Reputation: 205

AJAX Post - One variable is not being sent properly

I have four variables I'm trying to pass via AJAX to be processed by some PHP on the same page: newJudgeName, newJudgeSection, newJudgeStatus, and originalJudgeName. The success function echos them out and they're the correct values, it's just the newJudgeStatus variable is not being picked up by my PHP. I've switched newJudgeStatus with newJudgeName in the data line on the AJAX request and then the value is sent just fine (I can see it in the db under Judge Name)... it's only when it's in the original spot in the ajax request that it doesn't work. I'm completely new to Ajax. Any help would be much appreciated.

AJAX:

    $.ajax({   
        type: "POST",   
        url: "test.php",   
        data: 'newJudgeName=' + newJudgeName + '&newJudgeSection=' + newJudgeSection + '&newJudgeStatus =' + newJudgeStatus + '&originalJudgeName=' + originalJudgeName,
        success: function(){
            alert('newJudgeName=' + newJudgeName + '&newJudgeSection=' + newJudgeSection + '&newJudgeStatus =' + newJudgeStatus +'&originalJudgeName=' + originalJudgeName);
        }
    });

PHP:

if(isset($_POST['newJudgeName'])){
        $newJudgeName = $_POST['newJudgeName'];
        $newJudgeSection = $_POST['newJudgeSection'];
        $newJudgeStatus = $_POST['newJudgeStatus'];
        $originalJudgeName = $_POST['originalJudgeName'];
        $judgeID = judgeNametoID($originalJudgeName);
        $con = mysql_connect("-","-","-");
        if (!$con)
          {
          die('Could not connect: ' . mysql_error());
          }
        else {
            // connected to database successfully
        }
                    mysql_select_db("cm", $con);
        $query = ("UPDATE `casemanagers`.`judges` SET `Name`='$newJudgeName' , `Section`='$newJudgeSection', `Active`='$newJudgeStatus' WHERE `judgeID`='$judgeID';");
        $result = mysql_query($query);
        mysql_close($con);
}

Upvotes: 1

Views: 245

Answers (3)

Shafiul
Shafiul

Reputation: 2890

Instead of manual processing, I'll recommend to put all these inside a form & use following code to send data:

$('#id_of_the_form').serialize();

instead of buggy:

newJudgeName=' + newJudgeName + '&newJudgeSection=' + newJudgeSection + '&newJudgeStatus =' + newJudgeStatus +'&originalJudgeName=' + originalJudgeName ...

See http://api.jquery.com/serialize/

Upvotes: 1

Facundo Farias
Facundo Farias

Reputation: 408

You should send the data this way:

$.ajax({   
    type: "POST",   
    url: "test.php",   
    data: {
        'newJudgeName' : newJudgeName,
        'newJudgeSection' : newJudgeSection,
        'newJudgeStatus' : newJudgeStatus,
        'originalJudgeName' : originalJudgeName
    },
    success: function(){
        alert('newJudgeName=' + newJudgeName + '&newJudgeSection=' + newJudgeSection + '&newJudgeStatus =' + newJudgeStatus +'&originalJudgeName=' + originalJudgeName);
    }
});

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270609

You have an errant space in your data string:

'&newJudgeStatus =' + newJudgeStatus +
---------------^^^^

// Should be
'&newJudgeStatus=' + newJudgeStatus +

Upvotes: 5

Related Questions