I am a beginner and I have written a piece of code to use an array values inside a select form, but I don't know how to get the chosen value.
What I have :
$list = '<select><option>' .implode('</option><option>',$apn).'</option></select>';
Now I would like to get the name of the chosen value but I don't know how.I tried to add
<select name="test">
to my select form and get it with
echo $_POST['test']
but it does not work.
Thank you !
<?php $myArray = array(1 => 'red', 2 => 'blue'); ?>
<select name="theName">
<?php
foreach ($myArray as $key => $value) { ?>
<option value="<?php $key ?>"><?php echo $value ?></option>
<?php } ?>
</select>
Basically, you are looping through the option value with a foreach statement and assigning the key as the value.
You then get the chosen value with name 'theName'... echo $_POST['theName'];
Related
Let's say I have a photo upload system where the user have to set a category for each album to get the basics for a nice and clean search functionality. But if an user is changing now a value like this:
<select>
<option value="">Choose a Category</option>
<option value="Holiday">Holiday</option>
</select>
to this:
<select>
<option value="">Choose a Category</option>
<option value="Holiday">Something Stupid</option>
</select>
is "something stupid" entered into the database.
That's why I have to do a server side check. But I don't know how to get all the correct values of the option fields to compare it with the posted value.
So my first considerations were the following:
PHP:
// Get all values of the option fields
// Push all the values into an array
if (in_array('foo', $array)) {
// foo is in the array
}
Thank you for helping.
Ok, so I think I guessed what you tried to tell.
You should not have the tags hardcoded in your list.php file, but instead have an array there. That way, you can use it both for generating the select field, but also for the verification. However, generally a database table with the categories would be preferable.
path/list.php
return array(
'1' => 'Name of Ctg 1',
'2' => 'Name of Ctg 2'
);
Here you generate the select
<select name="whatever">
<?php
$options = include('path/list.php');
foreach($options as $id => $name) {
echo '<option value="' . $id . '">' . $name . '</option>';
}
?>
</select>
And how to verify it then in the "target" page
$options = include('path/list.php');
if(!array_key_exists( $valueFromUser, $options ) ) {
// invalid option
}
When the data is posted from the page containing the select tag, it will be in the $_REQUEST array, if you run this php in catcher php page:
foreach ($_REQUEST AS $key => $value) echo "$key = $value <br>";
you will see the results from your list.php.
I'm having a contact form field named, addons with select options
it's code is
<?php foreach ($addons as $options) { ?>
<option value="<?php echo $options->addon_name; ?>"><h5><?php echo $options->addon_name; ?></h5></option>
<?php } ?>
Every addon have it's unique price. It can be fetch through $options->addon_price; I added only addon name in contact form so i'm getting only it's name once user submits form. Likewise, I need to get that addon price too without creating new field and asking users to select price in dropdown.
Any ideas to do this ?
You can use the following trick:
<?php foreach ($addons as $options) { ?>
<option value="<?php echo $options->addon_name.','.$options->addon_price; ?>">
<h5><?php echo $options->addon_name; ?></h5>
</option>
<?php } ?>
Add the price with the name in value of dropdown and then after submitting the page you can get the string with name and price and then you can split it with the , by using the explode function like it:
$pieces = explode(",", $str);
And you can use the price and name by $name = $pieces[0] and $price = $pieces[1].
You Can use
<?php foreach ($addons as $options) { ?>
<option value="<?php echo $options->addon_price; ?>"><h5><?php echo $options->addon_name; ?></h5></option>
<?php } ?>
and u can fetch the price form the drop down
you can try this you will be able to get the price and it will show addon_name to the user.
<?php foreach ($addons as $options) { ?>
<option value="<?php echo $options->addon_price; ?>"><h5><?php echo $options->addon_name; ?></h5></option>
<?php } ?>
It sounds like you need a unique ID to identify the addons. If it's a static array that doesn't change, you can even use the array keys for that purpose, but I would add an addon_id field to the objects.
The addons array would be something like this:
$addons = array(
1 => $addon1,
2 => $addon2,
3 => $addon3,
... etc
);
Your select options can have the array keys as values:
<?php foreach ($addons as $key => $options) { ?>
<option value="<?php echo $key; ?>"><h5><?php echo $options->addon_name; ?></h5></option>
<?php } ?>
Then when you're processing the selected addon, use the key to access the appropriate addon, like so:
$selected_addon = $addons[$_POST['addon']];
, obviously substituting the correct form element name, and sanitizing the input first, this is just to give you an idea.
I'm reading a lot of discussion here to find a solution to my issue (for example: how to pass the id value of a select list in a form using post or get?) but for me is not working.
I have a table with a form inside every row:
<select name="form_type" class="textselect">
<?php foreach($SContentType as $key => $val){ ?>
<option value="<?php echo $key ?>" <?php if($thisObjs[$k]['type']==$key) echo "SELECTED" ?>><?php echo $val ?></option>
<?php } ?>
</select>
When I try to get the $_POST value I have all the variables except the Select.
Here is my $_POST print
Array (
[a] => ACT_ARTICLES
[p] => SUBACT_MODIMAGE
[form_id] => 6454
[obj_id] => 8754
[form_description] => )
As you can see I have all the variables of the form except form_type, the variable of the select.
By definition of the select boxes, your code should be working fine. Is it placed within <form> and </form> tags?
It may have something to do with your multiple table rows. Try doing:
<select name="form_type[]">
Does it show up now?
As you tagged the question as jquery, here is a jquery way to do it. The .. is for any specific class or id you want to give for more specific DOM element.
$(".textselect .. option:selected").text()
I have a form that looks like this.
<form action="index.php" method="post">
<select name="dropdownOption">
<option value="First Choice +200">First Choice</option>
<option value="Second Choice +300">Second Choice</option>
</select>
<p><input type="checkbox" value="Rocket +100"> Rocket</p>
What I want to do is loop through every value sent in the _POST and then sum up every number after the '+' sign into a variable in the index.php file.
So for example if they chose the 'Second Choice' and ticked the 'Rocket' Checkbox it should add up to 400 (300+100).
In this example there is only two different options but in the final application there will be 20-30 different things that can vary which is why looping is necessary.
Also How can I loop through every element that is not hidden (so exclude hidden form elements).
foreach ($_POST as $post) {
$num = explode('+', $post);
if (is_numeric((int) end($num)) {
$sum += end($num);
}
}
If you need negative values you can just make your option look like this:
<option value="Second Choice +-300">Second Choice</option>
I'm considering you are storing the values sent using _POST in an array.
$total = 0;
foreach($selections as $sel){
$sep = $explode('+', $sel);
$value = (int)$sep[1];
$total += $value;
}
Why wouldn't you drop the text in the values, and keep only numeric data?
The values aren't visible anyway, but in your code, you can easily map the numeric values to the text.
In other words, you can have something like this:
$choices = array(
'1' => array('value' => '100', 'text' => 'First choice'),
'2' => array('value' => '150', 'text' => 'Second choice'),
'3' => array('value' => '250', 'text' => 'Third choice'),
);
And display the data like this (or any other way, but having the ids of choices in the value field):
<form action="index.php" method="post">
<select name="dropdownOption">
<?php foreach($choices as $id => $choice): ?>
<option value="<?php echo $id ?>"><?php echo $choice['text']; ?></option>
<?php endforeach; ?>
</select>
</form>
And then having the initial array, you can easily sum up all the data:
$sum = 0;
foreach($selected as $id){
$sum += $choices[$id]['value'];
}
And display data to the user too:
<?php foreach($selected as $id): ?>
<p>You chose <?php echo $choices[$id]['text']; ?></p>
<?php endforeach; ?>
This is a way better approach as it gives you a lot of flexibility.
I am using the array below to echo a HTML select box but I haven't been able to figure out how to do the loop using the unique values:
sid evid
13 1
14 1
15 2
16 3
I am trying to loop through the above array by the unique values stored in 'evid' so I can create a select box which groups by 'evid'. At the moment, my loop produces 4 select boxes when it should only produce 3. sid 13 and 14 is outputting a select box each since my code isn't grouping them but I just can't get my head around how I get these two values to group during the loop.
Anyone have any suggestions?
EDIT
Code I'm using:
<?php while ($s = mysql_fetch_array($sessions)){ ?>
<?php foreach ($evs as $ev){
if ($ev['evid'] == $s['evid']){
?>
<form action="" method="">
<fieldset>
<ul>
<li>
<label></label>
<select name="sessions[]">
<option name="sessions[]" value="<?php echo $s['sid']; ?>"><?php echo $s['sname']; ?></option>
</select>
</li>
</ul>
</fieldset>
</form>
<?php } // end if match ?>
<?php } // end foreach ?>
<?php } // end while ?>
If I understand well what you need, and assuming the arrays are sorted, have a try with:
$sid = array(13, 14, 15, 16);
$evid = array(1, 1, 2, 3);
$current_evid = $evid[0];
echo "<select .....>\n";
for ($i=0; $i<count($evid); $i++) {
if ($current_evid != $evid[$i]) {
echo "</select>\n";
echo "<select ...>\n";
$current_evid = $evid[$i];
}
echo "<option value=".$sid[$i].".....\n";
}
echo "</select>\n";
output:
<select .....>
<option value=13.....
<option value=14.....
</select>
<select ...>
<option value=15.....
</select>
<select ...>
<option value=16.....
</select>
Feed your array into array_unique()
Try using PHP's array_unique - details here: http://php.net/function.array-unique
Or - if you're using something like MySQL to get the data, try using GROUP BY or DISTINCT in your SELECT statement.
EDIT per comment:
If you want/need to keep your original array, just copy the array, and use array_unique on the copy. Then, use the copy to create the select dropdown.
Alternate solutoin:
Keep a $values_written array.
In your loop, before you write the <option>, check that it's value is not already in the $values_written (using PHP's in_array - details here)
Then write the <option>.
Then add it's value to the array (using PHP's array_push - details here)