I have this form :
<form>
<select name="data[][]" multiple="multiple">
<option>1</option>
<option selected="selected">2</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">3</option>
<option>4</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">5</option>
<option selected="selected">6</option>
</select>
</form>
I would like to get this :
$_POST['data'] = array(
0 => array(2),
1 => array(3),
2 => array(5,6)
);
I instead get this :
$_POST['data'] = array(
0 => array(2),
1 => array(3),
2 => array(5),
3 => array(6)
);
The solution would be to set the index : name="data[0][]" but i want it to be automatically done ..
Any ideas?
One way.Change to :
<select name="data[0][]" multiple="multiple">
...
<select name="data[1][]" multiple="multiple">
...
<select name="data[2][]" multiple="multiple">
Also see #CBroe comment.
You don't need to insert the keys manually.
I tested your code and it seems to be correct for what you intend to get or you expect.
I tested it like this;
<html>
<body>
<?php
if (!isset($_GET["submit"])) {
?>
<form method="get" action="yourfilename.php">
<select name="data[][]" multiple="multiple">
<option>1</option>
<option selected="selected">2</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">3</option>
<option>4</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">5</option>
<option selected="selected">6</option>
</select>
<input type="submit" value="submit" name="submit"/>
</form>
<?php
}
else {
$data= $_GET["data"];
print_r($data);
echo $show;
}
\\ So to get the 5 or 6 value in the array with key "2";
\\ Output: 5
$show= $data[2][0];
\\ Or
\\ Output: 6
$show= $data[2][1];
?>
</body>
</html>
Related
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]; ?>
I need your help! I am trying to save a variable in sql table using Php but I have problem. There are two questions in php, the first concern the continent and the second is depended from the continent. I want to use a loop to check which of the continents has been selected in the first question and then save the value of the second question. I hide the option of unchecked continent using some javascript code (I don't have problem).
The HTML code:
<form method="post" action="">
<fieldset><legend>Continents</legend>
<select id="q1" name="q1">
<option value="1">Africa</option>
<option value="2">Asia</option>
<option value="3">Australia</option>
<option value="4">America</option>
<option value="5">Europe</option>
</select>
<select id="q2" name="Africa">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Asia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Australia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="America">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Europe">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
</fieldset>
The Php code
$q1 = $_POST['q1'];
$continents = array("Africa","Asia", "Australia","America","Europe");
for ($i = 1; $i <= 5; $i++) {
if($q1 == $i) {
$q2 = $_POST[$continents[$i-1]]
}
}
Your array should be from 1 to 5 instead of 0 to 4 as you have values 1 to 5 in q1.
Alternatively, I suggest that change your HTML structure to get the continent value in a single line without using loop. You need to change the values of continent like,
<select id="q1" name="q1">
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="America">America</option>
<option value="Europe">Europe</option>
</select>
And to get the value of selected continent use $_POST[$_POST['q1']]. For egs, $_POST['q1']=Asia, then $_POST['Asia'] will return the Asia's choice,
$q2= $_POST[$_POST['q1']];
change
$continents =
array(1 => "Africa",
2 => "Asia",
3 => "Australia",
4 => "America",
5 => "Europe");
or
if(($q1-1) == $i) {
$q2 = $_POST[$continents[$i]]
}
First, you may change your select continent HTML:
<select id="q1" name="q1">
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="America">America</option>
<option value="Europe">Europe</option>
</select>
Then you could loop over your continents and get the answer:
$q1 = $_POST['q1'];
$continents = array("Africa","Asia", "Australia","America","Europe");
foreach($continents as $continent) {
if ($_POST['q1'] == $continent) {
$q2 = $_POST[$continent];
}
}
Now you have your answer to the second question in $q2.
How about this?
// Always try to seperate logic from your view
// Intialize data
$continents = array("Africa", "Asia", "Australia", "America", "Europe");
// Check for $_POST
if(isset($_POST["q1"])) {
foreach($continents as $id => $continent) {
if($_POST["q1"] == $id) {
// Do something special here
}
}
}
// Render HTML
<form method="post" action="">
<fieldset>
<legend>Continents</legend>
<select id="q1" name="q1">
<!-- Notice that I echo only variables not all of the html -->
<?php foreach($continents as $id => $continent) { ?>
<option value="<?php echo $id; ?>"><?php echo $continent; ?></option>
<?php } ?>
</select>
<select id="q2" name="Africa">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Asia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Australia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="America">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Europe">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
</fieldset>
Beautiful thing about my solution is that now you can create whatever array you want and your code will always work.
You can for example SELECT data from database and store them in $continents variable.
$query = "SELECT id, name FROM continents";
$result = mysql_query($query);
$continents = array();
while($row = mysql_fetch_assoc($result)) {
$continents[$row["id"]] = $row["name"];
}
Cool right? :)
I found the solution finally!!!
Replace
$q2 = $_POST[$continents[$i-1]]
with
$q2 = $_POST["{$continents[$i-1]}"];
That solution I wanted!!
I'm trying to do something very simple in PHP, but keep getting an error message. Essentially, when someone selects "Cat", I want "Miaow" to appear.
My idea was:
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<?php if ($_POST['demo'] == 'Cat') { echo 'Miaow'; } ?>
However, in PHPFiddle,
I get 'E_NOTICE : type 8 -- Undefined index...'
as soon as the code runs. Am I missing something basic? Thanks!
Your form might be passing data by $_GET instead of $_POST.
Did you specify the method ?
<form method="post" action="index.php">
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<input type="submit" value="Submit">
</form>
You can var_dump($_POST); and var_dump($_GET); to see what those variables contains in your PHP file.
Or you can do it in javascript like this :
function animal(value) {
if(value == "Cat") {
document.getElementById("myAnimal").innerHTML = "Miaouw";
} else {
document.getElementById("myAnimal").innerHTML = "Rooooah";
}
}
<form action="#">
<select name="demo" onchange="animal(this.value)">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
</form>
<span id="myAnimal"></span>
Is it even possible to do this using PHP so that "Miaow" comes up automatically on select, rather than having to submit the form?
You are looking for JavaScript code, not PHP. Here is a jQuery example:
$(document).on('change', '.animals', function(){
$('.noise-target').html( $('option:selected', this).data('noise') );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="animals">
<option>Select</option>
<option data-noise="Woof">Dog</option>
<option data-noise="Meow">Cat</option>
</select>
<div class="noise-target"></div>
Maybe this will help, I write some block as far as I understand...
<form action="#" method="post">
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<input type="submit" name="sub" />
</form>
<?php
if(isset($_POST['sub'])){
if($_POST['demo'] == "cat"){
echo "Miao";
}
}
?>
<form action="a.php" method="post">
<select id="sel_1" name="sel[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
Now when i am trying to fecth the data like this
$offer = $_POST['sel'];
print_r($offer);
its displaying data like this:
Array
(
[0] => 1 // 1, 2 selected for sel_1
[1] => 2
[2] => 2 // 2, 3 selected for sel_2
[3] => 3
)
Shouldn't it come like this?
Array
(
[0] => Array(
[0] => 1
[2] => 2
)
[1] => Array(
[0] => 2
[2] => 3
)
)
I want to create string data like this(in the nxt a.php file):
for sel_1 data is created like "1, 2";
for sel_2 data is created like "2, 3";
How can i fetch the data in the above format.
I am trying this
for($i = 0; $i<count($offer) ; $i++)
{
for($j = 0; $j<count($offer[$i]); $j++)
{
$string = $tring. $offer[$i][$j];
}
}
Set the names sel1[] and sel2[] (different). In PHP you can use array_merge to obtain another array with the values from the first and from the second array:
$offer = array_merge($_POST['sel1'], $_POST['sel2']);
$string = '';
for($i = 0; $i < count($offer); $i++)
{
$string .= $offer[$i];
}
Try like this,it's working as per your requirement:
Instead of this
$offer = $_POST['sel'];
Put like this
$offer[] = $_POST['sel'];
Code:-
<form action="" method="POST">
<select id="sel_1" name="sel1[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel2[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
<?php
error_reporting(0);
$offer=array();
$offer[] = $_POST['sel1'];
$offer[]= $_POST['sel2'];
echo "<pre>";
print_r($offer);
echo "</pre>";
?>
For output click here: Output
Try this:
<form action="a.php" method="post">
<select id="sel_1" name="sel[1][]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel[2][]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
Worked for me
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]);