how to echo text not value - php

I am attempting to echo the name of a selected dropdown rather than its value. I understand that echoing a dropdown's value can be achieved by implementing something such as:
$no2 = $_POST['vehicleStyle'];
echo $no2;
where vehicleStyle is the name.
this is my code
<select name="vehicleStyle">
<option value="2">Volvo</option>
<option value="3">Saab</option>
<option value="4">Fiat</option>
<option value="5">Audi</option>
</select>
If I add
$no2 = $_POST['vehicleStyle'];
echo $no2;
to my code, I get either 2,3,4 or 5 (whichever is selected). How can I echo the dropdown name, either volvo, saab , fiat or audi?
NOTE: i use this code
$no2 = $_POST['vehicleStyle'];
echo $no2;
to show the price of the car which i set in value, but also i need to echo the text to show the name of the car, how i can do that?

if you don't need the values (2, 3, 4, 5), you can just use:
<select name="vehicleStyle">
<option>Volvo</option>
<option>Saab</option>
<option>Fiat</option>
<option>Audi</option>
</select>
but if you need both, you should do something like this:
$no2 = [
"2" => "Volvo",
"3" => "Saab",
"4" => "Fiat",
"5" => "Audi"
]$_POST['vehicleStyle'];
echo $no2;
be aware that if you change the dropdown in the future, you have to come back here and change also this array

Simple approach is give the value the same as the text in it!
<form method="post">
<select name="vehicleStyle">
<option value="Volvo">Volvo</option>
<option value="Saab">Saab</option>
<option value="Fiat">Fiat</option>
<option value="Audi">Audi</option>
</select>
<input type="submit" name="vehicleName">
</form>
if(isset($_POST['vehicleName'])){
echo $_POST['vehicleStyle'];
}

If you are also need both text(name) and value(id) as magnus eriksson Suggest you need to separate them like: id;name and explode it on the back end to get id as well as name.
<form method="post">
<select name="vehicleStyle">
<option value="2;Volvo">Volvo</option>
<option value="3;Saab">Saab</option>
<option value="4;Fiat">Fiat</option>
<option value="5;Audi">Audi</option>
</select>
<input type="submit" name="vehicleName">
</form>
if(isset($_POST['vehicleName'])){
list($id, $name) = explode(';', $_POST['vehicleStyle']);
echo $id.' => '.$name;
}

