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
    Dec 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Check if array contains variable

    I am currently trying to check using javascript whether a php array contains a variable, and if it does then display a message.

    Any help would be much appreciated.

    I have written the following code...



    Code:
    <?php
    //php which sets users array to the results of the sql
    $selectquery = "SELECT Username FROM User";
    $selectresult = mysql_query($selectquery);
    while ($row = mysql_fetch_array($selectresult)){
    	$users[] = $row['Username'];
    
    }
    ?>
    
    
    
    <script language="javascript" type="text/javascript">
    function verifyUsername(array_var){
    
    	var user = document.getElementById("username").value;
    	
    	for(var i=0; i<array_var.length; i++){
    		if(array_var[i] == user){
    			document.getElementById("usernameerror").textContent = "already in array";
    		}
    	}
    }
    </script>
    
    
    
    //html code for the form
    Username: <input type="text" name="username" id="username" onblur="return verifyUsername(<?php $users?>)"/>
            
    <span id="usernameerror" class="red"></span>

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    In order for javascript to check your array from the PHP code you would have to write that array to the page (as javascript).

    It would look something like this (I haven't tested this so you may need to clean up a bit):
    PHP Code:
    <?php
    //php which sets users array to the results of the sql
    $selectquery "SELECT Username FROM User";
    $selectresult mysql_query($selectquery);
    while (
    $row mysql_fetch_array($selectresult)){
        
    $users[] = $row['Username'];

    }
    ?>
    <script type="text/javascript">
    var the_list_from_php=new Array();
    <?php
    for($i=0;$i<count($users);$i++){
        
    //print out variable data to create the items in the javascript array...
        
    print "the_list_from_php[".$i."]='".$users[$i]."';\n";
    }
    ?>
    </script>
    Then you just need to check the array named "the_list_from_php" to see if the value you are testing for exists in the array.

    Let me know if you have questions.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    supersav144 (12-15-2010)


  •  

    LinkBacks (?)


    Tags for this Thread

    Posting Permissions

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