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"
Related
I trying to create dynamic server side select input, after i submit, the set_value('nilai[]') not showing any value.
Here's my Controller below:
$this->load->library('form_validation');
$this->form_validation->set_rules('nilai[]', 'Nilai Pantuhir', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('pantuhir/pantuhir_form');
} else {
$list_pantuhir = $this->input->post('nilai');
foreach ($list_pantuhir as $key => $value) {
echo $value."<br />";
}
}
Here's my view below :
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select class="form-control" name="nilai[]">
<option value="">- Choose-</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'A';?>" <?php if(set_value('nilai[]') == $rowPerson['intUserId'].'-'.'A') { echo 'selected'; } ?>>A</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'B';?>" <?php if(set_value('nilai[]') == $rowPerson['intUserId'].'-'.'B') { echo 'selected'; } ?>>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>
I want to show set_value and get selected in option field if validation not correct.
Hope this will help you :
Use set_select instead of set_value. If you use a menu, this function permits you to display the menu item that was selected, after the form validation throws any error
It should be like this :
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select name="nilai[]" >
<option value="" >---Choose----</option>
<option
value="<?=$rowPerson['intUserId'].'-A';?>"
<?=set_select('nilai[]', $rowPerson['intUserId'].'-A');?>
>A</option>
<option
value="<?php echo $rowPerson['intUserId'].'-B';?>"
<?=set_select('nilai[]', $rowPerson['intUserId'].'-B');?>
>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>
For more : https://www.codeigniter.com/user_guide/helpers/form_helper.html#set_select
Use this code in the view
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select class="form-control" name="nilai[]">
<option value="">- Choose-</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'A';?>" <?php if(set_value('nilai[]',rowPerson['intUserId'].'-'.'A') == $rowPerson['intUserId'].'-'.'A') { echo 'selected'; } ?>>A</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'B';?>" <?php if(set_value('nilai[]',$rowPerson['intUserId'].'-'.'B') == $rowPerson['intUserId'].'-'.'B') { echo 'selected'; } ?>>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>
I have a form with an input field. It works very well. When I write something into the input field and click submit, the value is stored into my database:
<form action="update.php?id=<?php echo $id?>" method="post">
<input name="option" type="text" value="<?php echo !empty($option)?$option:'';?>">
<button type="submit">Submit</button>
</form>
But I don't need an input field, I need a select box instead. I tried this, but it is not working:
<form action="update.php?id=<?php echo $id?>" method="post">
<select>
<?php
$default = "No";
$options = array("Yes","No");
foreach($options as $val) {
echo ($val == $default) ? "<option selected=\"selected\" value=\"$val\">$val</option>":"<option value=\"$val\">$val</option>";
}
?>
</select>
<button type="submit">Submit</button>
</form>
Do you have an idea what I did wrong?
missing name in select
<select name="option">
<?php
$default = "No";
$options = array("Yes","No");
foreach($options as $val) {
echo ($val == $default) ? "<option selected=\"selected\" value=\"$val\">$val</option>":"<option value=\"$val\">$val</option>";
}
?>
</select>
Missing name attribute of select input.
<select name="option">
You can access it by -
$_POST['option'] // for POST method
// sample code if you need call the value from database here for static value
<form action="update.php?id=<?php echo $id?>" method="post" >
<select name="option">
<option value="PHP" <?php if($options=="PHP") echo 'selected="selected"'; ?> >PHP</option>
</select>
<button type="submit">Submit</button>
</form>
I finally found the answer:
<select name="option">
<option <?php if ($option == Yes ) echo 'selected'; ?> value="Yes">Yes</option>
<option <?php if ($option == No ) echo 'selected'; ?> value="No">No</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>
<form name="search" method="post" >
Seach for: <input type="text" name="find" value="<?php echo (isset($_POST['find']) ? $_POST['find'] : ''); ?>" /> in
<Select NAME="field">
<Option VALUE="category1" <?php echo (isset($_POST['field']) && $_POST['field'] === 'category1') ? 'selected="selected"': ''; ?>>category1</option>
<Option VALUE="category2" <?php echo (isset($_POST['field']) && $_POST['field'] === 'category2') ? 'selected="selected"': ''; ?>>category2</option>
</Select>
<input type="submit" name="search" value="Search" />
</form>
<?php
if(!empty($_POST)){
$options = array('category1'=> array('1' => 'dog', '2' => 'cat'), 'category2' =>array('1'=>'flower', '2'=>'grass'));
$input = trim($_POST['find']);
$category = $_POST['field'];
$output = $options[$category][$input];
echo $output;
}
?>
Question:
How could I make category2 to be the default value for the select box instead of category1? I tried this:
<Option VALUE="category2" <?php
echo (isset($_POST['field']) && $_POST['field'] === 'category2') ? 'selected="selected"' : '';
?> selected = "selected">category2</option>
but it breaks my function. When I input 1 and select category1, after I clicked search, the select box changed to category2. That is not what I want. I want this function to perform like this:
Remember the values that user make for input field and select box.
Set the default value category2 for the select box.
How could I achieve this?
try default will be cat2 and after selected will be result
<form method="post">
<Select name="field">
<Option <?php if(isset($_POST['field']) && $_POST['field'] == "category1") echo 'selected="selected"'; ?> VALUE="category1" >category1</option>
<Option <?php if ((isset($_POST['field']) && $_POST['field'] == "category2") OR empty($_POST['field'])) echo 'selected="selected'; ?> VALUE="category2" >category2</option>
</Select>
<input type="submit" />
</form>
u may use like this
<form method="post">
<Select name="field">
<Option VALUE="category2" >category2</option>
<Option <?php if($_POST['field'] == "category1") echo 'selected="selected"'; ?> VALUE="category1" >category1</option>
<Option <?php if($_POST['field'] == "category3") echo 'selected="selected"'; ?> VALUE="category3" >category3</option>
</Select>
<input type="submit" />
</form>
I have simple code
File name:newEmptyPHP.php
<form method="post" action="newEmptyPHP.php">Title:
<select name="title">
<option value="select">Select</option>;
<option value="Dr" selected=<?php if(isset($_POST[ 'title'])=="Dr" ){ echo "selected"; } ?>>Dr</option>';
<option value="Prof">Prof</option>';
<option value="Mr">Mr</option>';
<option value="Ms">Ms</option>';
<option value="Miss">Miss</option>';
<option value="Mrs">Mrs</option>';</select>
<input type="submit" value="submit">
</form>
I want to keep selected particular value which was selected before the form submission.
But i did not get the desirable output.
any help appreciated.
change this
<option value="Dr" selected=<?php if(isset($_POST['title']) == "Dr")
{ echo "selected"; } ?> >Dr</option>';
to
<option value="Dr" <?php if(isset($_POST['title']) && $_POST['title'] == "Dr")
{ echo "selected='selected'"; } ?> >Dr</option>';
<?php if(isset($_POST['title']) == "Dr"){ echo "selected"; } ?>
Change to
<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo "selected"; } ?>
But better to use JS / jQuery. Example for jQuery:
<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"):?>
<script type="text/javascript">
$(function(){
$('select[name=title]').val('<?php echo html_special_chars($_POST[''title'])?>');
});
</script>
<?php endif?>
<?php
if($_POST['title'] == "Dr")
{ ?>
<option value="Dr" selected>Dr</option>
<?php
}
else
{
?>
<?php
<option value="Dr">Dr</option>
<?php
}
?>
Change
<option value="Dr" selected=<?php if(isset($_POST['title']) == "Dr"){ echo "selected"; } ?> >Dr</option>';
To :
<option value="Dr" <?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo 'selected="selected"'; } ?> >Dr</option>';
1.Remove isset where you are checking it for value, isset will return either true or false
2.Make selected="selected inside if condition
There is a mistake in your code. isset(Something) will return either true or false and will not return Dr you are looking for.
Retry with this
<form method="post" action="newEmptyPHP.php">
Title:<select name="title" >
<option value="" >Select</option>
<option value="Dr" selected=<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo "selected"; } ?> >Dr</option>';
<option value="Prof" <?php if(isset($_POST['title']) && $_POST['title'] == "Prof"){ echo "selected"; } ?>>Prof</option>';
<option value="Mr" <?php if(isset($_POST['title']) && $_POST['title'] == "Mr"){ echo "selected"; } ?>>Mr</option>';
<option value="Ms" <?php if(isset($_POST['title']) && $_POST['title'] == "Ms"){ echo "selected"; } ?>>Ms</option>';
<option value="Miss" <?php if(isset($_POST['title']) && $_POST['title'] == "Miss"){ echo "selected"; } ?>>Miss</option>';
<option value="Mrs" <?php if(isset($_POST['title']) && $_POST['title'] == "Mrs"){ echo "selected"; } ?>>Mrs</option>';
</select>
<input type="submit" value="submit">
I thin you have static option values.If so the you have to check for every option like as below for one.
<form method="post" action="newEmptyPHP.php">
Title:<select name="title" >
<option value="select" >Select</option>;
<?php
$selected = "";
if(isset($_POST['title']) && $_POST['title'] == "Dr")
{
$selected = 'selected="selected"';
}
?>
<option value="Dr" <?php echo $selected; ?>>Dr</option>
<option value="Prof">Prof</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
<option value="Mrs">Mrs</option>
</select>
<input type="submit" value="submit">
</form>
I would rather prefer this code for the purpose
$option_array = array('select'=>'select','mr'=>'mr'... other values);
<?php
$option_string = '';
foreach($option_array as $key=>$value)
{
if($key == $_POST['title'])
{
$selected = 'selected';
}
else
{
$selected = '';
}
$option_string .= "<option value='$key' $selected>$value</option>";
}
?>
<form method="post" action="newEmptyPHP.php">Title:
<select name="title">
<?php echo $option_string; ?>
</select>
<input type="submit" value="submit">