Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't mix php and javascript like that.. php is a server-side language, while javascript is client-side; php renders the page before the user sees it, while javascript modifies the page without refreshing. Any php values in your js will get rendered and output before the js even executes.</p> <p>In order to do what you need, you need to use ajax, which is asynchronous javascript and xml, a method of client-server communication that allows for what you want to happen.</p> <p>To do this, I would suggest <a href="http://jquery.com/" rel="nofollow">jQuery</a>, a javascript library which makes such requests very simple. As an example of how you would make such a request in jquery.... </p> <p>The <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow"><code>jQuery.ajax()</code></a> method:</p> <pre><code>$.ajax({ url: "validate.php", type: "POST", data: "username=" + username, sucesss: function(data) { if (data == 1) $("#ajax_div").html("Username is taken, choose another!"); } else { $("#ajax_div").html("Username is free :)"); } } }); </code></pre> <p>That would be how to do it in ajax, while your php file would either return a 1 or a 0 depending on the result of an sql query comparing usernames in the database.</p> <p>To do this without jquery, it would be something like this:</p> <pre><code>function checkUsername() { var xmlhttp; 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) { if (xmlhttp.responseText == 1) { document.getElementById('ajax_div').innerHTML = "Username is taken, please choose another!"; } else { document.getElementById('ajax_div').innerHTML = "Username is free :)"; } } } xmlhttp.open("POST","validate.php",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("username=" + username); } } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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