0

I am trying to make a ajax call to do a database update in my php class. However, it seems the class is being called but the parameters are not passed for some reason. Here is my jquery:

$(".sendRSVP").click(function(e){
  e.preventDefault();
  var nameArray = [];
  //var uniqueCode =  parseInt($(this).find('.theCheckbox').attr('id'));
  //var response =  ($(this).find('.theCheckbox').is(":checked")) ? '1' : '0';
  //the parameters passed should be uniqueCode and response which both gave legit values
  if($("#displayContacts").is(":visible")){
    $.get("submitRSVP.php", {rs: '1', resp: '12345'})
    .done(function(rtn){
      console.log(rtn); //error is returned
    })
  }
});

Here is my php code:

<?php
    require 'dbh.php';
    $rsvp = $REQUEST["rs"];
    $response = $REQUEST["resp"];
    session_start();
    if(session_start()) $invitationCode = $_SESSION['login_user'];
    $hint = "here1";
    try{
        $updateQuery = "UPDATE `db686470460`.`GuestWithPlusOnes` SET `Confirmed`= '$response' WHERE `GuestWithPlusOnes`.`UniqueID`= '$rsvp'";
        $updateStmt = $conn->prepare($updateQuery);
        $updateStmt->execute();
        if ($updateStmt->rowCount() > 0) {
            $hint = 'success';
        }else {
            $hint = 'error';
        }
        $_SESSION['login_user'] = $rsvp;
        $updateStmt = null;
    }
    catch(Exception $e){
        $hint = $e;
    }
    echo $hint;
?>

I definitely have a record in my table with that uniqueId because when I change the query to:

 $updateQuery = "UPDATE `db686470460`.`GuestWithPlusOnes` SET `Confirmed`= '1' WHERE `GuestWithPlusOnes`.`UniqueID`= '12345'";

that updates as normal. Is there something else I could be missing?

saint
  • 268
  • 1
  • 5
  • 16
  • 1
    first try to chage this `$REQUEST["rs"]` to `$_REQUEST["rs"]` – Andriy Lozynskiy Dec 20 '17 at 19:01
  • Check your variables assignment, you are sending {rs: '1', resp: '12345'} and you are setting your rsvp to the rs (1), but in the hardcoded query you said that works you are sending 12345. I guess you should set **$rsvp=$REQUEST['resp']** – Jordy García Dec 20 '17 at 19:03
  • 2
    Try changing $REQUEST to $_REQUEST – Iker Vázquez Dec 20 '17 at 19:07
  • @AndriyLozynskiy that was the issue exactly. Thanks all – saint Dec 20 '17 at 19:13
  • Two pieces of advice: If you're updating the db, you might want to use the POST method. The GET method is meant for requesting data, whereas POST is for updating. Additionally, whichever method you use, you should query the values from the $_GET or $_POST array instead of $_REQUEST. More info here: https://stackoverflow.com/questions/1924939/among-request-get-and-post-which-one-is-the-fastest – nageeb Dec 20 '17 at 20:00

0 Answers0