Page 1 of 1
assign a variable to a select option**Fixed**
Posted: Mon Sep 20, 2010 9:44 am
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?
Re: assign a variable to a select option
Posted: Mon Sep 20, 2010 6:37 pm
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>
Re: assign a variable to a select option
Posted: Tue Sep 21, 2010 8:29 am
by Obadiah
bump
Re: assign a variable to a select option
Posted: Tue Sep 21, 2010 9:52 am
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!