Not sure why in practice this would not work, but i am trying to make a page reload after submit, but with multiple <select>'s.
Each has it's own name, but how do I know which was changed on $_POST, without giving each select its own form.
Here is the current code:
<form id="productSelectedForm" action="products.php?filter=true" method="post">
<input type="hidden" name="model" value="<?php echo $item['model']; ?>" />
<ul>
<li>
<label><strong>Select available Grade: </strong>
<select id="gradeSelect" class="propChanged" name="grade" onchange="this.form.submit();">
<?php foreach( $props[ 'grade'] as $g) { ?>
<option value="<?php echo $g; ?>">
<?php echo $g; ?>
</option>
<?php } ?>
</select>
</label>
</li>
<li>
<label><strong>Select available Colour:</strong>
<select id="colourSelect" class="propChanged" name="colour" onchange="this.form.submit();">
<?php foreach( $props[ 'colour'] as $g) { ?>
<option value="<?php echo $g; ?>">
<?php echo $g; ?>
</option>
<?php } ?>
</select>
</label>
</li>
<li>
<label><strong>Select available Storage:</strong>
<select id="storageSelect" class="propChanged" name="storage" onchange="this.form.submit();">
<?php foreach( $props[ 'storage'] as $g) { ?>
<option value="<?php echo $g; ?>">
<?php echo $g; ?>
</option>
<?php } ?>
</select>
</label>
</li>
<li>
<label><strong>Select available Network:</strong>
<select id="networkSelect" class="propChanged" name="network" onchange="this.form.submit();">
<?php foreach( $props[ 'network'] as $g) { ?>
<option value="<?php echo $g; ?>">
<?php echo $g; ?>
</option>
<?php } ?>
</select>
</label>
</li>
</ul>
</form>
Is there any way to do this without using Javascript and individual forms, that i am missing ?
Thanks
Addy
From your given PHP it can be seen that you do not pre-select a certain option for the SELECT elements, so the browser will select the first by default.
Then when the user submits the form you cannot make the distinction between a voluntary, conscious choice for that first item, or whether the user just changed another SELECT and did not do anything with this one.
I would suggest to start every SELECT with a first option that is empty, and has an empty VALUE attribute, like this:
<option value=""><option>
<?php foreach( $props[ 'grade'] as $g) { ?>
<option value="<?php echo $g; ?>">
<?php echo $g; ?>
</option>
<?php } ?>
Then when you process the $_POST data, you will know whether the user made an explicit choice if the value is not empty, for example:
if (!empty($_POST['grade'])) {
// a grade was specified by the user.
}
Keeping State
Although your code does not seem to keep track of previously made selections, you might probably want that, and so you should somehow store those previous selections. Once you are able to do that, you will be able to find out what changed by comparing the previous values with the current submitted values.
You could use session variables for keeping track of the user's choices:
session_start();
// detect change and remember current selections
$changed = array();
foreach (array("grade", "colour", "storage", "network") as $key) {
if (isset($_POST[$key])) {
if (empty($_SESSION[$key]) || $_POST[$key] != $_SESSION[$key]) {
$changed[] = $key;
}
$_SESSION[$key] = $_POST[$key];
} else {
$_SESSION[$key] = "";
}
}
After this code has run you will have in the array $changed the list of items (e.g. ["storage"] ) that have a changed value. Normally, it should not be possible that more than one value changes at a time (since you submit immediately on a change), but who knows...
Then in your form you should make sure the user keeps the previously selected items selected, for example, for grade:
<select id="gradeSelect" class="propChanged" name="grade" onchange="this.form.submit();">
<?php foreach( $props[ 'grade'] as $g) { ?>
<option value=""></option>
<option value="<?php echo $g; ?>"
<?php if ($g == $_SESSION["grade"]) echo " selected "; ?>
>
<?php echo $g; ?>
</option>
<?php } ?>
</select>
Related
I need to generate a list of teams from the cups selection box at the top of my page, this code allows me to run a while loop to generate the correct results for the first selection box, however, when i replicate the code in the second it shows no results?
<form class="seed-form">
<select name="team" required>
<option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
<?php
while ($team = mysqli_fetch_assoc($show_teams)) { ?>
<option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
<?php } ?> <!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
</select>
<select name="team" required>
<option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
<?php
while ($team = mysqli_fetch_assoc($show_teams)) { ?>
<option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
<?php } ?> <!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
</select>
<input type="submit" name="submit" value="Generate" class="submit">
<?php
if (isset($_POST['submit'])) { //checking if submit button was clicked
include_once 'action/dbcon.php';
$cname = $_POST['cupname'];
if (empty($cname)) {
header("Location: tables.php?field=empty"); //return them if fields are empty
exit();
} else {
$sql = "SELECT * FROM teams WHERE cup_name='$cname'";
$show_teams = mysqli_query($conn, $sql);
$numberCheck = mysqli_num_rows($show_teams);
if ($numberCheck < 8) {
header("Location: tables.php?tables=1"); //Take to cup page if there arent enough teams in the cup
}
}
}
?>
The problem is that fetch_assoc() returns the next row in the resultset, unless there are no more left (in which case it returns NULL - see docs).
Your first while loop runs until there are no more results. Therefore when you start the second while loop, there are still...you guessed it: no more results. You already used them up. Therefore the first call to fetch_assoc() in the second loop returns NULL immediately, and so the loop condition is never met, and the loop never executes.
There are two different ways to solve this:
1) Reset the result pointer to the start of the resultset. Somewhere between the two loops, write
mysqli_data_seek($show_teams, 0);
See docs for more info.
2) Read all the data into a PHP array, which you can then loop through as many times as you like using foreach:
<?php
$teams = array();
while ($team = mysqli_fetch_assoc($show_teams)) {
$teams[] = $team;
}
?>
<form class="seed-form">
<select name="team" required>
<option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
<?php
foreach ($teams as $team) { ?>
<option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
<?php } ?><!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
</select>
<select name="team" required>
<option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
<?php
foreach ($teams as $team) { ?>
<option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
<?php } ?> <!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
</select>
In user edit module have a dropdown in which i show options from database.
Now i select a value from dropdown and want to set it as selected.
SO next time when i edit user i can see which was the last value selected.
When i edit a user i select some value from dropdown, and then save the record back to database. Next time when i open that record i want the option i selected last time to be shown selected.
I tried this :
<tr>
<td>Base INI File</td>
<?php
if(isset($_GET['id']))
{
$id=$_GET['id'];
btn_edit_file($id);
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<td>
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value) { ?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>"><?php echo $value->base_ini_filename;
if($value->id == $value->base_ini_filename){echo "selected='selected'";} ?> </option>
<?php } ?>
</select>
</td>
<td>
<?php echo btn_edit('customer/upload_ini/edit_ini_custom/'); ?>
</td>
<script type="text/javascript">
$(document).ready(function() {
$('#base_ini_id').change(function() {
var id = $("#base_ini_id").val();
var url = "/bizrtc/customer/upload_ini/edit_ini_custom/";
$("#edit_link").attr("href",url+ id);
$("#edit_link").attr("target","_blank");
});
});
</script>
</tr>
do this :
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value)
{
?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>"
<?php if($value->id == $user->base_ini_id){echo "selected";} ?>>
<?php echo $value->base_ini_filename;?></option>
<?php } ?>
</select>
Try below code:
<select required name="base_ini_id" id="base_ini_id" class="form-control">
<option value="">Select</option>
<?php foreach($base as $value) { ?>
<option id="emp" class="specialLink" value="<?php echo $value->id;?>" <?php if($value->id == $value->base_ini_filename){echo "selected='selected'";} ?> >
<?php echo $value->base_ini_filename; ?>
</option>
<?php } ?>
</select>
Aside from the incorrect closing tag in HTML, I also would like to note that you might want to double check your condition:
if($value->id == $value->base_ini_filename)
Seems like you are comparing very different thing
where is the user saved value ?
first you should get the user saved value into one variable
you option tag should look like below
<option id="emp" class="specialLink" value="<?php echo $value->id;?>">
<?php
if($value->id == $usersavedvalue){echo "selected='selected'";} ?> ><?php echo $value->base_ini_filename; ?> </option>
I want to used set_value for input where type=text and i have no problem with that.
I am confused to use dropdown value. i am fetch dropdown values from database and i could not understand where i use set_value .
My code:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):
if($item->sport_name==$mySport)
{?>
<option value="<?php echo $item->sport_id; ?>" selected><?php echo $item->sport_name; ?></option>
<?php }else{?>
<option value="<?php echo $item->sport_id; ?>"><?php echo $item->sport_name; ?></option>
<? } endforeach; ?>
</select>
You Can try This:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):?>
<option value="<?php echo $item->sport_id; ?>" <?php if($item->sport_name==$mySport)
{ echo "Selected";} ?>><?php echo $item->sport_name; ?></option>
<? } endforeach; ?>
</select>
As per the docs, you would not use set_value for a select element:
"Permits you to set the value of an input form (text input) or textarea.".
Rather, you would use set_select.
If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).
set_select($field[, $value = ''[, $default = FALSE]])
Parameters:
$field (string) – Field name
$value (string) – Value to check for
$default (string) – Whether the value is also a default one
Returns: ‘selected’ attribute or an empty string
Return type: string
Example:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<option value="one" <?php echo set_select('sport_name', 'one'); ?> >One</option>
<option value="two" <?php echo set_select('sport_name', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('sport_name', 'three'); ?> >Three</option>
</select>
your code should be work correctly, but best way: you can declare var called $selected and make comparision to assign it with selected word inside loop when the value of select == current selected value:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php
foreach($getSport as $item):
$selected='';
if($item->sport_name==$mySport)
{
$selected='selected';
}
?>
<option value="<?php echo $item->sport_id; ?>" <?php echo $selected; ?> >
<?php echo $item->sport_name; ?></option>
<?php endforeach; ?>
</select>
The best way of doing this, while remaining readability would be as follow:
<select class="form-control" name="sport_name" id="sport_name">
<option selected>--Select Sport--</option>
<?php foreach($getSport as $item): ?>
<option <?php if($item->sport_name == $mySport){ echo "selected"; } value="<?php echo $item->sport_id;?>"><?php echo $item->sport_name; ?></option>
<?php endforeach; ?>
</select>
Ofcourse this is thinking you've got your data correctly set as objects or if you have arrays you'd use
<?php echo $item['sport_name']; ?>
I have this problem that the selected value in my dropdownlist is showing twice. What should i do so that it will show only once.
<td>
<select class="span8" style="width:100%" name="from_time[]" id="from_time<?php echo $count;?>" value="start_from" onchange="calculate_by_ajax(this.id);">
<option value="<?php
$start_from=$jobs_result['start_from'];
//$date_arr_to= explode(" ", $start_from);
//$date_to= $date_arr_to[0];
//$time_to= $date_arr_to[1];
$sec="SELECT DATE_FORMAT('$start_from', '%H:%i') as tp";
$sec_exe=mysql_query($sec);
$sec_res=mysql_fetch_array($sec_exe);
?>" selected="selected">
<?php echo $sec_res['tp']; ?></option>
<?php include("list.php"); ?>
</select>
</td>
Got it, instead of modifying one of the available options and marking it as selected, you actually add another option with your code.
You could change list.php to look like this (i.e. an array of all the times)
<?php
$drop_down_values = array('11:35', '11:36', '11:27');
?>
Then your code should look like this:
<?php
include("list.php");
$start_from=$jobs_result['start_from'];
$sec="SELECT DATE_FORMAT('$start_from', '%H:%i') as tp";
$sec_exe=mysql_query($sec);
$sec_res=mysql_fetch_array($sec_exe);
?>
<td>
<select class="span8" style="width:100%" name="from_time[]" id="from_time<?php echo $count;?>" value="start_from" onchange="calculate_by_ajax(this.id);">
<?php foreach ($drop_down_values as $value) {
if ($value == $sec_rec) {
echo "<option selected=\"selected\">$value</option>";
} else {
echo "<option>$value</option>";
}
}
?>
</select>
</td>
I have a little problem.
I'm searching the internet now for quite some time to find a proper solution but I was not successful so far.
This is what I want :
First, I choose Category
Then, in second selection contain all the store result from category selection.
this is my first drop down list which is contain Category :
<select name="category" class="form-control" id="select1">
<option value="-1"> - Choose One -</option>
<?php
$StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
$stores = $StoreCategoriesAPIAccessor->getStoreCategories();
foreach ($stores as $store):
?>
<option value="<?php echo $store->getStoreCategoryId(); ?>"><?php echo $store->getCategory(); ?></option>
<?php endforeach; ?>
</select>
this is my second drop down list :
<select name="category" class="form-control" id="select2">
<option value="-1"> - Choose One -</option>
<?php
$StoreAPIAccessor = new StoreAPIService(GuzzleClient::getClient());
$stores = $StoreAPIAccessor->getStores();
foreach ($stores as $store):
?>
<option value="<?php echo $store->getStoreId(); ?>"><?php echo $store->getStoreName(); ?></option>
<?php endforeach; ?>
</select>
Anyone know how to implement dynamic drop down list for this case ?
Well first of all I'd like to ask that you separate business logic from view logic as much as possible, so I will do that in this answer.
Secondly, the 2nd dropdown box logic that you have does not contain anything that would retrieve stores for a given category, I will therefore make some assumptions and you can adjust based on that.
<?php
$StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
$categories = $StoreCategoriesAPIAccessor->getStoreCategories();
if (!empty($_GET['category'])) {
$category_id = $_GET['category'];
$StoreAPIAccessor = new StoreAPIService(GuzzleClient::getClient());
$stores = $StoreAPIAccessor->getStores($category_id); // Assumption, the call to getStores() accepts category_id as a parameter
} else { // Optional as you don't need to declare variables in PHP, but its better practice to do so
$category_id = null;
$stores = array();
}
?>
<select id="select_category" name="category" class="form-control" onchange="window.location='?category=' + this.value">
<option value="">- Choose One -</option>
<?php foreach ($categories as $category): ?>
<option value="<?php echo $category->getStoreCategoryId() ?>"><?php echo $category->getCategory() ?></option>
<?php endforeach ?>
</select>
<select id="select_store" name="store" class="form-control"<?php echo $category_id == null ? " disabled='disabled'" : "">
<option value="">- Choose One -</option>
<?php foreach ($stores as $store): ?>
<option value="<?php echo $store->getStoreId() ?>"><?php echo $store->getStoreName() ?></option>
<?php endforeach ?>
</select>