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.
Related
is it possible to sends two values in
<option value="<?=$halls['id']?>">
I want to send like this
<option value="<?=$halls['id']?><?=$halls['rang_from']?>">
here is the code.
<?php
//var_dump($_POST);
$location=$_POST['location'];
$hall_query=mysqli_query($connection,"select * from halls where location='$location' and status='enabled'");
while($halls=mysqli_fetch_assoc($hall_query))
{
?>
<option value="<?=$halls['id']?>"><?=$halls['rang_from']?> To <?=$halls['rang_to']?></option>
<?php
}
?>
use separator like '|#|'
for example
const SEPARATOR = '|#|';
in form:
<select name="select_name">
....
<option value="<?=$halls['id'] . SEPARATOR . $halls['rang_from']?>"><?=$halls['rang_from']?> To <?=$halls['rang_to']?></option>
....
</select>
and when receive:
list($id, $rang_from) = explode(SEPARATOR, $_REQUEST['select_name']);
echo "id is $id, rang_from is $rang_from";
id is 21, rang_from is home
Place both values into the value attribute, separated by "/"
<option value="<?=$halls['id'] . '/' . $halls['rang_from'] . '/' . $halls['rang_to']?>"><?= $halls['rang_from']?> To <?=$halls['rang_to']?>
</option>
And When if you want to Recieve It Then Write
list($hall_id, $rang_from, $range_to) = explode('/', $_REQUEST['hall']);
for Checking that values comes or Not... Write
echo $rang_from; exit;
you can do by add two values into the one value by separate like this in your server side explode the value like this explode('##',$value) by explode function
<option value="<?=$halls['id'].'##'.$halls['rang_from']?>">
<?=$halls['rang_from']?> To <?=$halls['rang_to']?></option>
passing option value as an JSON array
<option
value="<?= json_encode(['id' => $halls['id'], 'rang_from' => $halls['rang_from']]) ?>">
<?=$halls['rang_from']?> To <?=$halls['rang_to']?>
</option>
Then use json_decode to fetch the posted array.
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'];
I'm working on a php form with some validation rules.
If some of the fields are left blank that are require the form will report and error message and ask the user to complete the field.
The form currently keeps the values of the $_POST data so that if errors occur the data which is in the field remains.
I am having a problem with two fields(drop down lists) which are populated with data from a database.
If I complete these fields but there is a error elsewhere the form displays with the values in the drop down list but when I correct the errors and try submit the form it tells me that the drop down list fields are empty.
Here is the code for them
<! Drop Down Menu to get student names from database !>
<SELECT NAME=studentName >
<OPTION VALUE=0 selected="selected" >
<?php if(isset($_POST['studentName'])) echo $_POST['studentName'];?>
<?php echo $options1?>
</SELECT>
Any idea's why this is happening?
For starters, <option value=0 selected="selected"> means that option 0 will always be selected.
You could do something like the following, excuse me if this snippet has bugs, but it should give you a general idea of how you could achieve what you want to:
<?php
$myOptions = array(
'0' => ' --- nothing selected ---',
'1' => 'Apples',
'2' => 'Bananas',
// ...
);
?>
<select name="studentName">
<? php
foreach($myOptions as $key => $value ) {
$selected = isset($_POST['theOption']) && $_POST['theOption'] == $key ? 'selected="selected"' : "";
echo '<option value="' . $key . '" '. $selected . '>' . $value . "</option>";
}
?>
</select>
You can cleanup the above a bit using the alternative PHP syntax:
http://php.net/manual/en/control-structures.alternative-syntax.php
This isn't part of your bug, but just a good practice consideration, some of your HTML tags and their attributes are uppercase, and it's common nowadays to use lowercase. It's more standard this way.
You shouldn't echo $_POST['studentName']; you should check it against the value of each of your options (which should be an array in PHP, which you convert to a list of <option>s in a foreach loop), and set the selected attribute if it matches.
On a sidenote, your HTML is invalid; some attributes are not quoted, and a comment should be in <!-- ... -->, not <! ... !>.
I have a php file with 2 arrays in it
//script containing all the rublics and subrublics
$rublic[0]='rublic1';
$rublic[1]='rublic2';
$rublic[2]='rublic3';
$rublic[3]='rublic4';
$rublic[4]='rublic5';
$subrublic[0]='subrublic1';
$subrublic[1]='subrublic2';
$subrublic[2]='subrublic3';
$subrublic[3]='subrublic4';
$subrublic[4]='subrublic5';
?>
The elements of these arrays are shown in the drop-down box. What I need to do is to grab the element which the user chose from the box and write the index number of the choice selected into a database field. How could I do that?
I will post my code here even though I realize that my approach to this problem is completely wrong from the start :(
//add the index number of the rublic and the subrublic to the db
include('rublics.php');
if(isset($_POST[' article_type']) && ($_POST['article_type'] != '0')){
$rublic_selected = $_POST['article_type'];
for($count_rublic=0; $count_rublic<=10; $count_rublic++){
if($rublic_selected == $rublic[$count_rublic]) {
$rublic_selected = $count_rublic;
}
if($rublic_selected == $subrublic[$count_rublic]){
$rublic_selected = $count_rublic;
}
}
} else {
echo 'You did not make the selection. Please choose the type of the article.';
}
Your dropdown can/should use the index numbers for the value attribute on the option elements. Ie:
<select id="article_type" name="article_type">
<option value="0">rublic1</option>
<option value="1">rublic2</option>
<option value="2">rublic3</option>
<option value="3">rublic4</option>
<option value="4">rublic5</option>
</select>
Then when the form is POSTed to your PHP script, you will already have the correct index number to write to the database.
Try this, it should be a good start:
$rublic[0]='rublic1';
$rublic[1]='rublic2';
$rublic[2]='rublic3';
$rublic[3]='rublic4';
$rublic[4]='rublic5';
$subrublic[0]='subrublic1';
$subrublic[1]='subrublic2';
$subrublic[2]='subrublic3';
$subrublic[3]='subrublic4';
$subrublic[4]='subrublic5';
//$user_input = 'rublic3';
$user_input = 'subrublic4';
$options = array('rublic', 'subrublic');
foreach($options as $option_key => $option_value)
{
foreach($$option_value as $key => $value)
{
if($value == $user_input)
{
echo 'found it at ', $option_value, ' ', $key;
break;
}
}
}
Basically, I loop through both arrays until I find the string that matches the correct user input and return the name of the array it was found in (rublic or subrublic) plus the index of that array.
here I have stupid question, hope you can help me.
I create a menu using Select element and option like this:
<option selected="selected">Select type...</option>
<option value="1">Doctor</option>
<option value="2">Patient</option>
and every time I need to pick one value from this menu and use the submit button next to it to transfer data.
But every time the page refreshed, this menu will reveal: Select type...
I want it to reveal the value I chose last time, but don't know how.
Many thanks in advance!!
You'll want to move that selected="selected" onto the selected option.
Doing so in PHP isn't too rough. Just check the $_POST or $_GET (however you sent the form) value for your select box, such as $_POST["selectBox"] for each value down the list. When you find a match, echo out the selected="selected" string there. If the value was empty, output it on your default value.
The easiest way to achieve this is to populate the <select> options in an array, then loop through it to display the <option> list and mark them as selected is the $_POST variable matches the correct value:
<?php $myselect = array(1=>'Doctor', 2=>'Patient'); ?>
<select name="myselect">
<option>Select type...</option>
<?php foreach ($myselect as $value => $label): ?>
<option value="<?php echo $value; ?>"<?php if (isset($_POST['myselect']) && $_POST['myselect'] == $value) echo ' selected'; ?>>
<?php echo $label; ?>
</option>
<?php endforeach; ?>
</select>
<select name="myselect">
<?php
$myselect = array('Select type...','Doctor','Patient');
for($i=0; $i<=2; $i++){
echo "<option value=\"{myselect[$i]}\"";
if (isset($_POST['myselect']) && $_POST['myselect'] == $myselect[$i]){
echo 'selected=\"selected\"';
}
echo ">{$myselect[$i]}</option>";
}
?>
</select>
You have to use the server-side language of you choice to store the selected value in a database, xml or text file.
Edit : I think I may have misunderstood your question.
There are a few ways to do this.
On submit you can save that value as a $_SESSION value and use that to set the select on page load.
Using Javascript you can either set a cookie on change or alter the url to add a parameter (url?selecttype=1) and set that on page load using PHP.
There's a good use of cookies in JS on quirksmode: http://www.quirksmode.org/js/cookies.html
You need to change which one is selected to match the request....
function create_select($properties, $opts)
{
$out="<select ";
foreach ($properties as $propname=>$propval) {
$out.=" $propname='$propval'";
}
$out.=">\n";
foreach ($opts as $val=>$caption) {
$out.="<option value='$value'";
if ($_REQUEST[$properties['name']]==$val) $out.=" SELECTED";
$out.=">$caption</option>\n";
}
$out.="</select>";
return $out;
}
print create_select(array('name'=>'direction',
'id'=>'direction',
'class'=>'colourful',
'onChange'=>''),
array('N'=>'North',
'S'=>'South',
'E'=>'East',
'W'=>'West'));