When I am setting same name in HTML 'select name' and set_rules name in form validation rules; it does not showing it is a required field.
My controller for validation as follows:
$this->form_validation->set_rules('cate', 'category name','trim|required|xss_clean|callback_select_validate');
function select_validate($abcd)
{
// 'none' is the first option that is default "-------Choose City-------"
if($abcd=="none"){
$this->form_validation->set_message('select_validate', 'Please Select Your Category.');
return false;
} else{
// User picked something.
return true;
}
}
if($this->form_validation->run() == false){
$this->index();
}
My HTML select form code as follows:
<select name="cate" >
<option value="none" selected="selected">Select Catagory</option>
<?php
foreach ($catagory_all as $value) {
if(!empty($project) && ($project->cat_id == $value->cat_id)){
echo '<option value="' . $value->id . '" selected="">' . $value->catagory . '</option>';
}
else{
echo '<option value="' . $value->cat_id . '">' . $value->catagory . '</option>';
}
}
?>
</select>
I've modified you coding... Please check with that.
View:
<select name="cate" >
<option value="" selected="selected">Select Catagory</option>
//Just specify value as empty. From that we can easily use required.
<?php
foreach ($catagory_all as $value) {
if(!empty($project) && ($project->cat_id == $value->cat_id)){
echo '<option value="' . $value->id . '" selected="">' . $value->catagory . '</option>';
}
else{
echo '<option value="' . $value->cat_id . '">' . $value->catagory . '</option>';
}
}
?>
</select>
Controller:
No need of callback. If the selection value is empty, It will return required error!
You can only specify following this,
$this->form_validation->set_rules('cate', 'category name','trim|required|xss_clean');
You call callback_select_validateselect_validate it means you code looking for function select_validateselect_validate in your code
so You have to change your function name from
function select_validate($abcd)
TO
function select_validateselect_validate($abcd)
OR remove select_validate from your callback
$this->form_validation->set_rules('cate', 'category name','trim|required|xss_clean|callback_select_validate');
Related
I'm trying to translate a Java 'calculator' I wrote to PHP/HMTL. To get this to function the initial selection has to be remebered. I know I need to use $_POST['subject'] to get $cc this however causes a problem for me.
I tried several versions but I fear I lack some basics. Is anyone able to push me in the right direction?
Code without remember option:
<form action="" method="post">
<legend>Make a selection below</legend>
<br>
Field of interest:<br>
<select name="subject">
<?php
$subjectarray = array("Please select a subject", "Wavelenght", "Radioactive Decay");
foreach ($subjectarray as $cc => $subject) {
echo '<option value="' . $cc . '">' . $subject . '</option>';
}
?>
</select>
Code with a version of the remember option (clearly not working);
foreach ($subjectarray as $cc => $subject) {
echo '<option value="' . $cc . '"
if ($_POST['subject'] == $cc) echo 'selected="selected"';>' . $subject . '</option>';
}
Another try
foreach ($subjectarray as $cc => $subject) {
echo "<option value=\"{$cc}\"";
echo ($_GET['subject'] == $cc) ? 'selected="selected"' :"";
echo ">" . $subject . "</option>";
}
How about:
<select name="subject">
<?php
foreach(["Please select a subject", "Wavelenght", "Radioactive Decay"] as $i => $val) {
$sel = empty($_POST['subject']) || $_POST['subject'] != $i ? null : 'selected';
?>
<option <?= $sel ?> value="<?= $i ?>"><?= $val ?></option>
<?php } ?>
</select>
Note:- you have to use $_POST['subject'] because method='POST' in form Tag.
You need to foreach through all the options.
Make an array with all the dropdown options, loop through that, and compare with what is stored in $_POST[] & just put the selected attribute on the one which you want to select.
<form action="" method="post">
<legend>Make a selection below</legend>
<br>Field of interest:<br>
<select name="subject">
<?php
$subjectarray = array("Please select a subject", "Wavelenght", "Radioactive Decay");
foreach ($subjectarray as $cc => $subject) {
if ($_POST['subject'] == $cc) {
$selected = 'selected';
} else {
$selected = '';
}
echo '<option value="' . $cc . '" '.$selected.'>' . $subject . '</option>';
}
?>
</select>
</form>
Second option is using $_GET and you are using POST in the form. It'll never work.
The first one looks like it'll throw an error... Not a valid syntax in PHP. In PHP you can't have if inside an echo or print.
What I like to do is(rewriting your foreach):
foreach ($subjectarray as $cc => $subject) {
$selected = $_POST['subject'] == $cc ? ' selected="selected"' : '';
echo '<option value="' . $cc . '"'. $selected .'>' . $subject . '</option>';
}
I want to keep selected values after form submission, as suggested here by #ignacio-vazquez-abrams. I tried this:
<form action="" method="get">
...
foreach ($file_array as $location)
$options .= '<option name="' . $location . (($_GET['name'] == $location) ? 'selected="true"' : '') . '" value="' . $location . '">' . $location .'</option>';
$select = '<select id="location" name="location">' . $options . '</select>';
echo $select;
...
</form>
but I got a notice:
Notice: Undefined index: name in ...
UPDATE
Than I tried this (as was suggested in comments and I how is recommended here):
$options .= '<option name="' . $location . (!isset($_GET['selected']) ? 'selected="selected"' : '') . '" value="' . $location . '">' . $location .'</option>';
with no errors, but the expected result still has not been produced - selected values after form submission are not retained.
How to achieve this?
There's a whole lot missing to actually adding a right answer for this, but i'll try anyway.
PHP locations array:
$locations = array( 'Loc1', 'Loc2', 'Loc3' );
On the form:
<form action="" method="get">
<select name="location">
<?php
foreach ( $locations as $location ) {
echo '<option value="' . $location . '" ' . ( isset( $_GET['location'] ) && $_GET['location'] == $location ? 'selected="selected"' : '' ) . '>' . $location . '</option>';
}
?>
</select>
</form>
First of all there's no way of telling that your posted value is the value name. As i recall there's no name attribute for option, it has to be a select around it which holds the attribute name, and you are putting the selected="true" in the name attribute which you are not properly closing in the first place (and which is non existing).
You are using a strict mode of PHP, therefore you need to check if the GET param is passed (by using isset()) before comparing its value to something.
<form action="" method="get">
<select id="location" name="location">
<?php foreach($file_locations as $location): ?>
<option <?php echo ((isset($_GET['location']) && $_GET['location'] == $location) ? 'selected="selected"' : '') ?> value="<?php echo $location ?>"><?php echo $location ?></option>
<?php endforeach; ?>
</select>
</form>
Also you need to write selected="selected" or just selected instead of selected="true".
In a form after submitting the data, if i want to edit form the data in the form should be from database i have to echo the selected value from the database if data is present else it has to show the options to select the values from another table where these options are present
here is the code
please correct it
<select name="ccname" class="form-control form-color">
<?php
if(isset($storyData)) {
echo '<option value="' . $storyData['ccname'] . '">' . $storyData['ccname'] . '</option>';
}
else{
echo "<option value="">CCname</option>";
foreach(getNames() as $ccname)
echo '<option value="'.$ccname.'">'.$ccname.'</option>';
}
?>
</select>
after working with concatenation operator finally debugged the code
here is the code
that i corrected
<select name="ccname" class="form-control form-color">
``<?php if(isset($storyData))
{
echo "<option value = '" . $storyData['ccname'] . "'>".$storyData['ccname']."</option>";
}
else{
echo "<option value='1'>CCname</option>";
foreach(getNames() as $ccname)
echo '<option value="'.$ccname.'">'.$ccname.'
</option>';
}
?>
</select>
I have a gender array as an example, but I am also using longer select list such as states. I want to capture the option value in a PHP varible that the client side user chooses. Then I can use that captured value in an email message.
function genderList() {
$gender = array('M'=>"Male",'F'=>"Female",);
return $gender;
}
$gender = genderList();
$email_form = '<?php $states = statesList(); ?>
<form class="aw-contact-form" method="post" action="' . get_permalink() . '">
<label for="cf_gender">' . $label_gender . '</label>
<select name="gender" id="cf_gender">
<option selected="selected"></option>';
foreach ($gender as $key => $value) {
$email_form .= '<option value="' . $key . '">' . $value . '</option>';
}
$email_form .= '</select>
</form>
return $email_form;
Any help will be greatly appreciated.
I have this
<select name="method">
<?php
while($m = $sel->fetch(PDO::FETCH_ASSOC)){
echo '<option value="' . $m['type'] . '">' . $m['name'] . '</option>';
}
?>
</select>
And in the html form, in the source, it shows the type and name, and everything works, but when I submit the form, $_POST['method'] is blank, and var dumped is NULL. Would be great if someone could help.
Umm your data isn't inside of a form?
<form method="post">
<select name="method">
<?php
while($m = $sel->fetch(PDO::FETCH_ASSOC)){
echo '<option value="' . $m['type'] . '">' . $m['name'] . '</option>';
}
?>
</select>
</form>
<form name="input" action="your path" method="post">
<select name="method">
<?php
while($m = $sel->fetch(PDO::FETCH_ASSOC)){
echo '<option value="' . $m['type'] . '">' . $m['name'] . '</option>';
}
?>
</select>
</form>