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
}
Related
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>
can i create multiple form look like the one in phpmyadmin where the user can
create multiple forms by selecting a number from option dropdown list to insert multiple value
<form>
<input type="text" />
<input type="text" />
<input type="text" />
</form>
<select>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
this is the code i am using
<form action="ChangeManager.php" method="post">
<select name="prog_lang">
<?php
if($questions = $num_of_questions){
$num_of_questions = 1;
for ($i=1; $i<=$num_of_questions; $i = $i + 1){
echo "<label>السؤال رقم #".$i."</label><br>";
echo "<textarea name='"."q".$i."' rows='10' cols='50'></textarea><br>";
echo "<label>الاجابات الممكنة:</label><br>";
for($j=1; $j<=4; $j = $j + 1){
echo '<input class="choice" type="text" name="'.'choice'.$i.$j.'" id="choice'.$i.$j.'"/>';
echo '<input class="correct" type="radio" name="'.'correct'.$i.'" value="correct'.$i.$j.'"/>';
echo '<label>صحيحة</label><br>';
}
echo "<br><br>";
}
}
?>
</select>
<input style="color:blue; font-size:14pt; width:100;"type="submit" value="حفظ الاختبار" />
</form>
I suggest to use JavaScript in order to manipulate the DOM. However you can do it with PHP as well:
First you need to know how many input:
<form action="" method="post">
<select name="howManyInput">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</form>
In the 'action' page you get the value of 'howManyInput':
<?php
$howMany = $_POST['howManyInput'];
?>
Then you print the input you need in a 'for' loop:
<form action="" method="post">
<?php
for( $i=0; $i<$howMany; $i++ ){
echo '<input name="inputName'.$i.'" >';
// if you need more the 1 input for '$howMany' echo it here
}
?>
</form>
The first form take you to the page where the user can insert data.
In that page you print as many input the user select in the previous page.
When the second form is submitted it goes to a page that receive all data.
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.
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";
}
}
?>
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]);