why I can not store selected value by $_POST in php - php

I create a dropdown list to select values:
<form action="." method="post" id="aligned">
<input type="hidden" name="action" value="update_customer">
<input type="hidden" name="customer_id"
value="<?php echo htmlspecialchars($customer['customerID']); ?>">
<label>Country:</label>
<select name="selected">
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass ='';
$db = 'tech_support';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn)
die('Could not connect: '. mysql_error());
mysql_select_db($db);
$selected= mysql_query("select * from countries where countryCode = '" .$customer['countryCode']. "'");
$sql = mysql_query("select * from countries order by countryName");
if($selectedrow = mysql_fetch_array($selected)){
echo "<option selected value=\"VALUE\">" . $selectedrow['countryName']."</option>";
}
//echo "<select>";
while ($row = mysql_fetch_array($sql)) {
echo "<option value =\"VALUE\">". $row['countryName']."</option>";
}
//echo "</select>";
?>
</select><br>
In another php file as below, I was trying to store the selected value for Country, but nothing is stored, why?
<?php $country_name = $_POST["selected"];
echo $country_name;//it always print out 'VALUE'-I guess it means null.
?>

You should use
echo "<option value ='".$row['countryName']."'>". $row['countryName']."</option>";
And I want to add something, MySQL is deprecated in PHP5 and removed from PHP7. what is your PHP version. If you use PHP 5 or later use mysqli.

Related

php $_POST select option value not working undefined index error

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to
mysql');
$dbname = 'vendordb';
mysql_select_db($dbname);
?>
<html>
<div class="row">
<form id="form1" name="form1" action="<?php $_SERVER['PHP_SELF'];?>"
method="post">
<?php
$result=mysql_query("select crtname from crtinfo");
echo "<select id='criteria1' name='criteria1'" . ">";
while($row = mysql_fetch_array($result))
{
echo "<option value=".$row[0]. ">". $row[0]. "</option>";
}
echo "</select>";
$crt1=$_POST['criteria1'];
echo $crt1;
?>
</form>
</div>
Unable to get dropdown value in $_POST
Error:
Notice: Undefined index: criteria1 in C:\xampp\htdocs\website\dropdowntest.php on line 25
cannot get value from $_POST
Although I'm not sure if this will fix it, this is a sample on how to prevent "Undefined index" errors from appearing by using isset()
if(isset($_POST["critera"])
{
$crt1=$_POST['criteria1'];
echo $crt1;
}
You cannot get the post value. Try this:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to
mysql');
$dbname = 'vendordb';
mysql_select_db($dbname);
?>
<html>
<div class="row">
<form id="form1" name="form1" action="<?php $_SERVER['PHP_SELF'];?>"
method="post">
<?php
$result=mysql_query("select crtname from crtinfo");
echo "<select id=criteria1 name=criteria1" . ">";
while($row = mysql_fetch_array($result))
{
echo "<option value=".$row[0]. ">". $row[0]. "</option>";
}
echo "</select>";
?>
<input type="submit" value="submit">
</form>
<?php
if(isset($_POST['criteria1'])){
echo $_POST['criteria1'];
}
?>
?>
</div>

PHP option value based on database table

My problem now:
once I echo the value from the database its woking good. but when I submit the form I got an empty value of the selected option.
Can any one help. I tried to used {} in he value code but it did not work.
What I want :
Set the the value of the selected option as it is on the database to insert it into another table.
<select class="form-control" name="CenterTO" id="CenterTO">
<option value="" selected>--select-- </option>
<?php
require("inc/DB.php");
$query = "SELECT centerName FROM jeCenter";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
while($row = mysql_fetch_array($result)){
echo '<option value="'.$row['centerName'].'">'.$row['centerName'].'</option>';
}
} else {
echo '<option>No data</option>';
}
mysql_close();
?>
Try this
<select class="form-control" name="CenterTO" id="CenterTO">
<option value="0" selected>--select--</option>
<?php
require("inc/DB.php");
$query = "SELECT centerName FROM jeCenter";
$result = mysql_query($query);
$count = count($result);
if (!empty($count)) {
while($row = mysql_fetch_array($result))
{
$name = $row['centerName'];
echo "<option value='$name'> $name </option>";
}
} else {
echo '<option>No data</option>';
}
mysql_close();
?>
</select>
You will have to "expand" your select query to include the ID of the row, and then instead of $row['centerName'] use $row['id'] (or what your ID column is named) in the value argument of the option element, and not the 'centerName'. If I understood correctly what you want, this should be it.
Edit: And do switch to mysqli_, mysql_ has been deprecated.
try to write like this:
echo '<option value="',$row["centerName"],'">',$row["centerName"],'</option>';
Warning:
Please don't use the mysql_ database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. Use mysqli or PDO extensions instead.
Solution
I recreated your issue and enhanced your code using mysqli extensions, and it's working fine. Take a look at the following code,
<?php
if(isset($_POST['submit'])){
// display submitted option
echo $_POST['CenterTO'];
}
?>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php
$host = "localhost";
$username = "root";
$password = "";
$dbName = "stackoverflow";
// Open a new connection to the MySQL server
$connection = new mysqli($host, $username, $password, $dbName);
// Check connection
if($connection->connect_errno){
// error
die('Connect Error: ' . $connection->connect_error);
}
// Execute query
$result_set = $connection->query("SELECT centerName FROM jeCenter");
?>
<form action="form_page.php" method="POST">
<select name="CenterTO">
<option value="" selected>--select-- </option>
<?php
// Check if number of rows is greater than zero or not
if($result_set->num_rows > 0){
// Loop through each record
while($row = $result_set->fetch_assoc()){
?>
<option value="<?php echo $row['centerName']; ?>"><?php echo $row['centerName']; ?></option>
<?php
}
}else{
?>
<option value="none">No data</option>
<?php
}
?>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
// Close connection
$connection->close();
?>

