<option> is having each option its own dropdown - php

I tried putting the loop inside but then when I pick a category it won't print in the echo and if the loop is outside like right now each category is printed alone I want them all in one tab and one go button
<?php
while($row = mysqli_fetch_array($result)) {
$category = $row["category"];
?>
<form action="" method="post">
<select name="work_place">
<option value="<?php echo $category;?>"><?php echo $category;?></option>
</select>
<input type="submit" value="go" />
</form>
<?php
}
<?php
$post = (isset($_POST['work_place'])) ? $_POST['work_place'] : '';
echo $post;
?>
Image of current issue:

<form action="" method="post">
<select name="work_place">
<?php
while($row = mysqli_fetch_array($result)) {
$category = $row["category"];
?>
<option value="<?php echo $category;?>"><?php echo $category;?>
</option>
<?php
} ?>
</select>
<input type="submit" value="go" />
</form>
You want to loop over 'option', only.
You do not want multiple forms and 'select' inputs.

Related

$_POST doesn't get the selected option value. Using PHP and MySQL to populate the HTML <select>

I want to save the selected value in post and access it from checkout.php. So far this method has worked for my other pages but here it just doesn't. This is how it looks like:
Source page of the value:
<form action = "checkout.php" method="post" >
<select name='time' required>
<option value="">-- Time --</option>
<?php
$result = getFreeAppointments($empid,$date);
while ($row = mysqli_fetch_array($result)) {
?>
<option value = <?php echo $row['StartTime'];?>><?php echo $row['StartTime'];?></option>
<?php } ?>
</select>
<input class="submit-block" type="submit" value="Checkout">
</form>
Then in checkout.php it looks like this.
if(isset($_POST['time']) && !empty($_POST['time'])) {
$time = $_POST['time'];
}
else{
$time = '10:00:00';
}
If I echo the $time variable, I always get the default value '10:00:00'. Any idea what could cause this problem?
You forgot to add name attribute in <input class="submit-block" type="submit" value="Checkout">
Like :- <input class="submit-block" type="submit" name="submit" value="Checkout">
<form action = "checkout.php" method="post" >
<select name='time' required>
<option value="">-- Time --</option>
<?php
$result = getFreeAppointments($empid,$date);
while ($row = mysqli_fetch_array($result)) {
?>
<option value = <?php echo $row['StartTime'];?>><?php echo $row['StartTime'];?></option>
<?php } ?>
</select>
<input class="submit-block" type="submit" name="submit" value="Checkout">
</form>
you have set isset($_POST["submit"]) but in HTML the form, there is no data with this name.
if (isset($_POST['submit'])){ // Problem is here
insertKunde($_POST);
if(isset($_POST['time']) && !empty($_POST['time'])) {
$time = $_POST['time'];
}
else{
$time = '10:00:00';
}
}

post dropbox value populated from database

iam trying to populate my dropbox from my database and then retrieve it in my php code...is this possible?
<form method="POST">
<select>
while ($fethc_items = mysql_fetch_assoc($items))
{
$item_id= $fethc_items['item_id'];
$itemname = $fethc_items['item_name'];
?>
<option value="<? echo $itemname;?>">Volvo</option>
<?}?>
<input type="submit" value="Submit" />
</form>
<?
if (isset($_POST['?']))
{
}
?>
Yes you can..
Try this code. I hope its will help you.
<form method="POST">
<select name="item">
<?php
while ($fethc_items = mysql_fetch_array($items))
{
$item_id= $fethc_items['item_id'];
$itemname = $fethc_items['item_name'];
?>
<option value="<?php echo $item_id;?>"><?php echo $itemname;?> </option>
<?php } ?>
</select>
<input type="submit" value="Submit" />
</form>
<?php
if (isset($_POST['item'])) {
}
?>
Thank You!

php script with multiple buttons

The idea is the page in which you select a category from drop-down menu, then after you click remove button, new form shows(contains yes/no buttons) that asks you if you really want to remove selected category. Problem is the second yes/no script. It works on separate page, but on page with first form it doesn't echo anything nor does it remove a pet. Please help, thanks!
<?php
/*remove a category*/
include("connection.php");
?>
<html><head></head>
<body>
<?php
$PetListquery= "SELECT distinct petType From petType ORDER by petType";
$PetListResult= mysqli_query($cxn,$PetListquery) or die ("Couldn't execute query.");
?>
<div style="border:2px solid;">
<form method="POST" action="removeCategory.php">
<div align='left'>
Choose category you want to remove:
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' ><?php echo $petType;?> </option>
<?php }?>
</select>
</div>
<div>
<p><input type='submit' name='Remove' value='Remove Category' />
</div>
</div>
</form>
<?php
foreach($_POST as $field => $value)
{ //second form starts after if
if($field == 'petType')
{
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<div>
<input name="Yes" type="submit" value="Yes">
<input name="No" type="submit" value="No">
</div>
</form>
<?php
echo "Are you sure you want to delete selected category?";
//clicking any of these buttons doesn't display anything
if(isset($_POST['Yes']))
{
echo "yes";
$DeleteQuery= "DELETE From petType WHERE petType='$petType'";
$DeleteResult= mysqli_query($cxn,$DeleteQuery) or die ("Error1!");
}
if(isset($_POST['No']))
{
echo "No!";
}
}
}
?>
</body></html>

dropdown selected item not printing

I am trying to print the dropdown selected item. I have well displayed the dropdwon list menu. but when i select an option it doesn't print the option. i have tried in many ways. But not yet got! Please help me, this is my following code.
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<?php
echo "<select name=category></option>";
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
You have issues in your code, try this one instead :
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<select name=category>
<?php
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
?>
</select>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
$_GET['category']
should be
$_POST['category']
Example for javascript:
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var eSelect = document.getElementById('cat');
eSelect.onchange = function() {
document.getElementById("displaytext").innerHTML = "Selected Value: "+this.value;
document.getElementById("displaytext").style.display= 'block';
}
}
</script>
</head>
<body>
<select id="cat" name="cat">
<option value="x">X</option>
<option value="y">Y</option>
<option value="other">Other</option>
</select>
<div id="displaytext" style="display: none;" ></div>
</body>
</html>
​

handle info from select box

I have the following code:
<form name="votos" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php $categorias=mysql_query("SELECT * FROM categoria") or die (mysql_error()); ?>
<?php while($cat=mysql_fetch_array($categorias)){
echo "<h5>".$cat[0]." - ".$cat[1]."</h5>";
$nomeados=mysql_query("SELECT * FROM nomeados WHERE cod_categoria='$cat[0]'") or die(mysql_error());
?>
<div class="styled-select">
<select name="voto">
<option value=""></option>
<?php
while($nom=mysql_fetch_array($nomeados)){
$nomes=mysql_fetch_array(mysql_query("SELECT nome FROM logins WHERE cod_login='$nom[0]'")) or die (mysql_error());
?>
<option value="<?php echo $nom[0]; ?>"><?php echo $nomes[0]; ?></option>
<?php
}
?></select></div>
<?php }
?>
<input type="submit" class="botao" value="" name="submit" />
</form>
The code shows all the categories with the nomenies in a select box.
The thing is that the select box goes to post with the same name. I don't know how to get the information from all of the select boxes.
If you have more elements with the same name you can post them as an array
<select name="voto[]">
and than you get it in php
$votos = $_POST['voto']
foreach($votos as $voto){
//do what you need to do
}
and it's an array with all the values

Categories