Eymiєl Moralєs
Eymiєl Moralєs

Reputation: 89

Passing 2 datas from AJAX to PHP

So I'm trying to pass 2 datas from AJAX to PHP so I can insert it in my database but there seems to be something wrong. My computation of the score is right but it seems that no value is being passed to my php file, that's why it's not inserting anything to my db.

AJAX:

<script type = "text/javascript" language="javascript">
$(document).ready(function() {
    $("#finishgs").click(function(){       
        var scoregs = 0;
        var remarkgs = "F";
        var radios = document.getElementsByClassName('grammar');
        for (var x=0; x<radios.length; x++){
            if (radios[x].checked) {
                scoregs++;
            }
            else
              scoregs = scoregs;
        }

        if (scoregs >= 12){
           remarkgs = "P";
        }
        else{
           remarkgs = "F";
        }
    });
});

$(document).ready(function() { 
    $("#GTScore").click(function(event) {
        $.post(
        "dbinsert.php",
        { scoregs:scoregs , remarkgs: remarkgs},
        function(data){
            $('#inputhere').html(data);  
          } 
        );
    });
});

PHP:

if( $_REQUEST["scoregs"] || $_REQUEST["remarkgs"]) {
    $scoregs = $_REQUEST['scoregs'];
    $remarkgs = $_REQUEST['remarkgs'];
}

Upvotes: 0

Views: 42

Answers (1)

Mawia HL
Mawia HL

Reputation: 3665

There is an extra closing bracket );, you should remove. Try this:

 $(document).ready(function() { 
       $("#GTScore").click(function(event) {
       event.preventDefault();//to prevent default submit
       $.ajax({
          type:'POST',
          url: "dbinsert.php",
           { 
            scoregs:scoregs , 
            remarkgs: remarkgs
           },
             success: function(data){
                 $('#inputhere').html(data);  
                 } 
          });
        });

And in php, you need to echo the variable or success/fail message after you insert data into the database:

echo $scoregs;
echo $remarkgs;

Upvotes: 1

Related Questions