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>';
}
Related
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".
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');
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 am using two selects, the one as the category and the second one as subcategory.
I wish to make the second select menu to be determined by what option was chosen in the second select menu.
Here is the html/php code.
<select name="category" id="category">
<?php
$options = array("Stationary", "Bag", "Paper");
foreach ($options as $option) {
echo '<option value"' . $option . '"';
if(in_array($option, $category)){
echo " Selected";
}
echo ">" . ucfirst($option) . "</option>";
}
?>
</select>
<select name="subcategory" id="subcategory">
<?php
$options = array("Pencils", "Ruler", "Pens");
foreach ($options as $option) {
echo '<option value"' . $option . '"';
if(in_array($option, $category)){
echo " selected";
}
echo ">" . ucfirst($option) . "</option>";
}
?>
<?php
$options = array("Small", "Large", "Paper");
foreach ($options as $option) {
echo '<option value"' . $option . '"';
if(in_array($option, $category)){
echo " selected";
}
echo ">" . ucfirst($option) . "</option>";
}
?>
<?php
$options = array("Notepad", "Plain ", "Colour");
foreach ($options as $option) {
echo '<option value"' . $option . '"';
if(in_array($option, $category)){
echo " selected";
}
echo ">" . ucfirst($option) . "</option>";
}
?>
</select>
I want it so when and you select one option from category it will it show the correct options to be shown in the <select>.
I'm not sure if you can do this, but if anyone has an idea or know how to do this your help would be helpful.
If I understand your question right what you want to do is to select the sub category depending on the category.
1st load all the categories into the category select box.
2nd when the category option is selected retrive the value from select box(category_id) and make an AJAX call to get the sub categories for the selected category.
3rd populate them to the sub category select box.
Updated answer as requested
jQuery Ajax POST example with PHP
If you have any issues let me know
I have a simple html form with select element. To define which option is selected, I add selected as
<option value="10" <?php if($value == '10') {echo 'selected="selected"';}?>>10</option>
<option value="20" <?php if($value == '20') {echo 'selected="selected"';}?>>20</option>
<option value="50" <?php if($value == '50') {echo 'selected="selected"';}?>>50</option>
$value is a variable which comes from PHP codes. This methods seems to be very simple and naive. Is it the best way to do so?
$options = array(10,20,50);
foreach($options as $option) {
$selected = ($value == $option) ? ' selected="selected"' : '';
echo '<option value="' . $option . '"' . $selected . '>' . $option . '</option>';
}
Not a bad way to do it. But why not create the entire thing using programmatic approach (i.e., generate the code using a PHP loop through an array):
$items = array(10, 20, 50);
for ($i = 0; $i < count($items); $i ++) {
echo("<option value='" . $items[$i] . "'");
if ($items[$i] === $value) {
echo(" selected='selected'");
}
echo(">" . $items[$i] . "</option>");
}
Something along the lines of the below should work:
<?
$values = array(10, 20, 30);
foreach ($values as $val) {
?>
<option value="<?=$val?>" <?=($value==$val ? "SELECTED" : "")?>><?=$val?></option>
<?
}
?>
Keep your values in an array and loop over it to generate the HTML. Then you don't need to repeat yourself.
<?php foreach($list as $key => $value): ?>
<option value=<?php echo $value ?> <?php echo $value == $selected? 'selected' : '' ?> ><?php echo $value ?></option>
<?php endforeach ?>
Hope you get the idea.
This is the like i do it:
<?
$values = array(10,20,30,......);
$value = XXX;
echo "<select name='somename'>";
foreach($values as $v)
{
if($v == $value)
{
echo "<option value='$v' 'selected'>$v </option>";
}
else
{
echo "<option value='$v'>$v </ option> ";
}
}
echo "</select>";
?>