I want the $delivery in the top input to change value depending on the value on another variable I'm using.
I'm just not sure how to place another if inside the current if and how to match up the value="0.00" so it all works together
<form action="" method="post" onclick="this.submit()">
<input <?php if ($delivery=='0.00'){ echo 'checked="checked"';} ?> type="radio" value="0.00" name="delivery"> Economy - up to 4 working days<br>
<input <?php if ($delivery=='4.99'){ echo 'checked="checked"';} ?> type="radio" value="4.99" name="delivery"> Express - next day <br>
<input <?php if ($delivery=='9.99'){ echo 'checked="checked"';} ?> type="radio" value="9.99" name="delivery"> Saturday<br>
</form>
Thanks.
What i ended up doing was this:
if ($pricetotal <= 50.00) {
$amount1 = 3.99;
}
elseif ($pricetotal >= 50.01) {
$amount1 = 0.00;
}
Then in my form changing it to this:
<form action="" method="post" onclick="this.submit()">
<input <?php if ($delivery<='3.99'){ echo 'checked="checked"';} ?> type="radio" value=" <?php echo htmlspecialchars($amount1); ?>" name="delivery"> Economy - up to 4 working days<br>
<input <?php if ($delivery=='4.99'){ echo 'checked="checked"';} ?> type="radio" value="4.99" name="delivery"> Express - next day <br>
<input <?php if ($delivery=='9.99'){ echo 'checked="checked"';} ?> type="radio" value="9.99" name="delivery"> Saturday<br>
I might not have explained exactly what i needed as i thought there would be a simple fix, but i wanted the top inputs value do change depending on $pricetotal and i managed to get it working.
Thanks for the help.
I would do something like this:
$deliveryEconomy = '';
$deliveryExpress = '';
$deliverySaturday = '';
switch($delivery)
{
case 0.00:
$deliveryEconomy = 'checked="checked"';
break;
case 4.99:
$deliveryExpress = 'checked="checked"';
break;
case 9.99:
$deliverySaturday = 'checked="checked"';
break;
}
<form action="" method="post" onclick="this.submit()">
<?php
echo "<input $deliveryEcononomy type='radio' value='0.00' name='delivery'> Economy - up to 4 working days<br>";
echo "<input $deliveryExpress type='radio' value='4.99' name='delivery'> Express - next day <br>";
echo "<input $deliverySaturday type='radio' value='9.99' name='delivery'> Saturday<br>";
?>
</form>
Normally you would run logic before this, but if you are looking for a quick and dirty mini-if statement, something like this might work for you:
if ($delivery==($boolVar ? '0.00' : 'something else')){ echo 'checked="checked"';}
These statements have the same result:
if($condition) {
$var = $statement_1;
} else {
$var = $statement_2;
}
$var = $condition ? $statement_1 : $statement_2;
Don't overuse it however, otherwise you may be tempted to try:
$var = $condition_1 ? $statement_1 : ($condition_2 ? $statement_2 : $statement_3);
Messy.
Related
I've got a page with checkboxes generated with the database. When we press the checkbox and submit it, it is working fine and it is updating in the database. But when I try to uncheck "1" checkbox it is checking out all checkboxes which are selected.
Query:
if(isset($_POST['submit'])){
foreach ($_POST['untrain'] as $room_id => $user_id) {
// This query needs protection from SQL Injection!
$user_id;
$untrainQuery = "UPDATE room_users SET trained = '1' WHERE user_id = $user_id AND room_id = $room_id";
$db->update($untrainQuery);
}
}
if(isset($_POST['submit'])){
foreach ($_POST['amk'] as $room_id => $user_id) {
// This query needs protection from SQL Injection!
$user_id;
$untrainedQuery = "UPDATE room_users SET trained = '0' WHERE user_id = $user_id AND room_id = $room_id";
$db->update($untrainedQuery);
}
}
Checkboxes:
<?php
if($room->trained == 1)
{ ?>
<input type='hidden' value="<?php echo $room->user_id; ?>" name="amk[<?php echo $room->room_id; ?>]">
<input type='checkbox' value="<?php echo $room->user_id; ?>" name="trained[<?php echo $room->room_id; ?>]" checked>
<?php echo "Y"; }
else{ ?>
<input type='checkbox' value="<?php echo $room->user_id; ?>" name="untrain[<?php echo $room->room_id; ?>]">
<?php echo "N";
}?>
</td>
<Td><?php
if($room->active == 1) {
?> <input type='checkbox' name="<?php echo $room->room_id; ?>" checked>
<?php echo "Active"; }
else { ?>
<input type='checkbox' name="<?php echo $room->room_id; ?>"
<?php echo "Inactive"; } ?>
I used the trick with the "hidden" input before the checkbox, but the only problem is that it is not working. When I click on it, it resets all checkboxes to 0.
I think you are missing how the combo checkbox + hidden input does work.
So here you go freely inspired by this answer:
<input id="foo" name="foo" type="checkbox" value="1" />
<input name="foo" type="hidden" value="0" />
Looks like you do know, if you use the trick, that, if the checkbox is unchecked, it will not be present in the post. So to trick the form, we will always add an hidden field. And if the checkbox is checked, then the fact that it will be included in the post is going to override the value of the hidden input.
So for your specific problem :
<td>
<input type="checkbox" value="1" name="trained[<?php echo $room->room_id; ?>_<?php echo $room->user_id; ?>]" <?php echo ($room->trained == 1) ? ' checked' : '' ?> /> Trained
<input type="hidden" value="0" name="trained[<?php echo $room->room_id; ?>_<?php echo $room->user_id; ?>]"/>
</td>
Please note the use of the ternary operator on this part of the code <?php echo ($room->trained == 1) ? ' checked' : '' ?> which I may use a lot when writing html template.
Please also note the trick on the name trained[<?php echo $room->room_id; ?>_<?php echo $room->user_id; ?>] which is needed because we cannot set the user_id as value of the input.
Then for the processing part :
if ( isset ( $_POST['submit'] ) ) {
foreach ( $_POST['trained'] as $ids => $value ) {
// This query needs protection from SQL Injection!
// ^ good point, on which I would suggest you using PDO and prepared statement :)
list($room_id,$user_id) = explode('_',$ids);
// ^ now need to explode the name on the underscore to get both user_id and room_id cf the trick above
$untrainQuery = "UPDATE room_users SET trained = '$value' WHERE user_id = $user_id AND room_id = $room_id";
$db->update ( $untrainQuery );
}
}
Wash, rinse, repeat for every checkbox you need and you should be good to go.
in database values are stored as "A,B,C,D,E,F" etc.
now i have to fetch those data in the form for update.
i have to check the checkbox if it matches the value with database value.
i am doing this kind of code(which i know is wrong)
<input type="checkbox" name="time[]" value="free" <?php if(!strpos($row['best'], 'free')=="false") { echo "checked='checked'";} ?> />Free
<input type="checkbox" name="time[]" value="Open" <?php if(!strpos($row['best'], 'Open')=="false") { echo "checked='checked'"; }?>/>Open Source
<input type="checkbox" name="time[]" value="portable" <?php if(!strpos($row['best'], 'portable')=="false") { echo "checked='checked'"; } ?> />Portable
<input type="checkbox" name="time[]" value="support" <?php if(!strpos($row['best'], 'support')=="false") {
echo "checked='checked'"; }?> />Support
When strpos is 0 (first character), then it is equal to false if you use double = (==). You need to use ===false. Besides it should be false, not "false".
You can do in this way:
suppose you have four static values :
<
?php
$time = ['free', 'open' , 'portable', 'support']; // your checkboxes
$fromDb = explode(',', $row['best']);
foreach($time as $t):
?>
<input type="checkbox" name="time[]" value="<?php echo $t;?>" <?php if(in_array($t, $fromDb) { echo "checked='checked'";} ?> /><?php echo ucfirst($t);?>
<?php endforeach;?>
I'm drawing data from a MySQL database that dynamically places a question with 4-5 radio button choices for the answer. These radio buttons all belong to the same group, $quest_name. The first pass of the while statement will create 4 radio buttons belonging to radio group "question_1".
It creates 30-40 of these questions on a page, each with 4 radio buttons. I want the user to fill in all there answers and the page to post back to itself with the users answers still selected and then display if they were correct or not (functionality I still have to add).
I'm trying to follow http://www.w3schools.com/php/php_form_complete.asp as an example, but use a dynamically created radio button name instead.
This is what I have thus far:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$quest_num = $row["id"];
$question = $row["question"];
$option_1 = $row["option_1"];
$option_2 = $row["option_2"];
$option_3 = $row["option_3"];
$option_4 = $row["option_4"];
$option_5 = $row["option_5"];
$answer = $row["answer"];
$quest_name = "question_" . $row["id"];
echo "(" . $quest_num . ") " . $question . "<br>";
?>
<label>
<input type="radio" name=<?php echo $quest_name ?>
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
value=<?php echo $option_1 ?>><?php echo $option_1 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_2 ?>><?php echo $option_2 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_3 ?>><?php echo $option_3 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_4 ?>><?php echo $option_4 ?>
</label>
<br>
<br>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
<input type="submit">
</form>
The part causing me grief so far is:
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
I have also tried:
<?php if (isset($quest_name) && $quest_name == $option_1) echo "checked"; ?>
and:
<?php echo (isset($quest_name) && $quest_name == $option_1) ? "checked" : ""; ?>
How do you post back to the same page what they've selected? Like in this case I'm trying to say if "question_1" is set and "question_1" is equal to "converter" (the first radio button option) then have it checked when submit button is clicked.
I'm not that good at web development, but I'm trying to create a website to help my fellow electrical technician classmates.
Thanks for any help.
EDIT :
Using this line of code fixed the issue:
<?php if(isset($_POST[$quest_name]) && $_POST[$quest_name]==$option_1) { echo 'checked="checked"'; } ?>
What you need is called Radio Group. In HTML layer it is created with same name for all and different values for each like this:
<p>
<label>
<input type="radio" name="RadioGroup1" value="Value1" id="RadioGroup1_0">
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="Value2" id="RadioGroup1_1">
Radio</label>
<br>
</p>
And when you want to get the user input in php layer you go like this:
<?php
//check if Radio Group 1 is set
if(isset($_POST['RadioGroup1'])) {
// print the value of Radio Group 1 choice
echo $_POST['RadioGroup1'];
}
?>
When you want to create a Selected Radio in HTML layer you go like this:
<input name="RadioGroup1" type="radio" id="RadioGroup1_1" value="radio" checked="checked">
So you have to check if user inputs the value of which radio like this:
<label>
<input type="radio" name="RadioGroup1" value="value1" id="RadioGroup1_0" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value1') { echo ' checked="checked"'; } ?>>
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="value2" id="RadioGroup1_1" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value2') { echo ' checked="checked"'; } ?> >
Radio</label>
You could use the following:
<input type="radio" name="<?php echo $quest_name ?>" value="<?php echo $option_1 ?>"
<?php if (isset($quest_name) && ($quest_name == $option_1)) echo "checked"; ?> />
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code for dynamic checked radio box:
PHP:
if ($author === 0) {$checked = 'checked';} else {$checked == '';}
if ($author === 1) {$checked = 'checked';} else {$checked == '';}
if ($author === 2) {$checked = 'checked';} else {$checked == '';}
HTML:
<input type="radio" name="test" <?PHP echo $checked; ?> />
<input type="radio" name="test" <?PHP echo $checked; ?> />
<input type="radio" name="test" <?PHP echo $checked; ?> />
This way is true? What’s is a better/optimized Way?
Can I write any PHP function or class for save code and checked any radio box?
It can save you some lines and unnecessary extra variables if you look forward using the shorten if statement form. Example with the first input :
<input type="radio" name="test" <?php echo $author === 0 ? 'checked' : '' ?> />
Ideally you want to check a button if the author value relates to that input, for example:
<input type="radio" name="test" value="0" <?php echo ($author == 0 ? 'checked="checked"' : ''); ?> />
<input type="radio" name="test" value="1" <?php echo ($author == 1 ? 'checked="checked"' : ''); ?> />
<input type="radio" name="test" value="2" <?php echo ($author == 2 ? 'checked="checked"' : ''); ?> />
Your currently checking all if the value is any.
The HTML markup is wrong. With that HTML the user is able to select all of those radios. To make it so that the user can only select one of the radio buttons out of that group of options, change the names to all match, like this:
<input type="radio" name="test1" <?PHP echo $checked; ?> />
<input type="radio" name="test1" <?PHP echo $checked; ?> />
<input type="radio" name="test1" <?PHP echo $checked; ?> />
Something you can do:
<?php
$checked = ($author >= 0 && $author <= 2 ? 'checked' : '');
?>
HTML
<input type="radio" name="test" <?php echo $checked; ?> />
<input type="radio" name="test1" <?php echo $checked; ?> />
<input type="radio" name="test2" <?php echo $checked; ?> />
<?php
switch($author) {
case 0:
case 1:
case 2:
$checked = "checked";
break;
default:
$checked = '';
}
I've already spent hours over this problem , and searched for answer online, but found nothing that helped me.
So, I am making a filter for a hotel website, and one of the criteria for hotel search is the district where the hotel is located. I use GET method:
<?php if (isset($_GET['District1'])){
$district1= ($_GET["District1"]);
}else ($district1=0);
if (isset($_GET['District2'])){
$district2= ($_GET["District2"]);
}else ($district2=0);
if (isset($_GET['District3'])){
$district3= ($_GET["District3"]);
}else ($district3=0);
if (isset($_GET['District4'])){
$district4= ($_GET["District4"]);
}else ($district4=0);
?>
where $district1, $district2, $district3, $district4 are 4 different districts of the city.
What I am trying to do is to build the district variables by using for loop (the digit at the end of variable name would be ascending by 1 : $district1..2..3..4),
then place these district variables in an if clause which is inside while loop as follows:
<?php
$districts_filter_query="SELECT * FROM districts_table ";
$districts_filter_result = mysql_query($districts_filter_query, $connect);
$districts_filter_row = mysql_fetch_array($districts_filter_result);
for($count = 1; $count <= 4 ; $count++){
do {
$district_name = $districts_filter_row['district_name'];
$district_value = $districts_filter_row['district_value'];
echo '
<label for="group13"> <p '; ?> <?php
if ( eval(' return $district'.$count.';') !=0){echo 'class="greenBG"';}
else {echo ' class="checkbox" ';}?> <?php echo ' >
<input type="checkbox" name="District'.$count.'" id="group13" value="'.$district_value.'"
onClick="this.form.submit();" ';?>
<?php if ( eval (' return $district'.$count.';') !=0){echo ' checked ';};?><?php echo '/> '.$district_name.'
</p>
</label> ';
}
while($districts_filter_row = mysql_fetch_array ($districts_filter_result));
}
?>
The problem is here, I guess: eval(' return $district'.$count.';'), by this I try to construct district variable names. But it does not work.
Everything works perfectly if I use this code:
<label for="group1"> <p <?php
if ($district1!=0){echo 'class="greenBG"';}
else echo 'class="checkbox"';?>>
<input type="checkbox" name="District1" id="group1" value='1013' onClick="this.form.submit();"
<?php if ($district1!=0){echo 'checked ';};?>/> district1
</p>
</label>
<label for="group2"> <p <?php
if ($district2!=0){echo 'class="greenBG"';}
else echo 'class="checkbox"';?>>
<input type="checkbox" name="District2" id="group2" value='1014' onClick="this.form.submit();"
<?php if ($district2!=0){echo 'checked ';};?>/> district2
</p>
</label>
<label for="group3"> <p <?php
if ($district3!=0){echo 'class="greenBG"';}
else echo 'class="checkbox"';?>>
<input type="checkbox" name="District3" id="group3" value='1015' onClick="this.form.submit();"
<?php if ($district3!=0){echo 'checked ';};?>/> district3
</p>
</label>
<label for="group4"> <p <?php
if ($district4!=0){echo 'class="greenBG"';}
else echo 'class="checkbox"';?>>
<input type="checkbox" name="District4" id="group4" value='1016' onClick="this.form.submit();"
<?php if ($district4!=0){echo 'checked ';};?>/> district4
</p>
</label>
But this is not suitable for me because there are more than 30 districts, and it would take a lot of time and effort to make any minor change to code.
I would be greatful if you help me solve this problem and save my time. Thanks, hope my question is clear enough.
Use http://php.net/manual/en/language.variables.variable.php instead of some ugly eval(). (For example ${"district$count"})
$count = 0;
do {
$count++;
$district_name = $districts_filter_row['district_name'];
$district_value = $districts_filter_row['district_value'];
?>
<label for="group<?php echo $count; ?>">
<p <?php
if (${"district$count"} != 0) echo 'class="greenBG"';
else echo 'class="checkbox"'; ?>>
<input type="checkbox" name="District<?php echo $count; ?>" id="group<?php echo $count; ?>" value='<?php echo $district_value; ?>' onClick="this.form.submit();"
<?php if (${"district$count"} != 0) echo 'checked '; ?>/> <?php echo $district_name; ?>
</p>
</label>
<?php
}
while($districts_filter_row = mysql_fetch_array ($districts_filter_result));
And then delete this for loop around it.
that's a little convoluted as an approach, but a quick solution to get rid of eval is to use variable variables:
<?php
$district_name = "district$count";
if ($$district_name != 0) { ... }
?>
... though a better solution would be to just use an array and loop through it.