4 Replies - 5779 Views - Last Post: 30 July 2012 - 10:50 AM

#1 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

onselect type event

Posted 29 July 2012 - 12:02 PM

Hi guys. I will try my best to explain this problem. I basically have both php and javascript validation on my site. On some fields, they dont validate the same (they should do). One field in my form shown below
<input type="text" placeholder="Game" class="game_selector" name="game" autocomplete="off"/>

Basically calls a php case called game_selector. This php case makes a call to my database, pulls some data, and then displays it back to the user. The user then has to select one of the bits of data they are provided with. With this javascript validation, I am displaying errors in real time. At the moment, for the above field, I only have
$("[name='game']").blur(function(){
			var to = $("input[name='game']").val();
		
			if(to != 0)
			{
				$("#wGame").css({
					"background-image": "url('http://www.example.com/tick.png')"
				});			
			} else {
				$("#wGame").css({
					"background-image": "none"
				});			
			}
		
		});

So it only makes sure the input isnt 0. Is there anyway I may be able to get it to check that something from the displayed results is selected? The php code puts the results from the database call into an array
array_push($game, $thisgame);


I am not to sure if I can do anything with this. I cant use the error handling from the php because this isnt executed until the submit button is pressed.

Anyways, I am pretty lost as to what my options are, so any advise appreciated.

Cheers

This post has been edited by Dormilich: 30 July 2012 - 01:08 AM
Reason for edit:: I doubt you own the test.com domain


Is This A Good Question/Topic? 0
  • +

Replies To: onselect type event

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: onselect type event

Posted 30 July 2012 - 01:11 AM

View Postnick2price, on 29 July 2012 - 09:02 PM, said:

So it only makes sure the input isnt 0. Is there anyway I may be able to get it to check that something from the displayed results is selected? The php code puts the results from the database call into an array

if JS has access to that array/object it can validate according to that, otherwise not. though according to your description that array is only available after the form submit ...


View Postnick2price, on 29 July 2012 - 09:02 PM, said:

I cant use the error handling from the php because this isnt executed until the submit button is pressed.

that would be possible through AJAX (where you do query the server without making a form submit).
Was This Post Helpful? 1
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: onselect type event

Posted 30 July 2012 - 04:42 AM

See, in the form, that field makes a call to the php case game_selector which displays a list of games to the user in real time. This case is (removed real query)
case "game_selector":
	
		// set headers for noncache json
	  	 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
                	 header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
               	 header("Cache-Control: no-cache, must-revalidate" ); 
               	 header("Pragma: no-cache" );
		 header("Content-type: text/x-json");
	
		// get query
		$q = mysql_real_escape_string($_GET['term']);
		
		// search database
		$query = mysql_query(query);

		$games = array();
		while($row = mysql_fetch_assoc($query)){
			
			// build label (advanced syling)
			$label = "<div class=\"code\">".$row['code']."</div>";
			$label .= "<div class=\"gamename\">".$row['name']."</div>";
			if($row['state']){ $label .= $row['state'].", "; }
			$label .= "<div class=\"clear\"></div>";
	
			// append label & value
			$thisgame['label'] = $label;
			$thisgame['value'] = $row['code']." - ".$row['name'];
			
			// append to games array
			array_push($games, $thisgame);
		}
		// return games
		echo json_encode($games);
	break;



So I am not sure if I can use anything above in my javascript code to check that something has been selected? I could probably check that the value in the field matches a value from the above php array, but how can I use a php array in my javascript code? Also, would this be a slow way of doing it?

Cheers
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: onselect type event

Posted 30 July 2012 - 06:20 AM

View Postnick2price, on 30 July 2012 - 01:42 PM, said:

See, in the form, that field makes a call to the php case game_selector which displays a list of games to the user in real time.

er, no? All I see is a HTML/CSS class named "game_selector" which itself does nothing with the server. and even on form submit all you have from that field is its name ("game") and its value.

at least with the code given here.

If you have indeed a real time game list, it must be done through AJAX.
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: onselect type event

Posted 30 July 2012 - 10:50 AM

Sorry, my mistake. On that page, some javascript/ajax code is executed (can never tell the difference)
$("input.game_selector").autocomplete({
		source: 'http://www.test.com/test.php?function=game_selector',
		minLength: 2
});


And it is the above which calls the php I showed. The only solution that I can think of, which may not even be possible, is to create another php case which takes the form input and compares it to the values in the array. I may then be able to call this somehow in my ajax code. I wouldnt think this would be very efficient though. In the ajax code I showed in a previous post, I grab the value from the field. I was hoping that I could somehow directly compare this to the values stored in the array when the above case is initialise.

Would something like this be possible?

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1