Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-15-2010, 02:28 PM   PM User | #1
supersav144
New Coder

 
Join Date: Dec 2010
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
supersav144 is an unknown quantity at this point
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>
supersav144 is offline   Reply With Quote
Old 12-15-2010, 02:55 PM   PM User | #2
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,007
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
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
Rowsdower! is offline   Reply With Quote
Users who have thanked Rowsdower! for this post:
supersav144 (12-15-2010)
Reply

Bookmarks

Tags
array, javascript, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:48 AM.


Advertisement
Log in to turn off these ads.