0

I'm developing a registration form for my site. Actually when a visitor choose an username, a php query to my MySQL DB is used to control if it's already used and if so, a javascript windowd appear. Can i use a PHP query inside Javascript for displaing a real-time notice near the form (using HTML5)?

<script>
var username = document.getElementById('username');
var userdb = <? php control_username($username); ?>

var checkUsername = function () {
    if (userdb.value == true) {
        username.setCustomValidity('Username already used');
    } else {
        username.setCustomValidity('');
    }
};

username.addEventListener('change', checkUsername, false);
</script>

and here there's the php function:

<?php function control_username($username){
$db=connessione_db();
$query = "SELECT username FROM utente WHERE username = '$username';";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_close();

if($row[0]==$username){
    return TRUE;
 }
else{
    return FALSE;
}
$query=NULL;

}

how can i do?

Greg
  • 2,163
  • 1
  • 21
  • 23
Evinrude
  • 1
  • 2

3 Answers3

0

You can use AJAX or jQuery AJAX to send a request to a php page, Check if the username exists, return the result, and display it using Javascript again.

Here is the jQuery sample:

<script>
    $.ajax({
        type: 'POST',
        url : 'checkUsername.php',
        data: {'username' : $('#username').html()},
        cache : false,
        success: function(data){
            if(data == 'exists')
                //username exists
                alert('username already exists!');
        },
        error: function(request , status , error){
            alert(request.resposeText);
        }
    });
</script>

and this should be your checkUsername.php file:

<?php
$db=connessione_db();
$query = "SELECT count(*) as count FROM utente WHERE username = '$username'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_close();

if($row[count] > 0)
    echo 'exists';
else
    echo '';
Mehrdad Hedayati
  • 1,434
  • 13
  • 14
0

PHP is run on the server, Javascript is run on the client's machine. Once your PHP script has generated a page and sent it to the user, it doesn't run any longer. Your client has no knowledge of a PHP script. It can't directly run, call, or read anything about your PHP script because it resides solely on the server (never on the client's machine). Your client's machine does, however, know about your Javscript since it has been sent along with the page. If you want to access PHP functionality from your page, you can either send a GET/POST call to the server and reload the page, or use AJAX to make the call in the background. Check out Jquery's implementation of AJAX calls, it makes using it pretty simple.

Cully
  • 6,427
  • 4
  • 36
  • 58
  • Thanks for the response, it's my first web project and it's my first project and I have not yet understood the various features, great explanation. – Evinrude May 07 '14 at 20:41
  • The simplest way to do this is, instead of validating in Javascript, just have your form submit and validate the data when it's sent to the server. If it's not valid, output an error message along with the form again. If it is valid, do whatever you want to do. – Cully May 07 '14 at 20:46
0

No you can't do it like that. PHP is serverside, Javascript clientside. The moment Javascript is executed is the code working clientside. All PHP code is fixed. Compare it to the combination of HTML and PHP in an HTML page. It is fixed there as well. Same applies to PHP in Javascript.

Here are some answers from related questions on stackoverflow: How to put php inside javascript? How to embed php in javascript?

Here is an example from ajaxref, showing the basics: http://ajaxref.com/ch3/asyncsend.html

This is another tutorial showing how an ajax call is handled: http://code.tutsplus.com/articles/how-to-make-ajax-requests-with-raw-javascript--net-4855

I advice you to first understand this process and later on start using a framework like jQuery.

Community
  • 1
  • 1
Loek Bergman
  • 2,192
  • 20
  • 18