Jump to content

[SOLVED] switch case with a GET value??


samoht

Recommended Posts

Hello,

 

I have a little php that I wrote as part of an ajax form check.

<?php
$db =& JFactory::getDBO();
$text =& JRequest:: getString('value');


$sql = "
    SELECT COUNT(*)
        FROM `#__users`
        WHERE `username` = ".$db->quote($text).";";
$db->setQuery( $sql );
echo "##@##";
if ( $database->loadResult() != 0 ) {
  echo '<span class="red">Username Already Taken</span>';
} else {
  echo '<span class="green">This Username is available</span>';
}
echo "##@##";

?>

 

the url is served up by ajax with &value='+username+' at the end of it.

This all works fine - but now I want to add more fields to check. I assume that in the ajax I will simply change the variable in the url (maybe &value2='+email+') and then when I get to my php script can I just use a switch case to check which variable is being passed?

 

hopefully that makes sense

Link to comment
https://forums.phpfreaks.com/topic/147738-solved-switch-case-with-a-get-value/
Share on other sites

But I don't just want the value - I want to know which variable has a value.

 

so if my url looked like:

index2.php?com_mycomp&formname=ajax_form&value=coolguy

 

i know I'd be looking at the username because the variable is 'value'

if my url looked like:

index2.php?com_mycomp&formname=ajax_form&[email protected]

 

I'd be looking at the email field - and I should know this because the variable is 'value2'

 

So in my php I need to check which variable is in the url

 

To clarify: I know how to get the uri  - but then what?

Also, should I name my variables something totally different rather than (value, value2, value3 etc)?

foreach ($_GET as $key => $val) {
    echo "You are working with {$key} which is {$val}.<br />";
}

 

You can use that to figure out what values are passed in/get the uri.

 

As far as value etc I would name them different. A more descriptive name makes debugging a bit easier.

<?php

// $_GET is an array that contains everything passed in via the querystring
print_r($_GET);

/*
   The above code is here for debugging purposes
   So you can see what you're being passed in each
   time.  Once you're confident, remove it.
*/

// Logic goes here.  I have an if inside the foreach, you can have a case if you prefer.
foreach ($_GET as $key => $value){
   if ($key = 'username'){
      // username code
   }
   elseif ($key = 'email'){
      // email code
   }
}
?>

by doing this I get an 'echo' for each variable:

<?php
$db =& JFactory::getDBO();
$myurl =& JRequest:: geturi();

foreach ($_GET as $key => $val) {

   if ($key = 'username'){
      // username code
	$text =& JRequest:: getString('username');
	$sql = "
		SELECT COUNT(*)
			FROM `#__users`
			WHERE `username` = ".$db->quote($text).";";
	$db->setQuery( $sql );
	echo "##@##";
	if ( $database->loadResult() != 0 ) {
	  echo '<span class="red">Username Already Taken</span>';
	} else {
	  echo '<span class="green">This Username is available</span>';
	}
	echo "##@##";
   }
   elseif ($key = 'email'){
      // email code
	$regemail =& JRequest:: getString('value2');
	$sql = "
	    SELECT COUNT(*)
	        FROM `#__users`
	        WHERE `email` = ".$db->quote($regemail).";";
	$db->setQuery( $sql );
	echo "##@##";
	if ( $database->loadResult() != 0 ) {
	  echo '<span class="red">Email Already Taken</span>';
	} else {
	  echo '<span class="green">This email is available</span>';
	}
	echo "##@##";
   }

}
?>

 

Output:

##@##This Username is available##@####@##This Username is available##@####@##This Username is available##@####@##This Username is available##@####@##This Username is available##@## 

 

how do I limit to just the one?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.