Thank you all,
i solved this by editing the code to:
<form method="post">
<select name="vehicleName">
<option value="2|volvo">Volvo</option>
<option value="3|saab">Saab</option>
<option value="4|fiat">Fiat</option>
<option value="5|audi">Audi</option>
</select>
<input type="submit" name="submit">
</form>
and
if(isset($_POST['submit']))
{
$no2=$_POST['vehicleName'];
$no2_explode=explode('|', $no2);
for text
<?php echo $no2_explode[1]; ?>
and for the price number
<?php echo $no2_explode[0]; ?>

Related

I want to get values in PHP throughout a html form

Im new to PHP and want to get these arrays to send by a form, but cant manage to store it in a variable and access them
HTML
<form action="./index.php" method="post">
<select name="multicheckbox[]" multiple="multiple" class="4colactive">
<option value="LunVie" name="LunVie">Lunes a Viernes</option>
<option value="LunSab" name="LunSab">Lunes a Sábados</option>
<option value="Todos" name="Todos">Todos los días</option>
<option value="Otros" name="Otros">Otros</option>
</select>
<button type="submit">Enviar</button>
</form>
PHP
<?php
$values = $_POST["multicheckbox"];
echo $values[2];
?>
multicheckbox is an array, iterate over it.
foreach($_POST["multicheckbox"] as $check) {
echo $check . "<br />\n";
}
Also note options don't have names, the select has a name. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option
<select name="multicheckbox[]" multiple="multiple" class="4colactive">
<option value="LunVie">Lunes a Viernes</option>
<option value="LunSab">Lunes a Sábados</option>
<option value="Todos">Todos los días</option>
<option value="Otros">Otros</option>
</select>

PHP Display label and selected value

:::EDIT:::
Ok so apparently I should have put in the larger picture here, and that is my fault. Ok so let's say there are 2 sets of dropdowns,
Consider the following HTML:
<form name="myform" action="process.php" method="POST">
<input type="hidden" name="check_submit" value="1" />
<select name="month">
<option selected="--">--</option>
<option value="January">01</option>
<option value="February">02</option>
<option value="March">03</option>
<option value="April">04</option>
<option value="May">05</option>
<option value="June">06</option>
<option value="July">07</option>
<option value="August">08</option>
<option value="September">09</option>
<option value="October">10</option>
<option value="November">11</option>
<option value="December">12</option>
</select>
<select name="year">
<option selected="--">--</option>
<option value="1965">65</option>
<option value="1966">66</option>
<option value="1967">67</option>
<option value="1968">68</option>
</select>
<input type="submit" />
</form>
I am using $_POST in 'process.php' to display the year selected by the user like this:
<?php
echo "{$_POST['month']} ";
echo "{$_POST['year']} ";
?>
If "67" is selected "1967" displays AND when "09" is selected, "September" is displayed so the question still stands...
Is there a way that I can display the option and the label on the same page? This is simple HTML with a PHP processor, nothing more.
Thanks!
you can't achieve it by php alone you can use jquery with this like follow
<select name="month" onchange="document.getElementById('month_text').value=this.options[this.selectedIndex].text">
<option selected="--">--</option>
<option value="January">01</option>
<option value="February">02</option>
<option value="March">03</option>
............
<option value="December">12</option>
</select>
<select name="year" onchange="document.getElementById('year_text').value=this.options[this.selectedIndex].text">
<option selected="--">--</option>
<option value="1965">65</option>
<option value="1966">66</option>
<option value="1967">67</option>
<option value="1968">68</option>
</select>
<input type="hidden" name="year_text" id="year_text" value="" />
<input type="hidden" name="month_text" id="month_text" value=""/>
in php
echo $_POST['year']; // will print 1967
echo $_POST['year_text']; // will print 67
echo $_POST['month']; // will print January
echo $_POST['month_text']; // will print 01
You can do like this:
Modify html like:
<option value="1965-65">65</option>
In PHP:
$explode = explode("-",$_POST['year']);
echo $explode[0]; //will be 1965
echo $explode[1]; //will be 65
OR
your html
<option value="1965">65</option>
PHP
echo substr($_POST['year'], 2, 2); // 65
<?php
$monthArray = array('01'=>'January','02'=>'February','03'=>'March','04'=>'April',
'05'=>'May','06'=>'June','07'=>'July','08'=>'August',
'09'=>'September','10'=>'October','11'=>'November',
'12'=>'December');
$month = $_POST['month'];
$year = $_POST['year'];
echo $month;
echo $year;
echo "You have selected ".substr($year, 2)." Year.";
echo "You have selected ".$monthArray[$month]." month.";
?>
I have updated the code for month also.

Using $_POST to get select option value from HTML

I use select as below:
<select name="taskOption">
<option>First</option>
<option>Second</option>
<option>Third</option>
</select>
How do I get the value from the select option and store it into a variable for future use, in PHP?
Use this way:
$selectOption = $_POST['taskOption'];
But it is always better to give values to your <option> tags.
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
You can access values in the $_POST array by their key. $_POST is an associative array, so to access taskOption you would use $_POST['taskOption'];.
Make sure to check if it exists in the $_POST array before proceeding though.
<form method="post" action="process.php">
<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
<input type="submit" value="Submit the form"/>
</form>
process.php
<?php
$option = isset($_POST['taskOption']) ? $_POST['taskOption'] : false;
if ($option) {
echo htmlentities($_POST['taskOption'], ENT_QUOTES, "UTF-8");
} else {
echo "task option is required";
exit;
}
You can do it like this, too:
<?php
if(isset($_POST['select1'])){
$select1 = $_POST['select1'];
switch ($select1) {
case 'value1':
echo 'this is value1<br/>';
break;
case 'value2':
echo 'value2<br/>';
break;
default:
# code...
break;
}
}
?>
<form action="" method="post">
<select name="select1">
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
</select>
<input type="submit" name="submit" value="Go"/>
</form>
for php8+ versions, you can use match expression:
$select = $_POST['select1'] ?? '';
$result = match ($select) {
'value1' => 'this is value1',
'value2' => 'this is value2',
default => 'unknown value',
};
echo $result;
<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
$var = $_POST['taskOption'];
Depends on if the form that the select is contained in has the method set to "get" or "post".
If <form method="get"> then the value of the select will be located in the super global array $_GET['taskOption'].
If <form method="post"> then the value of the select will be located in the super global array $_POST['taskOption'].
To store it into a variable you would:
$option = $_POST['taskOption']
A good place for more information would be the PHP manual: http://php.net/manual/en/tutorial.forms.php
Like this:
<?php
$option = $_POST['taskOption'];
?>
The index of the $_POST array is always based on the value of the name attribute of any HTML input.
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
try this
<?php
if(isset($_POST['button_name'])){
$var = $_POST['taskOption']
if($var == "1"){
echo"your data here";
}
}?>
-- html file --
<select name='city[]'>
<option name='Kabul' value="Kabul" > Kabul </option>
<option name='Herat' value='Herat' selected="selected"> Herat </option>
<option name='Mazar' value='Mazar'>Mazar </option>
</select>
-- php file --
$city = (isset($_POST['city']) ? $_POST['city']: null);
print("city is: ".$city[0]);

how to retain selected values in Select field after form submission?

i have a main page that i used in uploading tickets to db, i have Select field that i want to retain the value that the user selected prior to submitting the form but its not happening...
here is my code for select field:
<select name="XiD" id="XiD">
<option value="Blank" selected="selected">Please Select...</option>
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
<option value="DDD">DDD</option>
<option value="EEE">EEE</option>
<option value="FFF">FFF</option>
</select>
just to add, this Xid is being posted prior to submitting
$XiD = $_POST['XiD'];
and i try to use this script:
UPDATE - changed from $_GET to $_POST but still not working...
<script type="text/javascript">
document.getElementById('XiD').value = "<?php echo $_POST['XiD'];?>";
</script>
my jquery version im using: jquery-ui-1.10.2 and jquery-1.9.1
Use jQuery like this.
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('select#XiD').val('<?php echo $_POST['XiD'];?>');
});
</script>
You are using PHP to generate HTML. There's absolutely no need to use a third language to dictate how that HTML should be!
<select name="XiD" id="XiD">
<option value="Blank"<?=$XiD=='Blank' ? ' selected="selected"' : ''?>>Please Select...</option>
<option value="AAA"<?=$XiD=='AAA' ? ' selected="selected"' : ''?>>AAA</option>
...
</select>
Or, even better, simplify your code with an array and avoid writing duplicate code that's hard to maintain:
<?php
$options = array(
'Blank' => 'Please Select...',
'AAA' => 'AAA',
'BBB' => 'BBB',
'CCC' => 'CCC',
'DDD' => 'DDD',
'EEE' => 'EEE',
'FFF' => 'FFF',
);
<select name="XiD" id="XiD">
<?php foreach(options as $k => $v){ ?>
<option value="Blank"<?php echo $XiD==$k ? ' selected="selected"' : ''?>>Please Select...</option>
<? } ?>
</select>
According to your problem, it seems like you are using POST method (because you are getting value to $XiD = $_POST['XiD'];) and using GET to retrieve value.
So, use document.getElementById('XiD').value = "<?php echo $_POST['XiD'];?>";
instead of
document.getElementById('XiD').value = "<?php echo $_GET['XiD'];?>";
I would create a function to check if the current GET value is the same as the option in the list and in that case, return the needed attribute to make it selected by default, which is selected="selected".
This should work:
<?php
function isSelected($value){
if($value == $_GET['XiD']){
return 'selected="selected"';
}else{
return '';
}
}
?>
<select name="XiD" id="XiD">
<option value="Blank" <? echo isSelected('Blank');?>>Please Select...</option>
<option value="AAA" <? echo isSelected('AAA');?>>AAA</option>
<option value="BBB" <? echo isSelected('BBB');?>>BBB</option>
<option value="CCC" <? echo isSelected('CCC');?>>CCC</option>
<option value="DDD" <? echo isSelected('DDD');?>>DDD</option>
<option value="EEE" <? echo isSelected('EEE');?>>EEE</option>
<option value="FFF" <? echo isSelected('FFF');?>>FFF</option>
</select>
After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>

