assign a variable to a select option**Fixed**

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
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

assign a variable to a select option**Fixed**

Post by Obadiah »

what I want to do is if the user selects yes on a dropdown have the value of lets say variable x = 10, so that if there were 10 selects with yes or no if there are 9 that are selected yes it says x=90...how do i go about coding that...all i have right now is the declaration of 2 variables one is the x and the other is the dropdown (dd1)

Code: Select all

$dd1= "<select><option>select one</option></select><select><option>No</option></select><select><option>No</option></select>";
$x=0;

if ($dd1 == "yes"){
$x +=10;
}
It would be my guess that it is not working because the boolean yes/no is not directly defined even though the dropdown is!...so how could i go about doing this?
Last edited by Obadiah on Wed Sep 22, 2010 5:07 am, edited 1 time in total.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Re: assign a variable to a select option

Post by Obadiah »

here is what i have tried....it doesnot add the numbers at all...however it is an example of what i was trying to accomplish with the dropdowns

Code: Select all

<?php
$num1 = 0;
$boolpool = $bool1 + $bool2;
$bool = $_POST['bool'];
$text1 = "<input type=\"text\" name=\"bool\">";

if ($bool == "yes"){
	$num = $num += 10;
	echo $num;
}
else if ($bool == "no"){
	$num = $num -= 10;
	echo $num;
}
else if ($bool == "na"){
	$num +=0;
}

$num2 = 0;
$bool2 = $_POST['bool2'];
$text2 = "<input type=\"text\" name=\"bool2\">";

if ($bool2 == "yes"){
	$num2 = $num2 += 10;
	echo $num2;
}
else if ($bool2 == "no"){
	$num2 = $num2 -= 10;
	echo $num2;
}
else if ($bool2 == "na"){
	$num2 +=0;
}

echo $boolpool;

?>
<html>
<head>
<title></title>
</head>
<body>
<form action="try.php" method="post"> 
<?php echo $text1; echo $text2; ?>
<input type="submit">
</form>
</body>
</html>
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Re: assign a variable to a select option

Post by Obadiah »

bump
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Re: assign a variable to a select option

Post by Obadiah »

Code: Select all

$num1 = 0;
$bool = $_POST['bool'];
$text1 = "<input type=\"text\" name=\"bool\">";

if ($bool == "yes"){
        $num = $num + 10;
        
}
else if ($bool == "no"){
        $num = $num + 0;
        
}
else if ($bool == "na"){
        $num +=0;
}

$num2 = 0;
$bool2 = $_POST['bool2'];
$text2 = "<input type=\"text\" name=\"bool2\">";

if ($bool2 == "yes"){
        $num2 = $num2 + 10;
        
}
else if ($bool2 == "no"){
        $num2 = $num2 + 0;
        
}
else if ($bool2 == "na"){
        $num2 +=0;
}

$boolpool = $num + $num2;
echo $boolpool;
fixes what i was trying to do....i kinda figured it out....now i want to place the inputs to dropdowns...i will post when i figure it out...but if someone feels like a good samaritan feel free to beat me to the punch!
Post Reply