PHP Why does my select option show empty - php

When I submit the form I don't get the value of the select option I tried using POST and session but it always show nothing
main.php
<form role="form" method="POST" action="test.php">
<?php if($id == 1 OR $id==2){
echo" <p> No data</p> ";}else{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata )
or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data )) {
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
$_SESSION['data_id']= $data_id;
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
test.php
$dataID = isset($_POST['data_id']) ? $_POST['data_id'] : '';
echo "data is $dataID";

Name of your input type select is data and you are accessing it with data_id so you have to use $_POST['data'] instead of $_POST['data_id']

get value of select box using name "data" as you have set name="data" in <select> box in html:
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is".$dataID;

main.php
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
}
?>
</select>
<?php
}
?>
<button type="submit" class="btn btn-primary">show</button>
test.php
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is $dataID";
try this one..

Check below points its may be creating issue.
Check first your <select class="form-control" name="data"> name is data so you can access it using $_POST['data'] not data_id.
Check for <option value="'.$row['data_id'].'"> may be data_id not giving correct value try to check with static value.

I modified this code for you,please use this code.Its work for me,i hope this code will work also for you.
main.php
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
?>
<option value="<?php echo $row['data_id']; ?>"><?php echo $dataName ?></option>
$_SESSION['data_id']= $row['data_id'];
<?php
}
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
test.php
<?php
if(isset($_POST['data']))
{
echo $_POST['data'];
}
?>

Related

How to populate dropdown field pre-selected with the existing data from another MySQL table?

In my database I have 2 tables:
To insert data, I have a form that populates dropdown options from the table formulation. This is what the insert form for formulation dropdown looks like:
<?php
$formulation = '';
$query = "SELECT * FROM formulation";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
$formulation .= '<option value="' . $row["formulationID"] . '">' . $row["formulation_name"] . '</option>';
}
?>
<select>
<option value="">Select formulation</option>
<?php echo $formulation; ?>
</select>
Now I am working on the ‘Update’ form. But my question is how can I populate the ‘Formulation’ field dropdown with the data from the formulation table (like as the insert form) but pre-selected with the existing formulation value for the name from the items table? Like this image below:
I am having problem with how I should build the form. How should I proceed with this form?
<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);
while ($row = $query->fetch_assoc()) {
$output['data'][] = array(
$row['name'],
);
}
echo json_encode($output);
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text"><br>
<label>Formulation</label>
<select >
<!--What should be the codes here? -->
</select>
</div>
<button type = "submit">Save changes</button>
</form>
Thanks in advance for your suggestion.
Note: I'm not a user of mysqli so maybe there will be some error, but you will get the idea. This will not tackle the update part, just the populate part
Since you are editing a certain item, I will assume that you have something to get the item's itemID.
<?php
$sql = "SELECT * FROM items WHERE itemID = ?";
$query = $connect->prepare($sql);
$query->bind_param("s", $yourItemID);
$query->execute();
$result = $query->fetch_assoc();
$itemName = $result['name'];
$itemFormulation = $result['formulation_fk'];
//now you have the name and the formulation of that certain item
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text" value="<?php echo $itemName; ?>"><br>
<label>Formulation</label>
<select >
<?php
$query = "SELECT * FROM formulation";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['formulationID']; ?>" <?php echo ($row['formulationID'] == $itemFormulation) ? 'selected' : ''; ?>>
<?php echo $row['formulation_name']; ?>
</option>
<?php
}
?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
I changed the code to better suit the problem, there may be typos, just comment for clarification
If I have understand Your question... You have to put Your result into a string. For example:
<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);
$option = '';
while ($row = $query->fetch_assoc()) {
$name=$row['name'],
$option.='<option value="$name">$name</option>'
}
echo json_encode($output);
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text"><br>
<label>Formulation</label>
<select >
<?=$option?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
I hope to be of help
This should do the trick:
<?php
$itemsSql = "SELECT * FROM items WHERE itemId = 5";
$itemQuery = $connect->query($sql);
$item = $itemQuery->fetch_assoc();
$formulationsSql = "SELECT * FROM formulation";
$formulationsQuery = $connect->query($sql);
$formulations = $itemQuery->fetch_assoc();
?>
<form action="updateItem" method="POST">
<div>
<label>Item Name</label>
<input type="text" value="<?= $item[0]['name']; ?>"><br>
<label>Formulation</label>
<select>
<?php foreach($formulations as $formulation){
echo '<option value="'. $formulation['formulationId'].'">' .
$formulation['formulation_name'] . '</option>';
} ?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>

