javascript - AJAX and PHP/SQL Uncertainty over the location of a error -
i trying update element in array, , information, whatever reason, doesn't seem getting through. here javascript sends data:
var xmlhttp = new xmlhttprequest(); if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { // nothing } } xmlhttp.open("get","updategame.php?q=" + str + "&e=" + recordtext,true); xmlhttp.send();
where recordtext
declared elsewhere.
and php recieves q
, 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 pins=$q"; $result = $conn->query($sql) or die($conn->error); $conn->close(); ?>
i have checked, , no information being updated.
i fixed ajax file, here , works fine, modified php file return data testing purposes. create 2 text files given names, copy-paste next codes , run browser :
eli_davies_1.php
<html> <head> <script type="text/javascript"> function ajax_send ( ) { 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 send. var ee = document.getelementbyid( "txt_e" ).value; // data 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 . "< , >" . $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 pins=$q"; $result = $conn->query($sql) or die($conn->error); $conn->close(); */ ?>
Comments
Post a Comment