displaying data from MySQL in Datalist problems

I have a <datalist> element that gets a list of names from a table in a MySQL database. The list suggests names when you start typing, however there are some double Barrelled names in the Database, which it does not list.
Here is the code, showing the while loop etc.
<?php
$sql="SELECT * FROM students";
$result=mysql_query($sql);
echo '<input id="ChildName" name="ChildName" list="names" style=" width:450px; height:45px;font-size:20pt;" maxlength="15" size="6" > <datalist id="names">';
while($rows=mysql_fetch_array($result)){
?>
<option value="<?php echo $rows[Name]; ?>">
<?php
}
?>
</datalist>
I am a Network / System Administrator but I have write code while selecting my own available Database, Please change columns accordingly, here is the solution and hope this will work for you as well
<?php
$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = '';
$dbName = 'rizwanranjha';
$dbConn = mysqli_connect ($dbHost, $dbUser, $dbPass) or die ('mysqli connect failed. ' . mysqli_error());
mysqli_select_db($dbConn, $dbName) or die('Cannot select database. ' . mysqli_error());
$sql = mysqli_query($dbConn, "SELECT * FROM iph_country");
echo '<input id="ChildName" name="ChildName" list="names" style=" width:450px; height:45px;font-size:20pt;" maxlength="15" size="6" >';
echo '<datalist id="names">';
while ($row = mysqli_fetch_array($sql)) {
echo "<option value='". $row['country_id']. "'>" . $row['country_name'] ."</option>";
}
echo '</datalist>';
?>
<?php
$sql="SELECT * FROM students";
$result=mysql_query($sql);
echo '<input id="ChildName" name="ChildName" list="names" style=" width:450px; height:45px;font-size:20pt;" maxlength="15" size="6" > <datalist id="names">';
while($rows=mysql_fetch_assoc($result)){
?>
<option value="<?php echo $rows["Name"]; ?>">
<?php
}
?>
</datalist>
either you use mysql_fetch_assoc($result) and then use $row['columnname']
OR
you use mysql_fetch_array($result) and then use $row[1];

Removing selected data from drop down list(php)

