I am trying to hide and show fields based of the selection in a dropdown field. I got it do hide but when I select another value it doesn't display again.
<script type="text/javacript">
function checkEm(){
if (document.getElementById('JobTypeDDL').selected = "2"){
document.getElementById('DateTbx').style.visibility = "hidden";
document.getElementById('DateTbx').style.display = "none";
} else {
document.getElementById('DateTbx').style.visibility = "visible";
document.getElementById('DateTbx').style.display = "block";
}
}
</script>
<div class="InputField">
<select name="JobTypeDDL" id="JobTypeDDL" onChange="checkEm()">
<option value="0" <?php if ($_POST['JobTypeDDL'] == "0") {echo "selected='selected'";} ?> >Select Job Type</option>
<option value="1" <?php if ($_POST['JobTypeDDL'] == "1") {echo "selected='selected'";} ?> >New Mobility</option>
<option value="2" <?php if ($_POST['JobTypeDDL'] == "2") {echo "selected='selected'";} ?> >Service/Repair/Install</option>
</select>
</div>
<input name="DateTbx" id="DateTbx" type="text" <?php if (!$_POST['DateTbx'] == "") {echo "value='". $_POST['DateTbx']."'";} ?> />
Use == to test equality in an if statement. You are using =, which is assignment.
In the if condition you're using a single =. Use === instead.
Related
Writing a section of a long form to add data to MySQL database and in this form
a boolean checkbox that allows a section to show or hide and I need to insert this boolean to database with the other data
Later I need to get this data from database and show it in front end....
So it will say
Yes (section is true and showing) + the rest of data in the section
My problem is in boolean checkbox to insert it as "YES allowed" to retrieve it later and say "Yes it's Allowed + data" .. I know it's easy but I'm at point of complete confusion
<label>Pets Allowed:</label>
<input class="form-pine" type="checkbox" name="pets" id="petscheck" value="<?php if(isset($result->dogs_permit)){ echo $result->dogs_permit; } ?>" onchange="valueChanged()"><br>
<script type="text/javascript">
function valueChanged() {
if (document.getElementById('petscheck').checked) {
document.getElementById("petsshowhide").style.display = 'block';
} else {
document.getElementById("petsshowhide").style.display = 'none';
}
}
</script>
<div class="col-pine-2" id="petsshowhide" style="display:none;">
<label>Pets fees:</label>
<select class="form-pine" name="dogs-fees">
<option value="">Select Option</option>
<option value="Inclusive"<?php if(isset($result->dogs_fees) && $result->dogs_fees == '0'){ echo 'selected'; } ?>>Inclusive</option>
<option value="Additional Charge"<?php if(isset($result->dogs_fees) && $result->dogs_fees == '1'){ echo 'selected'; } ?>>Additional Charge</option>
</select>
<label>pets Fees:</label>
<input class="form-pine" type="number" name="pets-price" value="<?php if(isset($result->dogs_price)){ echo $result->dogs_price; } ?>">
<label>currency:</label>
<select class="form-pine" name="dogs-curr">
<option value="">Select currency</option>
<option value="USD"<?php if(isset($result->dogs_curr) && $result->dogs_curr == 'USD'){ echo 'selected'; } ?>>USD</option>
<option value="EUR"<?php if(isset($result->dogs_curr) && $result->dogs_curr == 'EUR'){ echo 'selected'; } ?>>EUR</option>
</select>
</div>
function add
if(isset($_POST['submitpineapple'])){
global $wpdb;
$table_pine = $wpdb->prefix . 'ppf_admin';
$dogs_permit = $_POST['pets'];
$dogs_fees = $_POST['dogs-fees'];
$dogs_price = $_POST['pets-price'];
$dogs_curr = $_POST['dogs-curr'];
if(isset($_POST['adds_pineapple'])){
$pid = $_POST['adds_pineapple'];
$sql = "INSERT INTO $table_pine (dogs_permit, dogs_fees, dogs_price, dogs_curr) VALUES ($dogs_permit, '$dogs_fees', $dogs_price, '$dogs_curr')";
if($wpdb->query($sql)){
echo'<div class="success-msg">Save Successfully</div>';
}else{
echo'<div class="errorcss">Something went wrong!</div>';
}
You can store pets as 1 if it is selected or 0 if it is not selected in the table. Or in a parent table.
You will have $_POST['pets'] available when it is selected. You can check for this with isset($_POST['pets']) and get 1 or 0 with $dogs_permit=isset($_POST['pets'])? 1 : 0;
Here is the code that i am using for drop down to select.
<select name="formreg" value="">
<option value="Registered" <?php if($f_reg == 'Registered') { ?> selected <?php echo $f_reg; } ?> >Registered</option>
<option value="NonReg" <?php if($f_reg == 'NonReg') { ?> selected <?php echo $f_reg; } ?> >NonReg</option>
</select>
<select name="regform">
<option value="Registered" <?php if (isset($register) && $register=='Registered') {?> selected='selected' <?php echo $register; }?>>Registered</option>
<option value="NonReg" <?php if (isset($nonreg) && $nonreg == 'NonReg') {?> selected='selected' <?php echo $nonreg;} ?>>NonReg</option>
</select>
At the top of script:
$f_reg = $_POST["f_reg"]; // or $f_reg = $_GET["f_reg"] if you pass parameters as the url part
then your < select > tag:
<select name="formreg">
<option value="Registered" <?php if ($f_reg == "Registered") print "selected"?>>Registered</option>
<option value="NonReg" <?php if ($f_reg == "NonReg") print "selected"?>>NonReg</option>
</select>
I am doing PHP validations for my html values. However when PHP validation fails and I return back to the page, the select tag form data is cleared. Is there anyway to do save and reload the form data in php
<?php
$qualific=$passingyear="";
$qualificErr=$passingyearErr="";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true;
//qualification validations starts here
if(empty($_POST["qualif"]))
{
$qualificErr="* Qualification is Required";
$valid=false;
}
else
{
$qualific=test_input($_POST["qualif"]);
}
//qualification validations starts here
/*yearOfPassing validation starts here*/
if(empty($_POST["yearpass"]))
{
$passingyearErr="* Year Of Pass is Required";
$valid=false;
}
else
{
$passingyear=test_input($_POST["yearpass"]);
}
/*yearOfPassing validation starts here*/
//if valid then redirect
if($valid){
include 'database.php';
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';
exit;
}
}
<form method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>Qualification<span class="error">*</span>:</label>
<select name="qualif">
<option label="Select"></option>
<option>Below SSC(10 Std)</option>
<option>SSC(10 Std) passed</option>
<option>HSC(12 Std) passed</option>
<option>Graduate</option>
<option>Post Graduate</option>
</select>
<span class="error"><?php echo $qualificErr?></span> <br />
<br />
<label>Year of passing<span class="error">*</span>: </label>
<select name="yearpass">
<option label="Select"></option>
<option>1975</option>
<option>1976</option>
<option>1977</option>
</select>
</form>
try <?php if($_POST["qualif"] == "<value>") echo "selected"; ?> in each option tag.
like
<option <?php if($_POST["qualif"] == "Below SSC(10 Std)") echo "selected"; ?>>Below SSC(10 Std)</option>
Currently you are using <option> without values.
Use like :
<option value="SSC" <?php if(isset($_POST['qualif']) && $_POST['qualif'] == 'SSC') { echo "selected"; } ?> >SSC</option>
your are missing value parameter inside <option>.
you need to add some code inside <option> tag
Please try following code hope this will solve the issue.
<select name="qualif">
<option label="Select"></option>
<option value="Below SSC(10 Std)" <?php if($qualific == 'Below SSC(10 Std)') {?> selected <?php } ?>>Below SSC(10 Std)</option>
<option value=">SSC(10 Std) passed" <?php if($qualific == '>SSC(10 Std) passed') {?> selected <?php } ?>>SSC(10 Std) passed</option>
<option value="HSC(12 Std) passed" <?php if($qualific == 'HSC(12 Std) passed') {?> selected <?php } ?>>HSC(12 Std) passed</option>
<option value="Graduate" <?php if($qualific == 'Graduate') {?> selected <?php } ?>>Graduate</option>
<option value="Post Graduate" <?php if($qualific == 'Post Graduate') {?> selected <?php } ?>>Post Graduate</option>
</select>
<span class="error"><?php echo $qualificErr?></span> <br />
<br />
<label>Year of passing<span class="error">*</span>: </label>
<select name="yearpass">
<option label="Select"></option>
<option value="1975" <?php if($passingyear == '1975') {?> selected <?php } ?>>1975</option>
<option value="1976" <?php if($passingyear == '1976') {?> selected <?php } ?>>1976</option>
<option value="1977" <?php if($passingyear == '1977') {?> selected <?php } ?>>1977</option>
</select>
You should use jquery
<script type="text/javascript">
$(document).ready(function()
{
$("#id").val("<?php echo $_POST['qualif']; ?>");
});
</script>
I have a number field and a drop-down list that I am using as part of a form, as follows:
<label for="height">Height/Thickness</label>:
<input <?php if (empty($messages) == false) {echo 'value="'.htmlentities($_POST['height'], ENT_QUOTES).'"';} ?> type="number" name="height" id="height" />
<select class="dimensions" name="heightunit">
<option value="">Select...</option>
<option value="mm">mm</option>
<option value="cm">cm</option>
<option value="m">m</option>
<option value="in">in</option>
</select>
The $messages variable is an array that stores any error messages generated by the form. The bit between the <?php & ?> tags are there to ensure that if there is an error, that it will echo back the values the user entered before they submitted, so saving them time (and frustration) from entering the data again.
I want to do the same with the drop down list. At the moment, I am adding the following line within each option tag (similar to this answer):
<?php if ((empty($messages) == false) && ($_POST['lengthunit'] == 'mm') {echo 'selected="selected"';} ?>
But then it's really untidy:
....
<select class="dimensions" name="lengthunit">
<option value="" <?php if ((empty($messages) == false) && ($_POST['lengthunit'] == '') {echo 'selected="selected"';} ?>>Select...</option>
<option value="mm" <?php if ((empty($messages) == false) && ($_POST['lengthunit'] == 'mm') {echo 'selected="selected"';} ?>>mm</option>
<option value="cm" <?php if ((empty($messages) == false) && ($_POST['lengthunit'] == 'cm') {echo 'selected="selected"';} ?>>cm</option>
<option value="m" <?php if ((empty($messages) == false) && ($_POST['lengthunit'] == 'm') {echo 'selected="selected"';} ?>>m</option>
<option value="in" <?php if ((empty($messages) == false) && ($_POST['lengthunit'] == 'in') {echo 'selected="selected"';} ?>>in</option>
</select>
My question is - Is there a more elegant way to do this, without adding the php tag inside each option tag?
(I am thinking along the lines of:
if (OPTION_VALUE === $_POST['heightunit']) {echo 'selected="selected"';}. Is there anything that can be substituted for the OPTION_VALUE?)
EDIT: The reason I want to do this is because I want to apply this to a list of countries, which is over 200, and I just want to write 'good code'.
**EDIT: Looking back at it now, it looks so easy...anyways, thank you for your help and this is what I went with in the end:
function lengthlist($selectname) {
$distanceunits = array('','mm','cm','m','in');
foreach ($distanceunits as $value) {
if ($value == '') {$value = '...';}
if ($value == $_POST["$selectname"]) {
echo "<option value=\"$value\" selected=\"selected\">$value</option>";
} else {
echo "<option value=\"$value\">$value</option>";
}
}
}
Or you can also load the dimensions values in an array and then just write them on a while loop
<select class="dimensions" name="lengthunit">
<option>Select...</option>
<?php
$i=0;
$array=array("mm", "cm", "m", "in");
while ($i<4){ ?>
<option value="<?php echo $array[$i];?>"<?php if ((empty($messages) == false) &&
($_POST['lengthunit'] == $array[$i])) {echo 'selected="selected"';} ?>><?php echo
$array[$i];?></option>
<?php $i++; } ?>
</select>
Because in other cases you might wish to add more options for the users to select, so you would just add them to the array. You can also use a foreach loop instead of a while
if you want more elegant way - do it opposite way - move php outside of the option tag.
You can generate the whole list together with the tags from within php, like:
<?
/*some loop*/
echo "<option value='a' {$selected} >aaa</option>";
/*end of the loop*/
?>
where $selected is previously set as "selected" or just empty string. And use arrays of course as appropriate.
Once submitted selected option, the data is not stored.
just want to know how to post back the data if validation fails
The following line doesnt really work for me.
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
if someone could give me a hand?
Many thanks, here is my code
<?php
if(isset($_POST['numbers']) &&($_POST['fruits']) && $_POST['numbers'] != "null" && $_POST['fruits'] !== "null")
{
echo "Thank you!";
} elseif (isset($_POST['numbers']) && $_POST['numbers'] = "null") {
echo "you forgot to choose a number";
}
elseif(isset($_POST['fruits']) && $_POST['fruits'] = "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
<option value="null" selected="selected">-</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<select id="fruits" name="fruits" value="<?php echo (isset($_POST['fruits']))? $_POST['fruits'] : ''; ?>"/>
<option value="null" selected="selected">-</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Pear">Pear</option>
</select>
<input type="submit" value="Send" />
</form>
Solved it, Maybe not a best way, but at least got it sorted:
<?php
$item = null; #
$itemyear = null;
if(isset($_POST['numbers'])){
$item = $_POST['numbers'];
}
if(isset($_POST['fruits'])){
$itemyear = $_POST['fruits'];
}
if(isset($item) && isset($itemyear) && $item != "null" && $itemyear !== "null")
{
echo "Thank you!";
} elseif ($item == "null") {
echo "you forgot to choose a number";
}
elseif($itemyear == "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" />
<option value="null" selected="selected">-</option>
<option value="01" <?php if($item == '01'): echo "selected='selected'"; endif; ?>>01</option>
<option value="02" <?php if($item == '02'): echo "selected='selected'"; endif; ?>>02</option>
<option value="03" <?php if($item == '03'): echo "selected='selected'"; endif; ?>>03</option>
</select>
<select id="fruits" name="fruits" />
<option value="null" selected="selected">-</option>
<option value="Apple"<?php if($itemyear == 'Apple'): echo "selected='selected'"; endif; ?> >Apple</option>
<option value="Banana"<?php if($itemyear == 'Banana'): echo "selected='selected'"; endif; ?>>Banana</option>
<option value="Pear"<?php if($itemyear == 'Pear'): echo "selected='selected'"; endif; ?>>Pear</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
echo $item ."-". $itemyear;
?>
the PHP isset() function returns either true or false (depending on whether the input is.. well... set.
You would want to use this:
value='<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : ""; ?>
You can't set a value attribute on a <select>. You have to find the correct <option> and set its selected attribute. Personally, I put a data-default attribute on the <select> and then use JavaScript to loop through the options and find the right one.
Oh now I see.
I don't know what the usual thing to do is but one way is to put all the data as a query string when you redirect the user back. The reason why it doesn't stay in the $_POST global is because it's only kept on the page you post to then it's gone.
So when you redirect the user back
if(validationFailed)
{
header("Location: page.php?data=example");
}
The data can then be retrieved in page.php by using
$data = $_GET['data']; // contains "example"