why Isn't my form working

I'm trying to use a listbox form to query the database but it's not showing anything. The idea is that I've queried the database to fill the form with the names of suburbs and then, selecting a suburb will query the database again to return the names of parks in that suburb. When I use the search form it doesn't return anything.
this is the form:
<p>Select Suburb to search</p>
<form method="post" action="suburb_search.php" id="search">
<select>
<?php while ($row = $result->fetch_assoc()) { ?>
<option value="suburb"> <?php echo $row['suburb']?></option>
<?php }
} ?>
</select>
<input type="submit" name="search" value="Search" />
</form>
</div>
This is where it should use the results of the form to query the database but its not working:
<?php
$searchRequest = False;
if (isset($_GET['suburb'])){
$search = $_GET['suburb'];
$sql2 = "SELECT * FROM park_list WHERE suburb=$search";
$result2 = $db->query($sql2);
if($message){
echo "<p>$message</>";
} else {
?>
<div class="form">
<?php
while ($row2 = $result2->fetch_assoc()){
?>
<div class="results">
<h2><?php echo $row2['park_name'];?></h2>
<?php
}
}
} ?>
give a name for your select element as
<select name="suburb">
<?php while ($row = $result->fetch_assoc()) { ?>
<option value="suburb"> <?php echo $row['suburb']?></option>
<?php }
} ?>
</select>
and you are giving form method as POST but accepting data in GET in your php code, change this
if (isset($_GET['suburb'])){
to
if (isset($_POST['suburb'])){

give name to each option value

I have a table with id, header, etc. Would like to make a give a name from header column to dropdown list to each value. Now its only shows value, which is very uncomfortable :
<form method="POST" enctype="multipart/form-data" action ="uploadext.php">
<?php require_once('uploadext.php'); ?>
<div class="col-md-6"><input type="file" name="fileToUploadgp"></div>
<div class="col-md-6"><input type="file" name="fileToUploadpro"><br>
<?php
$dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$query = "SELECT id, header FROM done_add";
$query1="SELECT header FROM done_add";
$data = mysqli_query($dbc, $query);
$data1=mysqli_query($dbc, $query1);
$array=[];
while ($row = mysqli_fetch_array($data)) {
$arrayid[] = $row['id'];
$arrayhead[]=$row['header'];
}
?>
<select name="selectlink">
<?php foreach ($arrayid as $arr) {?>
<option value = "<?php print($arr)?>"
} ?><?php print($arr) ?></option>
<?php } ?>
</select>
<input type="submit" value="Отправить файлы" name="submita">
</div>
</form>
(sorry for bad english)
change the way you store your data in $array:
while ($row = mysqli_fetch_array($data)) {
$option = [];
$option['id'] = $row['id'];
$option['header'] = $row['header']
$array[] = $option;
}
every $option in $array will have and id and a header, then you can simply:
<select name="selectlink">
<?php foreach ($array as $option) {?>
<option value = "<?php echo $option['id'];?>"
} ?><?php echo $option['header'];?></option>
<?php } ?>
</select>

PHP - Variable inside <form action=" "> attribute as part of url for get method

