Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    PHP: Get input box value

    Hi I am trying to see if a username is already taken in my table.

    The following code calls the Java function when the user clicks out of the textbox.. It works great but I have to manually enter a username where it says 'HERE!?'

    I would like it to use the value of the textbox 'text'.

    How would I go about doing this?

    Code:
    <script type="text/javascript">
    function test(){
    document.getElementById("php_code").innerHTML="<?php 
    
    
    include 'config.inc';
    
    
    
    $dbunames = mysql_query("SELECT * FROM users WHERE username='HERE!?'");
    
    if(mysql_num_rows($dbunames) > 0 ) {
    echo "Already taken";
    }
    else
    {
    echo "available!";
    }
    
    
     ?>";
    }
    
    </script>
    
    
    <input name="text" onchange="test(); return false;" id="text" length="20">
    <input name="text2" id="text2">
    <span id="php_code"> </span>
    Thanks

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,436
    Thanks
    13
    Thanked 360 Times in 356 Posts
    completely wrong approach. PHP is executed on the server before the HTML/JS is displayed in the browser.

    hence you cannot mix JS and PHP like that.

    what you can do is ask a PHP script via AJAX, whether a certain username exists. and of course the DB table needs to be unique on the username field.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •