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.
Related
Is it possible to assign multiple values to select option drop down lists? I need to retrieve multiple pieces of data from each drop down when they are selected and I have only been able to get the "name" and an "id". The name is displayed for the user to select however it is the ID that is passed to be processed. Here is my code:
<?php
$att_1 = $att_1;
mysql_connect("xx.xx.xx.xx","xxxxxx","xxxxxx");
mysql_select_db("dezanjow_cf");
$sql=mysql_query("select id, name from model");
if(mysql_num_rows($sql)){
$select= '<select name="model">';
$select.='<option value="default">Select Model</option>';
while($rs=mysql_fetch_array($sql)){
$select.='<option value="'.$rs['id'].'">'.$rs['name'].'</option>';
}
}
$select.='</select>';
echo $select;
?>
This produces html that looks like:
<select name="model">
<option value="default">Select Model</option>
<option value="5">GH20</option>
<option value="6">GH21</option>
<option value="7">GH22</option>
</select>
I wish to display them like this (as example):
<select name="model">
<option value="default">Select Model</option>
<option value="5","abc">GH20</option>
<option value="6","def">GH21</option>
<option value="7","ghi">GH22</option>
</select>
Thus when the data is passed onto the next php script, I can use both data "5" and "abc" when the user selects "GH20". I have not seen much on Google about this and don't even know if this is possible. Please let me know if I am asking for the impossible! Many Thanks, Nick
Hey you cannot have multiple values like that because post or get will return just first value which in this case is 5
<option value="5","abc">GH20</option>
but you can make your value looks like this
<option value="5,abc">GH20</option>
and then in php script you can separate values by using explode function
explode(',', $_POST['model'])
which will return array that you can use
array( 0=> 5, 1=>abc)
Its better if you serialize your data then at next php script unserialize it.
$data=array($rs['id'], "abc"); //data to be stored
$serialized_data=serialize($data); //serialize data
$select.='<option value="'.$serialized_data.'">'.$rs['name'].'</option>';
IMPORTANT
//unserialize information
$unserialized_data=unserialize($serialized_data);
this will output the same array array('SOMEID', "abc")
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 have a standard select box in my php form for my website containing a list of counties in the UK, an example of which is shown below:
<p class='form_title'>County<br>
<select id="county" name="county">
<optgroup label="England">
<option>Bedfordshire</option>
<option>Berkshire</option>
<option>Bristol</option>
....
<option>Wiltshire</option>
<option>Worcestershire</option>
</optgroup>
<optgroup label="Wales">
<option>Anglesey</option>
...
<option>Radnorshire</option>
</optgroup>
<optgroup label="Scotland">
<option>Aberdeenshire</option>
...
<option>Wigtownshire</option>
</optgroup>
<optgroup label="Northern Ireland">
<option>Antrim</option>
...
<option>Tyrone</option>
</optgroup>
</select>
</p>
Once a user has submitted details, they can then edit them.
I therefore need a way to have selected the option that they previously chose, given that I have the option they chose saved in word form, such as Bedfordshire.
I know I need to add the word selected to one of the options, but I was wondering if there was a better way to do it than a massive case statement.
Thanks
You should set an option value for each option to track the value.
Also you should be generating that from a database and then you can set selected based on a $_GET or $_POST:
for example:
foreach($county as $name){
$selected = '';
if($name == $_GET['name']){
$selected = ' selected="selected"';
}
echo '<option value="'.$name.'"'.$selected.'>'.$name.'</option>';
}
this doesn't include your optgroup but that also should come from database.
Try something like this:
foreach($places as $place) {
$selected = $prevSelectionId == $place['id'] ? " selected='selected' " : "";
echo "<option $selected value='".$place['id']."'>".$place['name']."</option>";
}
</select>
I want to get text inside the <option> tags as well as its value.
Example
<select name="make">
<option value="5"> Text </option>
</select>
I used $_POST['make']; and I get the value 5 but I want to get both value and the text.
How can I do it using PHP?
In order to get both the label and the value using just PHP, you need to have both arguments as part of the value.
For example:
<select name="make">
<option value="Text:5"> Text </option>
</select>
PHP Code
<?php
$parts = $_POST['make'];
$arr = explode(':', $parts);
print_r($arr);
Output:
Array(
[0] => 'Text',
[1] => 5
)
This is one way to do it.
What about this? I think it's the best solution because you have separated fields to each data. Only one hidden field which is updated at each change and avoids hardcoding mappings.
This inside HTML:
<select name='make' onchange="setTextField(this)">
<option value = '' selected> None </option>
<option value = '5'> Text 5 </option>
<option value = '7'> Text 7 </option>
<option value = '9'> Text 9 </option>
</select>
<input id="make_text" type = "hidden" name = "make_text" value = "" />
<script type="text/javascript">
function setTextField(ddl) {
document.getElementById('make_text').value = ddl.options[ddl.selectedIndex].text;
}
</script>
This inside PHP:
<?php
$value = $_POST["make"];
$text = $_POST["make_text"];
?>
set the value of text to the value of the option tag, be it through static HTML markup or even if it's being generated by a server side script. You will only get the value attribute through POST
Another option however, on the server side, is to map the value ("5"), to an associative array, i.e.
<?php
$valueTextMap = array("5" => "Text");
$value = $_POST['make']; //equals 5
$text = $valueTextMap[$value]; //equals "Text"
?>
You'll need to include that Text in the value to begin with (e.g.: <option value="5_Text"> Text </option> and then parse, or...
You could use javascript on the page to submit the text as another parm in the POST action.
You can use simple dom
<?php
include('/mnt/sdb1/addons/simplehtmldom/simple_html_dom.php');
$html = file_get_html('tari.html');
$opt = array();
foreach($html->find('option') as $a) {
$opt[] = $a->value;
}
print_r($opt);
?>
I have always used a very elegant solution, similar to the ones already presented, which does not require a lot of additional code.
HTML
<select name="make">
<option value="1:First Option">First Option Text</option>
<option value="2:Second Option">Second Option Text</option>
<option value="3:Third Option Text">Third Option Text</option>
</select>
PHP
$value = split(':', $make)[0];
$text = split(':', $make)[1];
Benefits of this method
Yes, there are definitely similarities to serialworm's answer, yet we minimize the code in our PHP block by inconspicuously converting to an array and picking the element required right away.
In my case, I use this exact short-hand code in a contact form where this one-liner (to get the selected department name) is critical to keeping the code looking clean.
If I have a line like this,
<option value="someval">somval</option>
how can I position the cursor after the last quotation of value and put something like abcdef?
So the output would be
<option value="somval" abcdef>somval</option>
with PHP?
I want to do this dynamically and I can't figure out how to do it. I'm looking at strpos(), but I don't see how it can be done. I'll be posting a bunch of option tags into a textbox and code will be generated. so I'll have a lot of option fields.
#martin - Say I have a huge dropdown and each option lists a country that exists. Rather than having to manually type out something like this:
$query = $db->query("my query....");
while($row = $db->fetch($query)) {
<select name="thename">
<option value="someval" <?php if($row['someval'] == 'someval') { print "selected"; } ?> >someval</option>
<option value="someval" <?php if($row['someval'] == 'someval') { print "selected"; } ?> >someval</option>
<option value="someval" <?php if($row['someval'] == 'someval') { print "selected"; } ?> >someval</option>
... Followed by 100 more, because there are a lot of locations to list.
</select>
How can I post all the options I have into a textbox and have the above code automatically generated to save a lot of time?
Using your example you would do:
while($row = $db->fetch($query)) {
printf('<option value="someval"%s>someval</option>',
($row['someval'] == 'someval') ? ' selected="selected" ' : '');
}
This would go through the rows and output an option, replacing the %s with the attribute selected="selected" if $row['someval'] is equal to someval. However, the above is rather pointless, because all option elements will have the same value and text, so try
while($row = $db->fetch($query)) {
printf('<option value="%s"%s>%s</option>',
$row['country-code'],
($row['country-code'] === $selection) ? ' selected="selected" ' : '',
row['country-name']);
}
With $selection being anything you want to compare against. Replace the keys in $row with appropriate keys from in your database.
Note: The usual disclaimers about securing your output apply
You could capture (value=".+?") and replace it with $0 abcdef.
<?php
$string = '<option value="someval">someval</option>';
print preg_replace("/(value=\".+?\")/i", "$0 abcdef", $string);
?>
Which outputs the following:
<option value="someval" abcdef>someval</option>
With PHP, you can generate a whole string with any text you wish. Where do you have your original string? In a variable or a text file?