Trying to pass a php variable inside the action attribute for html forms as part of the get method of passing variables through url based on the option that was selected.
<?php
...
$sql = "SELECT staffID, staffName
FROM staff";
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));
?>
<form action = "task7.php?" method = "get">
<select>
<?php
while($row = mysqli_fetch_array($results))
{
$staffID = $row["staffID"];
$staffName = $row["staffName"];
?>
<option value = "<?php echo $staffID ?>"> <?php echo $staffName ?> </option>
<?php
}
?>
</select>
<br> <br>
<input type = "submit" value = "Submit">
<input type = "reset" value = "Reset">
</form>
In this particular case I'm trying to pass the staffID variable as part of the url. Something like this tast7.php?staffID=12345
All thats missing from your code is a name on the select
<select name="staffID"> <!-- adding the name will append the selected value to the url -->
<?php
while($row = mysqli_fetch_array($results))
{
$staffID = $row["staffID"];
$staffName = $row["staffName"];
?>
<option value = "<?php echo $staffID ?>"> <?php echo $staffName ?> </option>
<?php
}
?>
</select>
In php,
<form action = "task7.php" method = "get">
<select name="staffID">
<?php
while($row = mysqli_fetch_array($results))
{
?>
<option value = "<?php echo $row["staffID"]; ?>"> <?php echo $row["staffName"]; ?> </option>
<?php
}
?>
</select>
<br> <br>
<input type = "submit" value = "Submit">
<input type = "reset" value = "Reset">
</form>
You may do it using jQuery/JS easily.
Eg : (Using jQuery)
function formSubmit(element){
var url = $("#test").attr("action", "");
url += "?" + element.name + "=" + element.value;
$("#test").attr("action", url);
$("#test")[0].submit();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form action="test.php" method="get" id="test">
<select name="id" onChange="formSubmit(this)">
<option value="abc">abc</option>
<option value="xyz">xyz</option>
</select>
</form>
You are thinking too complicated. Have a try like that:
<form action = "task7.php" method = "get">
<select name="staffID">
<?php
while($row = mysqli_fetch_array($results))
{
$staffID = $row["staffID"];
$staffName = $row["staffName"];
?>
<option value = "<?php echo $staffID ?>"> <?php echo $staffName ?> </option>
<?php
}
?>
</select>
<br> <br>
<input type = "submit" value = "Submit">
<input type = "reset" value = "Reset">
</form>
What you are looking for is not a "php variable". Such thing does not exist on the client side. Instead you simply want to submit the value chosen for your <select> tag as a simple "HTTP GET" parameter. That is exactly what a form tag with method GET does. You just have to name your option select, that's all: <select name="staffID">
try this
$id = 1;
//later
<form action = "task7.php?staffID="+<?= $id ?> method = "get">

php:process the select tag

this code run but when test it .. the data not correct i select data from database and i make it in array ad show it id by select tag but when select any id and submit .. example i select 5 and click on submit the record will delete is 2 not 5
<?php
require_once "config.php";
$qid="select id from info_user";
$arrayid=array();
$result=mysql_query($qid);
while($res=mysql_fetch_array($result)){
$arrayid[]=$res['id'];
}
var_dump($arrayid);
if(isset($_POST['sub'])){
$id=$_POST['id'];
$q="delete from info_user where id=$id ";
$qq=mysql_query($q);
if($qq){
echo "you delete record ";
}
else{
echo "error in deleting";
}}
?>
<html>
<head>
<title>delete</title>
</head>
<form action="delete.php" method="post">
<select name="id"><?php for($i=0;$i<count($arrayid);$i++){?>
<option value="<?php echo $i;?>"><?php echo $arrayid[$i];} ?></option></select> <br />
<input type="submit" name="sub" />
</form>
</html>
I think that problem is in value for the options.
Try changing option line to
<option value="<?php echo $arrayid[$i];?>"><?php echo $arrayid[$i];} ?></option></select> <br />
Your <option> markup is wrong. Make your markup like this;
<select name="id">
<?php for ($i = 0; $i < count($arrayid); $i++) { ?>
<option value="<?php echo $i; ?>"><?php echo $arrayid[$i]; ?></option>
} ?>
</select>
Tip: You'll find debugging easier if you indent your code. IDE's generally have this option. NetBeans is my favourite.
Instead of sending the ids, you're sending an array key associated to the id.
For instance, assume that: $arrayid = array(10, 11, 12);.
This is equivalent to: $arrayid = array(0 => 10, 1 => 11, 2 => 12);.
You'll see the options 10, 11 and 12, but you'll send either 0, 11 or 12, because that's what you're setting to the options values:
<select name="id">
<option value="0">10</option>
<option value="1">11</option>
<option value="2">12</option>
</select>
If you select the option "11", the SQL statement to delete an entry will be:
delete from info_user where id=1
I did not test this code and I'm assuming the ids are integers, but try it:
<?php
require_once "config.php";
$qid = "select id from info_user";
$arrayid = array();
$result = mysql_query($qid);
while( $res = mysql_fetch_array($result) ){
$arrayid[] = $res['id'];
}
if( isset($_POST['sub']) ){
$id = (int)$_POST['id']; // make sure it's an integer to avoid SQL injections
$q = "delete from info_user where id=$id";
$qq = mysql_query($q);
if( $qq ) {
$error = "you delete record ";
} else {
$error = "error in deleting";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>delete</title>
</head>
<body>
<?php
if( isset($error) ) :
echo '<p>', htmlspecialchars($error), '</p>';
elseif( !empty($arrayid) ) :
var_dump($arrayid);
endif;
?>
<form action="delete.php" method="post">
<select name="id">
<?php foreach($arrayid as $id): ?>
<option value="<?php echo (int)$id;?>">
<?php echo (int)$id; ?>
<?php endforeach; ?>
</option>
</select>
<br />
<input type="submit" name="sub" />
</form>
</body>
</html>

Categories