-_- How to get the values of a dropdown box or radio button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidprogramer
Forum Commoner
Posts: 64
Joined: Mon Nov 28, 2005 6:11 pm

-_- How to get the values of a dropdown box or radio button

Post by davidprogramer »

Well, I have looked all over. I figured out how to pass text box values, but nothing on passing radio buttons or drop down boxes, or even check boxes. Here is some code to work with if you need:

Code: Select all

<?
function show_form($clan_name="",$clan_tag="", $viewemail="") {  ?>

<form action="modules.php?name=League&file=create_clan" method="post">
	First Name:
		<input type=text name=clan_name value="<?echo $clan_name?>"><br>
	Last Name:
		<input type=text name=clan_tag value="<?echo $clan_tag?>"><br>
	E-Mail:
		<select id="ViewE-Mail" name="viewemail">
			<option value="<?$email_Yes?>">Yes</option>
			<option value="<?email_No?>">No</option>
		</select><br>
	<input type=submit>
</form>

<? } 

if(!isset($clan_name)) {
        show_form();
}
else {
   if(empty($clan_name) or empty($clan_tag)) {
        echo "You did not fill in all required fields, please try again<p>"; 
        show_form($clan_name, $clan_tag, $viewemail);
   }
   else {
        echo "Thank you, $clan_name was successfully created!<br>";
// Test if the email worked here, but what variable do I use???
   }
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Your script requires Register Globals to be on, as I just mentioned in another thread you should have it turned off (if it isn't already). I suspect it is already turned off since your script isn't working. Please read Register Globals

Code: Select all

?>

            <option value="<?$email_Yes?>">Yes</option>
            <option value="<?email_No?>">No</option>
Where are these variables supposed to be set?
davidprogramer
Forum Commoner
Posts: 64
Joined: Mon Nov 28, 2005 6:11 pm

Post by davidprogramer »

i don't understand quite clearly. I am really new to php forms. I know that the clan name and tag work fine, but the drop down doesnt, do each of the drop down options have to be set?

By the way, I made a mistake, the code should be as follows:

Code: Select all

E-Mail:
		<select id="ViewE-Mail" name="viewemail">
			<option value="<?echo $email_Yes?>">Yes</option>
			<option value="<?echo email_No?>">No</option>
		</select><br>
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Re: -_- How to get the values of a dropdown box or radio but

Post by harrisonad »

I found these part erroneus:
davidprogramer wrote:

Code: Select all

<select id="ViewE-Mail" name="viewemail">
<option value="<?$email_Yes?>">Yes</option>
<option value="<?email_No?>">No</option>
</select><br>
the <?$email_Yes?> must be <?=$email_Yes?>
davidprogramer
Forum Commoner
Posts: 64
Joined: Mon Nov 28, 2005 6:11 pm

Post by davidprogramer »

Why??? These work~~

Code: Select all

First Name: 
        <input type=text name=clan_name value="<?echo $clan_name?>"><br> 
    Last Name: 
        <input type=text name=clan_tag value="<?echo $clan_tag?>"><br>
Doesn't make sense. All I am trying to do is gather the results from the dropdown box. Not both, just the one that is selected.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Either way it works. I just simply don't understand what your trying to do, and where are $email_YES and $email_NO supposed to be coming from?
Perhaps try and explain a bit clearer.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Since the options for "View Email" is either "Yes" and "No", why not just set the value for option to fixed value

Code: Select all

<select id="ViewE-Mail" name="viewemail"> 
<option value=1>Yes</option> 
<option value=0>No</option> 
</select>
This way, you would get a boolean value (1=true,0=false)

The second alternative is to make a checkbox instead of select box

Code: Select all

<input type=checkbox name='viewemail' value='1'>
however, you need to check for it's existence upon submission (since, unckecked checkboxes are not submitted) for you to know if it is yes. such as

Code: Select all

$view_email = isset($_POST['viewemail']); // true or false value
The third option is to make two radio boxes

Code: Select all

View Email?<br>
<input type=radio name=viewemail value=1 checked>Yes &nbsp;
<input type=radio name=viewemail value=0>No
:roll: wish I am still with you.
davidprogramer
Forum Commoner
Posts: 64
Joined: Mon Nov 28, 2005 6:11 pm

Post by davidprogramer »

harrison you got me closer. See how you made the selection a boolean, so it can only be 1 or 0. I am trying to pass that 1 or 0 to the script that outputs it. Right now, the script has the potential to create all fields with the textbox values.

What I am saying is. The fields with textboxes have only one value. The value that is given by the user. When the user clicks submit, it gets those values and echo's them. I need to figure out how to echo the value of viewemail (its either a 0 or a 1 in harrisons case). See what I am saying?
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

Code: Select all

View Email?<br>
<input type=radio name=viewemail value=1 <?php if($_POST['viewemail'] == 1)echo "checked";?>>Yes &nbsp;
<input type=radio name=viewemail value=0<?php if($_POST['viewemail'] == 0)echo "checked";?> >No
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

:lol: relax bro, everything can be solved....

If you want to print the word "Yes" or "No" then just make a condition for it

Code: Select all

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$viewemail = $_POST['viewemail'];

echo "
Firstname: $firstname <br>
Lastname: $lastname <br>
View Email: ";
if ($view_email) {
    echo "yes";
} else {
    echo "no";
}
:roll:
davidprogramer
Forum Commoner
Posts: 64
Joined: Mon Nov 28, 2005 6:11 pm

Post by davidprogramer »

trukfixer nailed it. :lol: Sorry for getting like that. Someone I know on phpdn (some of you may know him) teased me with alcohol so I had to have some myself....and guess what....I get jittery and paranoid easily lol. Thanks~
Post Reply