Checking radio button on submit - PHP - php

I need to check a radio button input on submit.
If none of the radio buttons are checked, $err1_diet gets set to true and the red class needs to be added.
And I also need to know which radio button was checked since this isn't the only question in the form.
if( !isset($_POST['diet']) ){
$err1_diet = true;
}elseif($_POST['diet'] == 1){
$diet = true;
}else{
$diet = false;
$yes = true;
}
<p class="<?php echo (($err1_diet == true) ? "red" : "" ); ?>">• Are you on a diet?<?php var_dump($err1_diet); ?></p>
<input type="radio" name="diet" value="1" <?php echo (($diet) ? 'checked="true"' : "" ); ?> /> Yes
<input type="radio" name="diet" value="0" <?php echo (($diet) ? '' : 'checked="true"' ); ?> /> No

Actually no ... it's checking one of the radio buttons before the form is submitted. That's the problem
You can solve your problem using $_SERVER['REQUEST_METHOD']=='POST' for example:
if( $_SERVER['REQUEST_METHOD']=='POST' and !isset($_POST['diet']) ){
$err1_diet = true;
}elseif($_POST['diet'] == 1){
$diet = true;
}else{
$diet = false;
$yes = true;
}
<p class="<?php echo (($err1_diet == true) ? "red" : "" ); ?>">• Are you on a diet?<?php var_dump($err1_diet); ?></p>
<input type="radio" name="diet" value="1" <?php echo (($diet) ? 'checked="true"' : "" ); ?> /> Yes
<input type="radio" name="diet" value="0" <?php echo (($diet) ? '' : 'checked="true"' ); ?> /> No

Related

How to show a php radio input value on a php email?

I have a php form with 4 radio inputs as below:
<?php if( $chauffeur_data['enable-paypal'] == '1' ) { ?>
<div class="radio-wrapper clearfix"><input type="radio" name="payment-method" value="paypal" <?php if( $paypal_check == '1' ) { echo 'checked="checked"'; } ?> /><label><?php esc_html_e('Pay with PayPal','chauffeur'); ?></label><img src="<?php echo plugins_url('../../assets/images/paypal.png', __FILE__); ?>"></div>
<?php } ?>
<?php if( $chauffeur_data['enable-stripe'] == '1' ) { ?>
<div class="radio-wrapper clearfix"><input type="radio" name="payment-method" value="stripe" <?php if( $stripe_check == '1' ) { echo 'checked="checked"'; } ?> /><label><?php esc_html_e('Pay with Credit Card','chauffeur'); ?></label><img src="<?php echo plugins_url('../../assets/images/stripe.png', __FILE__); ?>"></div>
<?php } ?>
<?php if( $chauffeur_data['enable-cash'] == '1' ) { ?>
<div class="radio-wrapper clearfix"><input type="radio" name="payment-method" value="cash" <?php if( $cash_check == '1' ) { echo 'checked="checked"'; } ?> /><label><?php esc_html_e('Pay with Cash to the Driver','chauffeur'); ?></label></div>
<?php } ?>
<?php if( $chauffeur_data['enable-pos'] == '1' ) { ?>
<div class="radio-wrapper clearfix"><input type="radio" name="payment-method" value="pos" <?php if( $pos_check == '1' ) { echo 'checked="checked"'; } ?> /><label><?php esc_html_e('Pay with Card on POS in the Car','chauffeur'); ?></label></div>
<?php } ?>
<?php // If all payment gateways are disabled
if( $chauffeur_data['enable-paypal'] != '1' && $chauffeur_data['enable-stripe'] != '1' && $chauffeur_data['enable-cash'] != '1' &&
$chauffeur_data['enable-pos'] != '1' ) { ?>
<input type="hidden" name="payment-method" value="cash" />
<?php } ?>
<button name="pay_now" id="pay_now" class="payment-button" type="submit">
<?php esc_html_e('Proceed To Payment','chauffeur'); ?>
</button>
<?php } else { ?>
<input type="hidden" name="payment-method" value="cash" />
<button name="pay_now" id="pay_now" class="payment-button" type="submit">
<?php esc_html_e('Proceed To Book','chauffeur'); ?>
</button>
<?php } ?>
When one of the radio buttons is checked, how can I show its value (e.g. pos) to the email I send?
The email is like below. What should be my code to show the radio button choice?
$customer_email_content .= '<li><strong>' . esc_html__('Payment Method you Selected','chauffeur') . ': </strong>' . '**payment-method-goes-here**' . '</li>'."\r\n";
After form is submitted, get the radio button value from $_POST array;
$payment_method = $_POST['payment-method'];
Use $payment_method to show the value of the radio button that is selected.
$customer_email_content .= '<li><strong>' . esc_html__('Payment Method you Selected','chauffeur') . ': </strong>' . $payment_method . '</li>'."\r\n";

PHP - Same values display on both different textboxes

So, I have made a code to edit radio buttons, drop down lists and text boxes.
The Division->(dropdown list and Other: textbox) and External->(textbox) are under the same "client_details".
This is the example of the edit form.
This is the code I made.
<?php
require("config.php");
$id = filter_input(INPUT_GET, 'id');
?>
<html>
<head>
<title> Edit a Contract </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
ID: <input type="hidden" name="id" value="<?php echo $id; ?>" />
<?php
$sql = "SELECT * FROM contracts WHERE `id` = $id";
$result = $con->query($sql);
$row = $result->fetch_assoc();
$client_type = $row['client_type'];
?>
<label for = "client1">
<input type="radio" name="client_type" id = "client1" value="Division" <?php echo ($client_type == 'Division')? "checked" : "" ?> onclick="toggleDivision()"/> Division
</label>
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
<label for ="client2">
<input type="radio" name="client_type" id = "client2" value="External" <?php echo ($client_type == 'External')? "checked" : "" ?> onclick="toggleExternal()"/> External
</label>
&nbsp
<input type="text" id="extText" name="client_details2" value="<?php echo $row['client_details']; ?>" disabled />
<br><br>
<div id="division">
Division:
<select id="mySelect" name="client_details" onclick="enableTextbox()" disabled>
<option value="Choose" <?php echo $row['client_details'] == 'Choose' ? "selected" : ""; ?> />Choose Division...</option>
<option value="Distribution" <?php echo $row['client_details'] == 'Distribution' ? "selected" : ""; ?> />Distribution</option>
<option value="Transmission" <?php echo $row['client_details'] == 'Transmission' ? "selected" : ""; ?> />Transmission</option>
<option value="Generation" <?php echo $row['client_details'] == 'Generation' ? "selected" : ""; ?> />Generation</option>
<option value="Procument" <?php echo $row['client_details'] == 'Procument' ? "selected" : ""; ?> />Procument</option>
<option value="Other" <?php echo $row['client_details'] == 'Other' ? "selected" : ""; ?> />Others</option>
</select>
<br><br>
Others:<input type="text" id="otherTxt" name="client_details1" value="<?php echo $row['client_details']; ?>" disabled />
<br>
</div>
<br>
<input type="submit" name="submit" value="Submit"/>
</form>
<script type="text/javascript">
function toggleExternal() {
document.getElementById("extText").disabled = false;
var divis_el = document.getElementById("division");
for (var i = 0; i < divis_el.children.length; i++) {
divis_el.children[i].disabled = true;
}
}
function toggleDivision() {
document.getElementById("extText").disabled = true;
var val = document.getElementById("mySelect").selectedIndex;
var divis_el = document.getElementById("division");
for (var i = 0; i < divis_el.children.length; i++) {
divis_el.children[i].disabled = false;
divis_el.children[5].disabled = true;
}
}
function enableTextbox() {
var val = document.getElementById("mySelect").selectedIndex;
if (val == 0 || val == 1 ||val == 2 ||val == 3 ||val == 4) { document.getElementById("otherTxt").disabled = true}
if (val == 5) { document.getElementById("otherTxt").disabled = false; }
}
</script>
</body>
<?php
if(isset($_POST['submit'])) {
$client_type = isset($_POST ['client_type']) ? $_POST['client_type'] :null;
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
if($client_type == 'Division'){
$client_details = isset($_POST ['client_details1']) ? $_POST['client_details1'] :null;
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null; // both variables under if statement are needed to successfully update the data avoiding blank values.
} else {
$client_details = isset($_POST ['client_details2']) ? $_POST['client_details2'] :null;
}
if($client_details == 'Other') {
$client_details = isset($_POST ['client_details1']) ? $_POST['client_details1'] :null;
}
$query = "UPDATE contracts set `client_type`=?, `client_details`=? WHERE `id`= ?";
$stmt = $con->prepare($query);
$stmt->bind_param("ssi", $client_type, $client_details, $id);
$stmt->execute();
if ($stmt->errno){
echo "FAILURE!!! " . $stmt->error;
} else {
echo "<br>Updated";
}
$stmt->close();
$con->close();
}
?>
EDIT: What I meant is this.
As you can see the example picture from the link, the "Generation" values is displayed in all the textboxes under both Division and External after I updated the data because The value was taken from the drop down list.
The "Other" textbox is under division but it will only enabled to be entered after the selecting the value "Other" from the drop down list under Division.
I only wanted the the Division values displayed in the textbox only under Division and not the under External when Division radio button is chosen. And if I choose External and input data in the textbox, I only wanted the value displayed in the textbox under External only after updated.
When I select the values and not "Other" value, the value should not displayed in the text box under Division. When I select "Other" value, then the value should be displayed in 'Other' textbox but I don't want the value to also displayed in the text box under External.
The question I'm trying ask is how to make the textbox values display values separately. I don't want the same values displaying in all the dropdown list and textboxes? How do I do that? Sorry for poor explaination as English is my second language
Well it's no wonder why both are displaying the samething, their values are both just <?php echo $row['client_details']; ?>
You already have a checking variable, just not sure why you didn't use it here.
E.g.
<?php $extText = ($client_type == 'External') ? $row['client_details'] : "" ?>
<?php $divText = ($client_type == 'Division') ? $row['client_details'] : "" ?>
<input type="text" id="extText" name="client_details2" value="<?php echo extText; ?>" disabled />
Others:<input type="text" id="otherTxt" name="client_details1" value="<?php echo $divText; ?>" disabled />

change order of list that uses pagination

I have a paginated list that displays 10 items to a page, but I want to give the option for someone to change the order of the list, for example, by gender or age.
The pagination works, and in terms of sorting the list, so far I have:
$orderby = ($_GET['orderby'] == 'gender')? $_GET['orderby'] : 'age';
if ($result = $mysqli->query("SELECT * FROM people ORDER BY $orderby"))
With 2 basic links: gender | age
echo "<a href='{$_SERVER['PHP_SELF']}?orderby=gender'>gender</a> | <a href='{$_SERVER['PHP_SELF']}?orderby=age'>age</a>";
When I click one of the links, it changes the order of the list as required, but when I go to another page of the pagination, it does not work.
Can someone please let me know of the best way to fix this. Thanks.
Try my function genPagnation(), please refer my code below:
<?
$totalPage = 23;
$currentPage = isset($_GET['p']) && is_numeric($_GET['p']) && $_GET['p'] >= 1 ? (int)$_GET['p'] : 1;
$params = array();
$devices = array();
if( isset($_POST['order']) || isset($_GET['order']) ){
$params['order'] = isset($_POST['order']) ? $_POST['order'] : $_GET['order'];
}
if( isset($_POST['device']) || isset($_GET['device']) ){
$params['device'] = isset($_POST['device']) ? $_POST['device'] : $_GET['device'];
$devices = $params['device'];
}
for( $i = 1; $i <= $totalPage; $i++ ){
if( $i == $currentPage )
print(" <b><u>{$i}</u></b> ");
else
print(" <a href='" . genPagnation($i, $params) . "'>{$i}</a> ");
}
print("<p><hr /></p>\n\n");
function genPagnation($p, $param = array(), $pname = null, $pvalue = null){
$param['p'] = $p;
if( !empty($pname) && !empty($pvalue) )
$param[ $pname ] = $pvalue;
$query = http_build_query($param);
$query = preg_replace('/%5B[0-9]+%5D/', '%5B%5D', $query);
return '?' . $query;
}
?>
<p>
Order By:
<a href='<?=genPagnation($currentPage, $params, "order", "male")?>'>Male</a> |
<a href='<?=genPagnation($currentPage, $params, "order", "female")?>'>Female</a>
</p>
<form method="POST" action="<?=$_SERVER['PHP_SELF']?>">
<p>
Filtering:
<label><input type="checkbox" name="device[]" value="iphone" <? if( !empty($devices) && in_array('iphone', $devices) ){ echo 'checked="checked"'; } ?> />iPhone</label>
<label><input type="checkbox" name="device[]" value="ipad" <? if( !empty($devices) && in_array('ipad', $devices) ){ echo 'checked="checked"'; } ?> />iPad</label>
<label><input type="checkbox" name="device[]" value="ipod" <? if( !empty($devices) && in_array('ipod', $devices) ){ echo 'checked="checked"'; } ?> />iPod</label>
<label><input type="checkbox" name="device[]" value="android" <? if( !empty($devices) && in_array('android', $devices) ){ echo 'checked="checked"'; } ?> />Android</label>
</p>
<p>
Ordering:
<label><input type="radio" name="order" value="male" <? if( isset($_REQUEST['order']) && $_REQUEST['order'] == 'male'){ echo 'checked="checked"'; } ?> />Male</label>
<label><input type="radio" name="order" value="female" <? if( isset($_REQUEST['order']) && $_REQUEST['order'] == 'female'){ echo 'checked="checked"'; } ?> />Female</label>
</p>
<p><input type="submit" value="search"></p>
</form>

Pre populated radio button in php

I have radio buttons for Yes and No and want to pre-populate them from a value in a mysql database. I am able to show the radio buttons but they always show the Yes checked even when the returned value is No. I am also not able to change the pre-populated value eg Yes to No.
<input type="radio" id="etag" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input type="radio" id="etag" name="etag" value="No" <?php echo ($d_etag == 'No') ? 'checked="checked"' : ''; ?> />No
Note : id = should b unique as mentioned by Phil in his comments
This is how you can achieve ur task!
<input type="radio" id="etag_1" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input type="radio" id="etag_2" name="etag" value="No" <?php echo ($d_etag == 'No') ? 'checked="checked"' : ''; ?> />No
To find out Which Radio button is clicked use
$("input:radio[name=etag]").click(function() {
var value = $(this).val();
});
or try this
$('input:radio[name=etag]:checked').val();
Can you var_dump($d_etag); and check the data type of that variable. it should be a string with Case-sensitive.
Can you try this:
<input type="radio" id="etagYes" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input type="radio" id="etagNo" name="etag" value="No" <?php echo ($d_etag == 'Yes') ? '' : 'checked="checked"'; ?> />No

Echo checked if checkbox is checked

if this is my checkbox
<input type="checkbox" name="Filter[]" value="Steak" id="Filter"/>
and if checkbox is checked var_export returns me
["Filter"]=> array(1) { [0]=> string(7) "Steak"
how do I echo "checked=checked" if checkbox is checked?
What you need is in_array(), this will check whether that value exists in the array, if your array contains the value, the function will return true and you can simply echo out the checked attribute
if (in_array('YOUR_VALUE_HERE', $arr)) {
echo 'checked="checked"';
}
You can also make a function passing value and array as parameter and returning the value from the function.
$checked = in_array('Steak',$_POST['Filter']) ? ' checked="checked"' : '';
echo '<input type="checkbox" name="Filter[]" value="Steak" id="Filter"'.$checked.'/>';
<?php // Check if the box was sent.
$checked = "";
$status = (isset($_REQUEST['status']));
if ($status == 'checked' )
{
$status = 1;
$checked = 'checked="checked"';
}
else
{
$status = 0;
}
echo $status;
echo <<<END
<form action="" method="post">
<input type="checkbox" name="status" $checked /> Testbox<br />
<input type="submit" onclick="return showDiv();"/>
</form>
END;
?>
I think this might solve your problem
$checked = in_array('Steak',$_POST['Filter']) ? ' checked="checked"' : '';
echo '';

Categories