I'm trying to make this implode function work.
This is the form part, assume all the item has been selected.
<form method="post">
<select name="test1" multiple="multiple" id="test1">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
</select>
</form>
The PHP part
<?php
$var1 = array();
$joinedString = array();
$var1 = $_POST['test1'];
$joinedString = implode(',', $var1);
?>
But The echoing part doesn't work, it gives me error, and only displaying only the first array value.
<?php
$echo $joinedString[0];
$echo $joinedString[1];
$echo $joinedString[2];
$echo $joinedString[3];
$echo $joinedString[4];
?>
Thank You guys, I'm quite new in programming. I always forgot that the code executed line by line, and always confused with variable and values, and yes, in real world I am also a clumsy & insignificant person.
<form method="post" action="sear.php">
<select name="test1[]" multiple="multiple" id="test1">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
<input type="submit" name="submit" value="submit" />
</select>
</form>
<?php
$var1 = array();
$joinedString = array();
$var1 = $_POST['test1'];
$joinedString = implode(',', $var1);
echo $joinedString;
?>
After getting the post values it definitely works.... Try it...
Use
<select name="test1[]" multiple="multiple" id="test1">
In php file.
$var1 = isset($_POST['test1']) ? $_POST['test1']: 0 ;
print_r($var1); //gives array
foreach($var1 as $var) {
echo $var;
}
Change
<select name="test1" multiple="multiple" id="test1">
to
<select name="test1[]" multiple="multiple" id="test1">
And it's already an array
$var1 = $_POST['test1'];
$imploded = implode(",", $var1);
echo $imploded;
//FOR GETTING INDIVIDUAL ITEMS FROM array
echo $var1[0];
Implode is not getting any array. Its works on Array.
You are passing a string instead of an array. implode() turns an array into a string.
In your script, $_POST['test1'] will only contain the submitted value of the select box, not the entire set of values. Since $var1 contains only a string, implode() will error out.
Just look up this:
http://www.tizag.com/phpT/php-string-implode.php
And $ is used with variable.
You are trying to use implode your array using (,) but post array values does not contain values seperated by comma, therefore you will have to use foreach.
<?php
$var1 = array();
$joinedString = array();
$var1 = $_POST['test1'];
foreach($var1 as $values)
echo $values."<br/>";
?>
<form method="post">
<select name="test1[]" multiple="multiple" id="test1">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
</select>
<input type="submit" >
</form>
Related
how can I get the selected key and value of a HTML select box using php?
<select>
<option value="KEY">VALUE</option>
</select>
php code ?
With PHP, it is not straight forward.
However, with use of array you can achieve it.
Take and array of all options in a common function:
$options = array();
$options[1] = 'one';
$options[2] = 'two';
$options[3] = 'three';
Display drop down like this:
<select name="opt">
<option value=""></option>
<?php
if (! empty($options)) {
foreach ($options as $key => $val) {
?>
<option value="<?php echo $key;?>"><?php echo $val;?></option>
<?php
}
}
?>
</select>
And where the form is posted, again get the array.
//$options : fetch from common function.
if (isset($_POST['opt'])) {
echo "key: " . $_POST['opt'];
echo "<br/>";
echo "value: " . isset($options[$_POST['opt']]) ? $options[$_POST['opt']] : '';
}
You must do like this
<form class="" action="receiver.php" method="post">
<select name="mykey">
<option value="KEY">VALUE</option>
</select>
</form>
In your receiver.php do in this example
$keys = $_POST['mykey'];
Name of the select is the key and option is the value.
<select name="KEY">
<option value="VALUE">VALUE</option>
<option value="VALUE_2">VALUE</option>
</select>
On submit you will get a KEY => VALUE pair in $_GET or $_POST (whatever you use to handle the request)
page1.html:
<form action="page2.php" name='add' method="post">
<select name="foo">
<option value="my_value">my_value</option>
</select>
<input type='submit' name='submit'/>
</form>
page2.php:
<?php
$result = $_POST['foo'];
echo "result = ".$result."<br>";//output: result = my_value
?>
Using PHP, you could use this:
Put Key and Value as POST data, delimited with a symbol, for example '/'.
HTML:
<select name="myselect">
<option value="KEY/VALUE">VALUE</option>
</select>
Then in PHP, separate that key with explode() function.
PHP:
$pair = explode("/", $_POST["myselect"]);
$key = $pair[0];
$value = $pair[1];
print "Key: $key<br />";
print "Value: $value<br />";
Let's say you have form like this
<select name="country">
<option value="IN_India">India</option>
<option value="CN_China">China</option>
<option value="AE_UAE">UAE</option>
</select>
Upon submitting it, your $_POST would look something like this
print_r($_POST);
Array
(
[country] => IN_India
}
Now explde your $_POST data and fetch values like this
$data = explode('_', filter_input(INPUT_POST, 'country'));
print_r($data);
Array
(
[0] => IN
[1] => India
)
I want to set both values as variable. Means if I want to use sales or training as a variable and if I want to use its value anywhere then I can use it.
<form action="code.php" method="post">
<select name="department">
<option>Please Choose Department</option>
<option value="sales">Sales</option>
<option value="training">Training</option>
</select>
<input type="submit" value="submit">
</form>
I am trying by below code but I can't.
code1.php
<?php
$stud = explode("_", $_POST['department']);
$sales = $stud[0];
$training = $stud[1];
echo "$sales";
echo "$training";
?>
$sales and $training are not working as variable.
Please help.
Probably you are looking for this:
<form action="code.php" method="post">
<select name="department[]" multiple>
<option>Please Choose Department</option>
<option value="sales">Sales</option>
<option value="training">Training</option>
</select>
<input type="submit" value="submit">
</form>
<?php
$sales = $_POST['department'][0];
$training = $_POST['department'][1];
echo $sales;
echo $training;
?>
select must be defined as multiple select and as array in name.
Update
<select name="department">
With
<select name="department[]" multiple>
PHP Part
<?php
$stud = $_POST['department'];
extract($stud);
echo $sales;
echo $training;
?>
Explanation:
We have used multiple attribute of <select> so that multiple options can be selected.
If we use multiple as select, the value getting posted as array. So, no need to explode it.
Then we extract() ed the posted array to get two desired elements.
This is my list box sample code
<form id="myForm" action="somePage.php" method="post">
<p>myList<br>
<select name="select">
<option value="Option 1" selected>---</option>
<option value="Option 2">sel1</option>
<option value="Option 3">sel2</option>
<option value="Option 4">sel3</option>
</select>
</p>
</form>
But the problem is that i would like to fill the list with the result of a query. Leaving out for a moment the query, the real point is "How can i print the <option value= ..." programmatically so that with a for cycle i can fulfill the list? For example i thought something like this
<form id="myForm" action="somePage.php" method="post">
<p>myList<br>
<select name="select">
<?php
for(i;i < myQueryResultArray length;i++){
$counter = i;
echo <option value="Option $counter">$myArrayValue[i]</option>
}
?>
</select>
</p>
</form>
This is for sure wrong but that's the idea i had. It may be correct with proper syntax? Or better other ways? Thanks
You'd want a for loop like this
for($i = 0; $i < sizeof($myQueryResultArray);$i++){ //note the changes here
//declare variables with $
//sizeof() will return length
echo "<option value='Option $i'>$myArrayValue[$i]</option>"; //notice the quotes
}
$res = $db->query($query);
foreach($res as $item) {
?>
<option value = "<?=$item['key1']?>"><?=$item['key2'] ?></option>
<?php
}
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]);
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
<option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
$test=$_POST['test'];
if ($test){
foreach ($test as $t){echo 'You selected ',$t,'<br />';}
}
if($t=='one')
$price1=12;
if($t=='two')
$price2=2;
$total = $price1 + $price2;
echo $total;
When one & two are both selected i'd like the result to be 14. What is the best way to make an addition to obtain 14 as a result ?
You should make one variable that will contain the result, instead of making price1, price2 and etc.
For example:
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
<option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
$test = isset($_POST['test']) ? $_POST['test'] : null; // we also want to check if it's set else we'll get an notice
$total = 0;
if ($test) {
foreach ($test as $t) {
echo 'You selected ',$t,'<br />';
switch ($t) {
case "one" : $total += 2; break;
case "two" : $total += 12; break;
}
}
}
echo 'Total: ' .$total;
put that IF's into foreach loop
You must place the if $t = one , etc. inside the loop
Firstly, $test should be assigned using a ternary operator, that way we can make sure we don't cause PHP Warnings.
$test = isset($_POST['test']) ? $_POST['test'] : false;
if($test):
//good
endif;
Secondly, why even bother using string interpretation of an integer?
<option value="1">one</option> //note the values
<option value="2">two</option>
Lets make an array that has our answers.
$vals = array(
0 => 'Not an option!',
1 => 12,
2 => 2,
3 => 14,
4 => 26
);
Create a dummy variable we can use to concatenate the values posted.
$val = 0;
foreach($test as $t):
$val += $t;
endforeach;
Now $t will be the value of all the posted select items.
echo $vals[$val];
And that simple echo statement will supply you with your answer. This way you're not having to do multiple if/else statements, keeps it cleaner and more robust imo.
and here's a Codepad illustrating this