I need to create a function which retrieves my data from my database into a drop down list, then choose and remove the selected data. I have my drop down list done & working but I'm experiencing some error with my 'Remove' function.
<body>
<?php
//connecting to database
$server = 'localhost';
$username = 'root';
$password = 'infosys';
$database = 'project';
mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
//dropdown list to retrieve sentence
$sql = "SELECT words FROM sentence";
$result = mysql_query($sql);
echo "<select name
='dropdownlist'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['words'] ."'>" . $row['words'] ."</option>";
}
echo "</select>";
?>
<form method="post" action="remove.php">
<input type="submit" value="Remove" />
</form>
<a href="index.php" >View list</a>
</body>
Followed by another php file
<?php
$server = 'localhost';
$username = 'root';
$password = 'infosys';
$database = 'project';
mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$dropdownlist1 = $_POST['dropdownlist'];
$dropdownlist2 = $dropdownlist1.value;
if(isset($_GET['id'])) {
$mysql_query("DELETE FROM 'words' WHERE id = $dropdownlist2");
header("location: index.php");
exit();
}
?>
Move your Select box or dropdown inside the form tag,
<form method="post" action="remove.php">
<select name ='dropdownlist'>
<?php while ($row = mysql_fetch_array($result)) { ?>
<option value='<?php echo $row['words'];?>'><?php echo $row['words'];?></option>
<?php } ?>
</select>
<input type="submit" value="Remove" />
</form>
in php,
if(isset($_POST['dropdownlist'])) {
$dropdownlist1 = $_POST['dropdownlist'];
mysql_query("DELETE FROM `sentence` WHERE `words` = '$dropdownlist1' ");
header("location: index.php");
exit();
}
Note: Use mysqli_* or PDO functions instead of using mysql_* functions(deprecated)

Turning a mysql column into an array and using the array in a dropdown

Long time listener, first time caller. I'm having trouble with pulling a column called "Rep_IP" from a mysql table called "roster", turning it into an array, and then using that array to populate a dropdown in html. I've tried several suggestions listed here as well as other places and I'm not having any luck. The page shows up just fine but the dropdown has no options to select. I figured I would see if one of you could tell me what I am doing wrong here.
<html>
<body>
<form action="insert.php" method="post">
<p>Rep ID:</p>
<?php
$con = mysql_connect("localhost", "root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("rep_stats", $con);
$query = "SELECT Rep_ID FROM roster";
$result = mysql_query($query) or die ("no query");
$result_array = array();
echo "$query"
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row;
}
?>
<select name="Rep_ID">
<?php
foreach($result_array as $rep)
{
echo "<option value=" . $rep['Rep_ID'] . ">" . $rep['Rep_ID'] . "</option>";
}
?>
</select>
Issues Handled: <input type="number" name="IssuesHandled">
Hours Worked: <input type="number" step="any" name="HoursWorked">
<input type="submit">
</form>
</body>
</html>
As you can see, the drop down is part of a form that is used to create an entry in a new table as well. I don't know if that makes a difference but I figured I would point it out.
Try this.
<select name="Rep_ID">
<?php
while($row = mysql_fetch_assoc($result))
{
echo "<option value=" . $row['Rep_ID'] . ">" . $row['Rep_ID'] . "</option>";
}
?>
</select>
Try this:
<?php
function select_list(){
$host = "host";
$user = "user";
$password = "password";
$database = "database";
$link = mysqli_connect($host, $user, $password, $database);
IF (!$link)
{
echo ('Could not connect');
}
ELSE {
$query = "SELECT Rep_ID FROM roster";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo "<option value=" . $row['Rep_ID'] . ">" . $row['Rep_ID'] . "</option>";
}
}
mysqli_close($link);
}
$begin_form =
<<< EODBEGINFORM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Insert data </title></head>
<body>
<form action="insert.php" method="post" name="form1">
<p>Rep ID:</p>
<select name="reps" form="form1">
EODBEGINFORM;
$end_form =
<<< EODENDFORM
</select><br>
Issues Handled: <input type="text" size="12" name="IssuesHandled"><br>
Hours Worked: <input type="text" size="12" name="HoursWorked"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
EODENDFORM;
echo $begin_form;
echo select_list();
echo $end_form;
?>
You will notice that I have used MYSQLI_ istead of MYSQL_ the reason is that this better for new code, see the comments above.
I debugged your code and ran I to a lot of problems.:-( The main problem was:
echo "$query" Your forgot the semicolon at the end of the line.
Good luck with you project.

Categories