Note that there are some explanatory texts on larger screens.

plurals
  1. POAuthentication using AJAX & PHP
    primarykey
    data
    text
    <p>I make a simple program to simulate authentication using AJAX and PHP. The following is my code. It will redirect to "/?" but nothing happened. I tried to use <code>checkLogin(); return false;</code> in <code>onclick</code> event but it didn't work.</p> <p><strong>index.php</strong></p> <pre><code>&lt;script type="text/javascript" src="validation.js"&gt;&lt;/script&gt; &lt;form&gt; &lt;p&gt; &lt;label&gt;Username &lt;abbr title="Required"&gt;*&lt;/abbr&gt;&lt;/label&gt; &lt;input id="usernamelogin" type="text" value="" /&gt; &lt;/p&gt; &lt;p&gt; &lt;label&gt;Password &lt;abbr title="Required"&gt;*&lt;/abbr&gt;&lt;/label&gt; &lt;input id="passwordlogin" type="text" value="" /&gt; &lt;/p&gt; &lt;p&gt; &lt;input id="login" type="submit" value="Login" onclick="checkLogin()" /&gt; &lt;/p&gt; &lt;/form&gt; </code></pre> <p><strong>validation.js</strong></p> <pre><code>function checkLogin(){ var u = document.getElementById("usernamelogin").value; var p = document.getElementById("passwordlogin").value; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); } else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onReadystatechange = function(){ if(xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200){ if(xmlhttp.responseText == u){ alert(':)'); } else{ alert(':('); } } } xmlhttp.open("GET", "login.php?u=" + u + "&amp;p=" + p, true); xmlhttp.send(); } </code></pre> <p><strong>login.php</strong></p> <pre><code>&lt;?php $u = $_GET['u']; $p = $_GET['p']; $host = 'localhost'; $user = 'root'; $pass = ''; $db = 'db'; $con = mysql_connect($host, $user, $pass); if(!$con) die("Could not connect: " . mysql_error()); mysql_select_db($db, $con); $query = "select username from login where username = '" . $u . "' and password = '" . $p . "'"; $result = mysql_query($query); $row = mysql_fetch_array($result); echo $row['username']; mysql_close($con); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload