Selected radio button make field true or false - php

I have the following code:
<?php $radioSwitch = of_get_option('radio_switch');
if ($radioSwitch == "true") { ?>
<!-- radio switch -->
<div class="row switches clearfix">
<input type="radio" name="switch" id="switch1" value="<?php echo of_get_option('switch1'); ?>" /><label for="switch1" class="field"><?php echo of_get_option('switch1'); ?></label>
<input type="radio" name="switch" id="switch2" value="<?php echo of_get_option('switch2'); ?>" /><label for="switch2" class="field"><?php echo of_get_option('switch2'); ?></label>
</div>
<?php } ?>
................................................................................
<?php
$datePicker1 = of_get_option('datepicker1');
$datePicker2 = of_get_option('datepicker2');
if (($datePicker1 == "true") || ($datePicker2 == "true")) { ?>
<div class="row clearfix">
<?php if ($datePicker1 == "true") { ?>
<!-- datepicker 1 -->
<input type="text" id="dateFrom" name="<?php echo of_get_option('datepicker1_text'); ?>" value="<?php echo of_get_option('datepicker1_text'); ?>" tabindex="6" />
<label class="error" for="dateFrom" id="dateFrom_error">Selectați data.</label>
<?php } ?>
<?php if ($datePicker2 == "true") { ?>
<!-- datepicker 2 -->
<input type="textarea" id="dateTo" name="<?php echo of_get_option('datepicker2_text'); ?>" value="<?php echo of_get_option('datepicker2_text'); ?>" tabindex="7" />
<label class="error" for="dateTo" id="dateTo_error">Selectați data.</label>
<?php } ?>
</div>
<?php } ?>
If radio button "switch1" is selected, only "$datepicker1" must be true and "$datepicker2" must be false, and if radio button "switch2" is selected, both "$datepicker1" and "$datepicker2" must be true. Anyone could help me?

PHP is a server-side language which is parsed on the server. You cannot modify variables of PHP on the browser.
Try submitting the radio button options as a form into a PHP page.

Related

PHP third branch of if else statement not working properly

<!doctype html>
<?php
This is for setting cookies
//first failed attempt
if (isset($_POST['firstname']) != null || isset($_POST['lastname']) != null ||
isset($_POST['phonenumber']) != null || isset($_POST['email']) != null
|| isset($_POST['sulleyaddress']) != null || isset($_POST['question1']) != null
|| isset($_POST['question2']) != null || isset($_POST['question3']) != null
|| isset($_POST['question4']) != null || isset($_POST['question5']) != null) {
setcookie('firstname',$_POST['firstname']);
setcookie('lastname',$_POST['lastname']);
setcookie('phonenumber',$_POST['phonenumber']);
setcookie('email',$_POST['email']);
setcookie('sulleyaddress',$_POST['sulleyaddress']);
setcookie('question1',$_POST['question1']);
setcookie('question2',$_POST['question2']);
setcookie('question3',$_POST['question3']);
setcookie('question4',$_POST['question4']);
setcookie('question5',$_POST['question5']);
}
this is for reseting cookies
//second failed attempt
if (isset($_POST['firstname']) == null) {
setcookie('firstname','');
}
if (isset($_POST['lastname']) == null) {
setcookie('lastname','');
}
if (isset($_POST['phonenumber']) == null) {
setcookie('phonenumber','');
}
if (isset($_POST['email']) == null) {
setcookie('email','');
}
if (isset($_POST['sulleyaddress']) == null) {
setcookie('sulleyaddress','');
}
if (isset($_POST['question1']) == null) {
setcookie('question1','');
}
if (isset($_POST['question2']) == null) {
setcookie('question2','');
}
if (isset($_POST['question3']) == null) {
setcookie('question3','');
}
if (isset($_POST['question4']) == null) {
setcookie('question4','');
}
if (isset($_POST['question5']) == null) {
setcookie('question5','');
}
?>
<html>
<head>
<title>Assignment 2 - Anthony Taveras</title>
<style>#import url("css/styles.css");</style>
<!-- <link rel="stylesheet" href="styles.css" /> -->
</head>
<body>
<?php
1st step. Make the form appear. Allow user to enter and submit data.
if (!isset($_POST['submit'])) {
?>
<div id="content-container">
<div id="content">
<form name="form" id="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Please Fill Out the Form</legend>
<ol>
<li><input type="text" name="firstname" value="<?php if (isset($_COOKIE['firstname'])) {echo $_COOKIE['firstname'];} ?>" ><label> First Name</label></li>
<li><input type="text" name="lastname" value="<?php if (isset($_COOKIE['lastname'])) {echo $_COOKIE['lastname'];} ?>" ><label> Last Name</label></li>
<li><input type="text" name="phonenumber" value="<?php if (isset($_COOKIE['phonenumber'])) {echo $_COOKIE['phonenumber'];} ?>" ><label> Phone Number</label></li>
<li><input type="text" name="email" value="<?php if (isset($_COOKIE['email'])) {echo $_COOKIE['email'];} ?>" ><label> Email</label></li>
<li><input type="text" name="sulleyaddress" value="<?php if (isset($_COOKIE['sulleyaddress'])) {echo $_COOKIE['sulleyaddress'];} ?>" ><label> Sulley Address</label></li>
</ol>
</fieldset>
<fieldset>
<legend>Please answer these questions</legend>
<ol>
<li class="question"><input type="text" name="question1" value="<?php if (isset($_COOKIE['question1'])) {echo $_COOKIE['question1'];} ?>" ><label> What is your favorite color?</label></li>
<li class="question"><input type="text" name="question2" value="<?php if (isset($_COOKIE['question2'])) {echo $_COOKIE['question2'];} ?>" ><label> Where were you born?</label></li>
<li class="question"><input type="text" name="question3" value="<?php if (isset($_COOKIE['question3'])) {echo $_COOKIE['question3'];} ?>" ><label> What is your favorite food?</label></li>
<li class="question"><input type="text" name="question4" value="<?php if (isset($_COOKIE['question4'])) {echo $_COOKIE['question4'];} ?>" ><label> What is your favorite movie?</label></li>
<li class="question"><input type="text" name="question5" value="<?php if (isset($_COOKIE['question5'])) {echo $_COOKIE['question5'];} ?>" ><label> What is your favorite book?</label></li>
<li><input type="submit" name="submit" value="Submit" /></li>
</ol>
</fieldset>
</form>
</div>
</div>
2nd step once submitted user can view responses
<?php
} elseif (isset($_POST['submit'])) {
?>
<!--=============================================Form Preview--------------------------------------->
<div id="content-container">
<div id="content">
<h1> Here's what you put down</h1>
<p> First Name: <?php print $_POST['firstname']; ?> </p>
<p> Last Name: <?php print $_POST['lastname']; ?> </p>
<p> Phone Number: <?php print $_POST['phonenumber']; ?> </p>
<p> Email: <?php print $_POST['email']; ?> </p>
<p> Sulley Address: <?php print $_POST['sulleyaddress']; ?> </p>
<p class="question"> What is your favorite color? <?php print $_POST['question1']; ?> </p>
<p class="question"> Where were you born? <?php print $_POST['question2']; ?> </p>
<p class="question"> What is your favorite food? <?php print $_POST['question3']; ?> </p>
<p class="question"> What is your favorite movie? <?php print $_POST['question4']; ?> </p>
<p class="question"> What is your favorite book? <?php print $_POST['question5']; ?> </p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="edit" value="Edit" />
</form>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="confirm" value="Finish" />
</form>
</div>
</div>
This is the problem area. This last step of the if else statement will not work properly. I'm not sure why. Any help is needed. Thanks.
<?php } elseif (isset($_POST['confirm'])) {
?>
<!--=============================================Form Confirmed--------------------------------------->
<div id="content-container">
<div id="content">
<p> Thank you, your data has been submitted</p>
</div>
</div>
<?php }
?>
</body>
</html>
You've got two form tags. One has the submit, one the hidden field.
You're not passing the variable therefore.
Try troubleshooting!
echo '<pre>';
print_r($_POST);
I would guess that it is because the forms are empty.
Try:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input id='placeholder' name='placeholder' type='text' value='placeholder' hidden>
<input type="submit" name="confirm" value="Finish" />
</form>

get value of radio button using jQuery and php

I want to get the value clicked in radio button.
This is my code:
<ul class="collapsible popout" data-collapsible="accordion" id="clicks">
<?php
foreach ($preguntas['preguntas'] as $row)
{
$opciones = $pregunta->opciones($row[0]);
?>
<li>
<div class="collapsible-header"><i class="material-icons">question_answer</i><?php echo utf8_encode($row[2]); ?></div>
<div class="collapsible-body">
<?php foreach ($opciones as $opcion){ ?>
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
<?php } ?>
</div>
<?php } ?>
</li>
</ul>
I need to get the value of this input id="opcion_..."
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
The problem is name and id is changing, it's not the same
Any idea?
Thank you.
JQuery selector for part of attribute $("[attribute^='value']") like this:
$("[id^='opcion_']").click(function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label><input name="pregunta_xxx" type="radio" id="opcion_123" value="123">radio for 123</label>
<label><input name="pregunta_xxx" type="radio" id="opcion_456" value="456">radio for 456</label>

How to add/change fields dynamicaly in form from if else condition?

Here i have numerous input fields in a form, which are working perfectly. And now, i want to add new input boxes and their labels according to property_type , Which is already exist on this page.
Now, Kindly Suggest me How can i add new fields in that form.
Here is my Form Code
<div class="dashboard_price_left">
<h3>
<?php
if ($this->lang->line('BasePrice') != '') {
echo stripslashes($this->lang->line('BasePrice'));
} else
echo "Base Price";
?>
</h3>
<p>
<?php
if ($this->lang->line('Atitleandsummary') != '') {
echo stripslashes($this->lang->line('Atitleandsummary'));
} else
echo "Set weekly or monthly price guests will see for your listing.";
?>
</p>
</div>
<?php
if ($listDetail->row()->home_type == 'Office') {
} else if ($listDetail->row()->home_type == 'House') {
}
?>
<form id="pricelist" name="pricelist" action="site/product/savePriceList" method="post">
<div class="dashboard_price_right">
<label><?php
if ($this->lang->line('Pernight') != '') {
echo stripslashes($this->lang->line('Pernight'));
} else
echo "Per night";
?>
</label>
<div class="amoutnt-container">
<?php if ($currentCurrency == '') { ?>
<span class="WebRupee"><?php echo $currencyDetail->row()->currency_symbols; ?></span>
<?php } else { ?>
<span class="WebRupee"><?php echo $currentCurrency; ?></span>
<?php } ?>
<input type="text" id="price" value="<?php
if ($listDetail->row()->price != '0.00') {
echo intval($listDetail->row()->price);
}
?>" class="per_amount_scroll" name="price" onkeypress="return onlyNumbersWithDot(event);" onchange="javascript:Detailview(this,<?php echo $listDetail->row()->id; ?>, 'price');" />
<input type="hidden" id="id" name="id" value="<?php echo $listDetail->row()->id; ?>" />
</div>
<div class="dashboard_currency">
<label><?php
if ($this->lang->line('Currency') != '') {
echo stripslashes($this->lang->line('Currency'));
} else
echo "Currency";
?>
</label>
<div class="select select-large select-block">
<select name="currency" id="currency" onchange="javascript:Detailview(this,<?php echo $listDetail->row()->id; ?>, 'currency');get_currency_symbol(this)" >
<!--<option value="">select</option>-->
<?php foreach ($currencyDetail->result() as $currency) { ?>
<option value="<?php echo $currency->currency_type; ?>" <?php if ($listDetail->row()->currency == $currency->currency_type) echo 'selected="selected"'; ?>><?php echo $currency->currency_type; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
</form>
Here is a pic of my Property prices form
Note: There are two types of property that are following:
1. Office
2. House
on the basis of these property, I want to add more fields in my form.
Please suggest me, Thanks in Advance :)
you can change it base on conditon and pass hidden field which indicates you office or house.
<?php
if ($listDetail->row()->home_type == 'Office')
{
<input type="hidden" name="home_type" value="Office" />
<input type="text" name="per_day" value="" />
<input type="text" name="half_day" value="" />
<input type="text" name="per_hour" value="" />
}
else if ($listDetail->row()->home_type == 'House')
{
<input type="hidden" name="home_type" value="House" />
<input type="text" name="per_night" value="" />
<input type="text" name="per_day" value="" />
<input type="text" name="per_hour" value="" />
}
?>

PHP form with radio buttons - make only one answer required, second optional

I have this PHP form with radio buttons:
{
$agreements = jm_get_application_setting( 'application_agreements', '' );
if( empty( $agreements ) ) return;
$questions = explode( "\n", $agreements );
if (!empty($questions)):
foreach ($questions as $index => $question) :
?>
<div class="form-group">
<p><strong for="question-<?php echo sanitize_title($question); ?>" ><?php echo $question; ?></strong></p>
<div class="form-control-flat">
<label class="radio">
<input type="radio" name="_noo_application_answer_<?php echo $index; ?>" value="1" required><i></i><?php echo esc_html__('Do il consenso', 'noo'); ?>
</label>
</div>
<div class="form-control-flat">
<label class="radio">
<input type="radio" name="_noo_application_answer_<?php echo $index; ?>" value="0" required><i></i><?php echo esc_html__('Nego il consenso', 'noo'); ?>
</label>
</div>
<input type="hidden" name="_noo_questions[]" value="<?php echo esc_attr($question); ?>"/>
</div>
<?php
endforeach;
endif;
}
How can I make only one answer required to validate the form?
I mean.. Just the first MUST be selected, but I also need to show the second one for "legal reasons".
As is, this form accepts both choices as acceptable.
You can set a variable for the condition, and change it's value at the end of first iteration.
if (!empty($questions)):
$first = 1; // <----- HERE
foreach ($questions as $index => $question) :
?>
<div class="form-group">
<p><strong for="question-<?php echo sanitize_title($question); ?>" ><?php echo $question; ?></strong></p>
<div class="form-control-flat">
<label class="radio">
<input type="radio"
name="_noo_application_answer_<?php echo $index; ?>"
value="1"
<?php echo $first ? "required" : ""; ?>> // <----- HERE
<i></i><?php echo esc_html__('Do il consenso', 'noo'); ?>
</label>
</div>
<div class="form-control-flat">
<label class="radio">
<input type="radio"
name="_noo_application_answer_<?php echo $index; ?>"
value="0"
<?php echo $first ? "required" : ""; ?>> // <----- HERE
<i></i><?php echo esc_html__('Nego il consenso', 'noo'); ?>
</label>
</div>
<input type="hidden" name="_noo_questions[]" value="<?php echo esc_attr($question); ?>"/>
</div>
<?php
$first = 0; // <----- HERE
endforeach;
endif;

show div with forms when customer starts typing in first form

i have a registration form. With text inputs for First name, second name and then company name.
If the customer starts typing, there has to slide down another div with forms where the customers can give their company information.
I had an old module, i tried to copy some code but it won't work because there are some functions which i don't know.
Here is my code, can somebody help me?
Sorry for my bad English:P
<div id="NewCustomerDiv" >
<label>Company name: </label>
<input type="text" name="CompanyName" value="<?php echo $customer['CompanyName']; ?>"/>
<br />
<div id="CustomerCompanyDiv" <?php if(!isset($customer['CompanyName']) || !$customer['CompanyName']){ echo "style=\"display:none;\""; } ?>>
<label>Company name:</label>
<input type="text" name="CompanyNumber" value="<?php if(isset($customer['CompanyNumber'])){ echo $customer['CompanyNumber']; } ?>"/>
<br />
<label>taxnumber:</label>
<input type="text" name="TaxNumber" value="<?php if(isset($customer['TaxNumber'])){ echo $customer['TaxNumber']; } ?>"/>
<br />
<?php if(count($array_legaltype) > 0){ ?>
<label>companytax:</label>
<select name="LegalForm" class="w1">
<?php foreach($array_legaltype as $k=>$v){ ?>
<option value="<?php echo $k; ?>" <?php if(isset($customer['LegalForm']) && $customer['LegalForm'] == $k){ echo "selected=\"selected\""; } ?>><?php echo $v; ?></option>
<?php } ?>
</select>
<br />
<?php } ?>
<br />
</div>
</div>
You have to do it in javascript (client-side) I would recommand you to use a library like jquery it'll be easier.
<script type="text/javascript">
//with jQuery
$(document).ready(function(){
//admiting that the input[name=CompanyName] is the one you want to bind with the div
$('#NewCustomerDiv input[name=CompanyName]').keyup(function(){
//admiting that the div you want to show is CustomerCompanyDiv
if(!$(this).val()){
$('#CustomerCompanyDiv').hide();
}
else{
$('#CustomerCompanyDiv').show();
}
});
if($('#NewCustomerDiv input[name=CompanyName]').val()){
$('#CustomerCompanyDiv').show();
}
});
</script>
<div id="NewCustomerDiv" >
<label>Company name: </label>
<input type="text" name="CompanyName" value="<?php echo $customer['CompanyName']; ?>"/>
<br />
<div id="CustomerCompanyDiv" style="display:none"> <label>Company name:</label>
<input type="text" name="CompanyNumber" value="<?php if(isset($customer['CompanyNumber'])){ echo $customer['CompanyNumber']; } ?>"/>
<br />
<label>taxnumber:</label>
<input type="text" name="TaxNumber" value="<?php if(isset($customer['TaxNumber'])){ echo $customer['TaxNumber']; } ?>"/>
<br />
<?php if(count($array_legaltype) > 0){ ?>
<label>companytax:</label>
<select name="LegalForm" class="w1">
<?php foreach($array_legaltype as $k=>$v){ ?>
<option value="<?php echo $k; ?>" <?php if(isset($customer['LegalForm']) && $customer['LegalForm'] == $k){ echo "selected=\"selected\""; } ?>><?php echo $v; ?></option>
<?php } ?>
</select>
<br />
<?php } ?>
<br />
</div>
</div>
See it in action : http://jsfiddle.net/TMG85/

Categories