I have a list that allows multiple selections to be made and then submitted. What I now need to do is populate the select box so that the values submitted remain in the following page.
I'm passing the variable in the url and can list them using the following:
foreach ($_GET['sector'] as $selectedOption)
My thinking is to store the results in an array and then when the list is created loop through with the following:
if (in_array($optionvalue, $selectedOption))
{
echo "<option value='".$optionvalue."' selected='selected>".$optionvalue."</option>";
}
else
{
echo "<option value='".$optionvalue."'>".$optionvalue."</option>";
}
The above works but only for whatever the last selected item is.
Is this the correct way to do this or is there a simpler method?
Related
dropmenu is <select name="dropbox"> with 3 options - admin, activate, delete. The below snippet of code shows if the activate <option> is selected and submitted by the submit button then echo etc. I have a <select name="dropbox"> on every row for each user. My code only works if i change the last drop box.
if(isset($_POST['submit']))
{
if(isset($_POST['dropmenu']) && $_POST['dropmenu'] == 'activate')
{
echo 'is activated';
}
else{
echo 'fail';
}
}
Is there a way i can use foreach drop box with a value selected?
One of the possible ways is to connect the name of the select element with user id like
<select name="dropbox_[userId]">
In this case you simply know how to build an input name for checking it's value in $_POST table.
If you don't interate by users on submit code then you can use a regular expression to get an inforamtion abut user connected with element.
I'm creating a control panel and having check boxes like the following
The value of $myboxid is the checkbox id, example: cb1 and the name is just the value eg Name of place: London. It grabs this information from my database
<input id='".$myboxid."' name='cplace[]' checked type='checkbox' value='".$box."'><label for='".$myboxid."'>".$boxname."</label>
What I'm trying to do is check which box is selected out of the multi box selection. I can get which boxes are selected and for that to output the value, my problem is I also need it to tell me which boxes are not selected.
My Form method is POST, my php back end is the following
$lname=$_POST['cplace'];
if(isset($_POST['cplace'])) {
foreach($lname as $place){
echo $place." CHECKED <BR>";
}
}
I'm trying to get this to output the checkbed boxes and the ones which are not selected.
Thanks for the help!
Well you can check the value of the checkbox in the POST array.
Simply print all the checkbox from the db and then check the value
In one line you can do like this
Inside your db loop
$isChecked=(in_array($_POST['cplace'], $box)) ? "checked" : "";
echo "<input id='".$myboxid."' name='cplace[]' ".$isChecked." type='checkbox' value='".$box."'><label for='".$myboxid."'>".$boxname."</label>";
Please, I need generate selectable list based on value from other list. I have function for generating first list.
function getCatList ()
{
$result = getCategory ();
echo "<select id=\"catList\">";
echo "<option value=\"\" selected=\"selected\">Select category</option>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value=\"".$row['codename']."\" onclick=\"<?php if($options==".$row['codename'].") echo 'selected=\"selected\"'; ?>".$row['visible_name']."</option>";
}
echo "</select>";
}
and I need generate next list based on selected value from this list.
I try set variable when onclick or some other attributes but without result.
I suppose it's caused because PHP is server-side language.
Can you help me? I would like avoid JS if possible. Is there any option how can I do this?
You cannot launch PHP that way.
If you want to (without refresh) generate a list from a selected value you need to use Javascript. The easy way is to use Ajax, but you can make something even with Javascript alone (in the same page).
You can for example save on loading some arrays of data, and then on select change the list or what you want.
okay, im new in this site also new in php and can't get the logic on this,
i have a product page that shows the name, quantity and an add to cart button in each row of product
i made this just cutted of some code
while($showProducts = mysql_fetch_array($products))
{
$currenQuantity = $showProducts['current_quantity'];
$prodid = $showProducts['product_id'];
echo"<select name='quan'>";
for ($x=0;$x<=$currenQuantity;$x++)
{
if($currenQuantity != 0)
{
echo "<option value=$x> $x </option>";
}
}
echo"</select><br/>";
}
now the problem is every time i tried to get the value by using $_POST['quan'] the value that i always get is the default value 1 even i select a different value of quantity of a certain product, and i'm blanked with ideas.
You can't use the same name for an input/select field in a form.
You have to specify a diffrent name or create an indexed array:
<select name="quan[$prodid]">
You can acces it via
$_POST['quan'][$prodid]
Just a wild guess: has each of your product rows a select named 'quan'?
Then you're getting the last select named 'quan' in your POST data.
Prefix each of the quantities selects with name of the product, or some kind of a (scrambled) id.
Additionally:
If you don't want to have a 0 in your select, don't start the for loop with 0
for ($x=1;$x<=$currenQuantity;$x++)
if the current quantity is 0, the for loop doesn't get executed.
If you have more selectboxes with the same name (quan) in one form, you are getting value of the last one.
You should change the selectbox definition to:
echo"<select name='quan[" . $prodid . "]'>";
then you can access the values using:
$_POST["quan"][$some_product_id]
$_POST["quan"][$another_product_id]
I have a form that has fixed fields as well as dynamic fields created from a database. the fields could be different every time and they are dropdowns with options like color size etc.I could name the dropdowns like this:
name="options[]"
and then do something like:
foreach($options as $option) {
//add to db
}
however that only gives me the values and I need to know the optionID as well
I could do somethign like:
name="<? echo $optionID; ?>"
for the dropdown but I don't know what optionID's were included in the form when I try to process the submitted data. how do I pass both the optionID and it's selected value when adding fields to a form dynammically?
You can make the array multidimensional. Something like this might work for you:
name="options[<?php echo $optionID;?>][]"
foreach ($_POST['options'] as $ID) {
foreach ($ID as $value) {
// stuff
}
}