post dropbox value populated from database - php

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!

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';
}
}

<option> is having each option its own dropdown

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.

Multiple selects are not being submitted in the form using post?

I am trying to create a form where I need to allow user to select different values (from select tag) against some labels.
I have two different array in php.
the standard array that contains the labels for select.
a set of values to be selected against the labels.
My problem is when I press the submit button, the form is submitted but $_POST does not show any value selected by the select tag. I want to get the selected values against the labels.
here is my code:
<?php
$data = array ('name', 'phone', 'address');
$values = array('a','2344','xyz');
?>
<html>
<head></head>
<body>
<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
<?php for($i = 0; $i < count($data); $i++){ ?>
<label for='<?php $data[$i]?>'> <?php echo $data[$i]?></label>
<select name='<?php $data[$i]?>' id = '<?php $data[$i]?>'>
<?php foreach($values as $val){ ?>
<option value='<?php $val ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
<br>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
echo$_POST['name'];
}
?>
When I press the submit button the error I get is "Notice: Undefined index: name". I have extensively searched in the questions already posted about multiple select statements but none of the answers matched my criteria. Thanks for the help.
You are forgot to print variables
<html>
<head></head>
<body>
<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
<?php for($i = 0; $i < count($data); $i++){ ?>
<label for='<?php echo $data[$i]?>'> <?php echo $data[$i]?></label>
<select name='<?php echo $data[$i]?>' id = '<?php echo $data[$i]?>'>
<?php foreach($values as $val){ ?>
<option value='<?php echo $val ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
<br>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
echo$_POST['name'];
}
?>
You have not written echo before each variable in selectbox:
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<?php for($i = 0; $i < count($data); $i++) { ?>
<label for='<?php echo $data[$i]; ?>'> <?php echo $data[$i]?></label>
<select name='<?php echo $data[$i]; ?>' id = '<?php echo $data[$i]; ?>'>
<?php foreach($values as $val){ ?>
<option value='<?php echo $val; ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
</form>

How to display selected value in combo box in php?

I've written this code... but this fails to display text... not sure what's wrong with the code. I'm new to PHP and trying to create a page that gets customer details and puts it in SQL.
<!DOCTYPE html>
<html>
<body>
<?php
$initial="";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
echo "Customer Intial: $initial <br>";
?>
</body>
</html>
Try this:
$initial="";
if(isset($_POST['initial'])){
$initial=htmlentities($_POST['initial']);
}
You correctly sent the POST however you didn't put the value given with the POST into the $initial variable.
<!DOCTYPE html>
<html>
<body>
<?php
$initial = "";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
$initial = filter_input(INPUT_POST, "initial");//GET the input in post method
if ($initial == 1) {
$initial_value = "Mr.";
} elseif ($initial == 2) {
$initial_value = "Mrs.";
} elseif ($initial == 3) {
$initial_value = "Ms.";
} elseif ($initial == 4) {
$initial_value = "M/s";
} else {
$initial_value = "Select initial";
}
echo "Customer Intial: $initial_value <br>";
?>
</body>
</html>
Try,
<form method="post" action=<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>>
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<br>
<br>
<?php
if(isset($_POST['initial'])){
$initial=$_POST['initial'];
} else {
$initial = "empty";
}
echo "Customer Intial : ".$initial;
?>
</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>
​

Categories