Eli Davies
Eli Davies

Reputation: 25

AJAX and PHP/SQL Uncertainty over the location of a error

I am simply trying to update an element in an array, and the information, for whatever reason, doesn't seem to be getting through. Here is the javascript that sends the data:

var xmlhttp = new XMLHttpRequest();
if (window.XMLHttpRequest) 
{
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} 
else 
{
    // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
    xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        // Do Nothing
    }
}
xmlhttp.open("GET","updateGame.php?q=" + str + "&e=" + recordText,true);
xmlhttp.send();

Where recordText is declared elsewhere.

And this is the PHP that hopefully recieves the q and e:

<?php

$q = intval($_GET['q']);
$e = strval($_GET['e']);



$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "*****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "UPDATE Games SET entries=$e WHERE PINs=$q";
$result = $conn->query($sql) or die($conn->error);

$conn->close();
?>

I have checked, and no information is being updated.

Upvotes: 1

Views: 64

Answers (1)

I fixed your Ajax file, here it is and it works fine, I also modified your PHP file to return the data just for testing purposes. Create two text files with the given names, copy-paste next codes and run from browser :

eli_davies_1.php

<html>
  <head>
    <script type="text/javascript">
function ajax_send ( a )
{ var ajax = false;
  if ( window.XMLHttpRequest )
       ajax = new XMLHttpRequest();
  else if ( window.ActiveXObject )
            ajax = new ActiveXObject( "Microsoft.XMLHTTP" );
  if ( ajax )
       { var qq = document.getElementById( "txt_q" ).value;  // DATA TO SEND.
         var ee = document.getElementById( "txt_e" ).value;  // DATA TO SEND.
         ajax.open("GET","eli_davies_2.php?q=" + qq + "&e=" + ee );  // EXECUTE PHP.
         ajax.onreadystatechange =
            function ()
            { if ( ( ajax.readyState ==   4 ) &&
                   ( ( ajax.status   ==   0 ) ||
                     ( ajax.status   == 200 ) ) )
                 { var d = document.getElementById( "div_result" );
                   d.innerHTML = ajax.responseText;  // DISPLAY DATA RETURNED.
                 }
            }
         ajax.send( null );
       }
}
    </script>
  </head>
  <body>
    <input type="text" id="txt_q" value="123" />
    <br/>
    <input type="text" id="txt_e" value="xyz" />
    <br/>
    <button onclick="ajax_send()">Send data</button>
    <br/>
    <div id="div_result"></div>
  </body>
</html>

eli_davies_2.php

<?php

$q = intval($_GET['q']);
$e = strval($_GET['e']);

echo "Data received = >" . $q . "< and >" . $e . "<";  // DATA RETURNED (FOR TESTING ONLY).
/*
$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "*****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "UPDATE Games SET entries=$e WHERE PINs=$q";
$result = $conn->query($sql) or die($conn->error);

$conn->close();
*/
?>

Upvotes: 1

Related Questions