Form Selectbox with wrong Value

Hy there
I have some selecboxes on my site like this one:
<div class="registrationdate">
<select class="filter registrationdate" name="rdat" id="regdate">
<option value="">---</option>
<option value="2012">at 2012</option>
<option value="2011">at 2011</option>
<option value="2010">at 2010</option>
</select>
</div>
now i got a problem after submit the form...
if "at 2011" is selected after submit i get just "2011", the value, but i want the text.
after refresh the select boxes change the text to "at 2011"
in source i can see the problem, but i don't know how to solve it.
after submit the form and i land on the same page the source of the selectbox above is altered like this:
<div class="registrationdate">
<select class="filter registrationdate" name="rdat" id="regdate">
<option value="2012">2012</option>
<option value="">---</option>
<option value="2012">at 2012</option>
<option value="2011">at 2011</option>
<option value="2010">at 2010</option>
</select>
</div>
so can here some help me? i hope i expressed me understandable.
thanx for help!
EDIT
OK, i think i have found the Problem in the code of my coworker... but i encounter another problem, the same what he wanted to solve.
This is the actual code:
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' name='filterform' method='get' class="filterform">
<div class='registrationdate'>
<strong><?php echo $GLOBALS['Lng']['filter_Erstzulassung']; ?>:</strong>
<select id="regdate" name="rdat" class="filter registrationdate">
<?php if (isset($_GET['rdat'])&&$_GET['rdat']!='') echo '<option value="'.$_GET['rdat'].'">'.$_GET['rdat'].'</option>'; ?>
<option value="">---</option>
<?php for ($i = date(Y); $i >= (date(Y)-7); $i-- ) {
echo '<option value="'.$i.'">ab '.$i.'</option>';
}?>
</select>
</div>
<input type="submit" value="<?php echo $GLOBALS['Lng']['apply']; ?>" name="submit" id="submit" class="field">
</form>
The Problem is that he want the entered option visible again after submit the form. In normal case if you send a form u got the default values. he tried to overwrite this with the php part "isset then echo".
AFAIK PHP you can't get the Text part of an select field, or?
What could he else to solve this?
If you wish to get "at 2011" you should set value of the option to
<option value="at 2011">at 2011</option>
As far as I understood, you want to keep value attribute, but get the text inside option tag.
$("#regdate option[value='2011']").text() will return "at 2011"
http://jsfiddle.net/BtNNT/5/

Categories