samoht Posted March 3, 2009 Share Posted March 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/147738-solved-switch-case-with-a-get-value/ Share on other sites More sharing options...
HuggieBear Posted March 3, 2009 Share Posted March 3, 2009 Yes, that's correct. You can use either $_GET['var_name'] to get a single value, or use $_SERVER['QUERY_STRING'] and parse it yourself. Quote Link to comment https://forums.phpfreaks.com/topic/147738-solved-switch-case-with-a-get-value/#findComment-775520 Share on other sites More sharing options...
samoht Posted March 3, 2009 Author Share Posted March 3, 2009 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&value2=coolguy@gmail.com 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)? Quote Link to comment https://forums.phpfreaks.com/topic/147738-solved-switch-case-with-a-get-value/#findComment-775533 Share on other sites More sharing options...
premiso Posted March 3, 2009 Share Posted March 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147738-solved-switch-case-with-a-get-value/#findComment-775543 Share on other sites More sharing options...
samoht Posted March 3, 2009 Author Share Posted March 3, 2009 OK, So since the $_GET is an array can I use something like: switch ($_GET[3]) { case 'username': //username code; break; case 'email': //email code; break; } Quote Link to comment https://forums.phpfreaks.com/topic/147738-solved-switch-case-with-a-get-value/#findComment-775552 Share on other sites More sharing options...
HuggieBear Posted March 3, 2009 Share Posted March 3, 2009 <?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 } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/147738-solved-switch-case-with-a-get-value/#findComment-775555 Share on other sites More sharing options...
samoht Posted March 3, 2009 Author Share Posted March 3, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/147738-solved-switch-case-with-a-get-value/#findComment-775571 Share on other sites More sharing options...
trq Posted March 3, 2009 Share Posted March 3, 2009 Use the comparison operator in your if's expressions instead of the assignment operator. eg; if ($key = 'username'){ should be... if ($key == 'username'){ Quote Link to comment https://forums.phpfreaks.com/topic/147738-solved-switch-case-with-a-get-value/#findComment-775574 Share on other sites More sharing options...
HuggieBear Posted March 3, 2009 Share Posted March 3, 2009 Use the comparison operator in your if's expressions instead of the assignment operator My bad. Thanks Thorpe. Quote Link to comment https://forums.phpfreaks.com/topic/147738-solved-switch-case-with-a-get-value/#findComment-775578 Share on other sites More sharing options...
samoht Posted March 3, 2009 Author Share Posted March 3, 2009 Thanks every one! Quote Link to comment https://forums.phpfreaks.com/topic/147738-solved-switch-case-with-a-get-value/#findComment-775582 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.