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
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&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)?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

 

Link to comment
Share on other sites

<?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
   }
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.