PHP dropdown not sending through POST - php

I have drop downs built by a PHP function pulling from MySQL tables. The drop downs work fine but the selected option isn't POSTing. I suspect the problem is with how the HTML select tag is created in PHP but I can't seem to find a way to correct it. I've searched around but nothing seems to work.
Here is the PHP function to build the drop down:
function contact_list(){
//Connection info
$dbhost = 'localhost';
$db = 'database';
$dbuser = 'user';
$dbpass = 'password';
//Define connection
$con = mysqli_connect($dbhost, $dbuser, $dbpass, $db);
//Check connection
if (mysqli_connect_errno()) {
echo " ERROR " . mysqli_connect_error() . " ERROR ";
}
//Define search strings for drop down
$contact_list = "SELECT * FROM CONTACT_LIST";
//Execute search of primary_contact table
if (!mysqli_query($con,$contact_list)) {
die('' . mysqli_error($con));
}
//Return primary_contact results as string
$contact_result = $con->query($contact_list);
//Build drop down
echo "<select name='Primary_Contact'>";
//Loop through results
foreach ($contact_result as $row)
{
echo "<option value='' >" .htmlspecialchars($row['Primary_Contact']). "</option>";
}
echo "</select><p></p>";
}
Here is a sample of the HTML. The Description text box is fine so I don't think the problem is with the form tags.
<form action="mod_output_test.php" method="POST">
<u>Description</u>: <p></p>
<input type="text" name="Description" size="48" value="<?=$Description;?>"> <p></p>
<!-- Primary Contact -->
<?php echo "<u>Primary Contact</u>:<p></p>Current value:&nbsp<b> " . $Primary_Contact . "</b>&nbsp New value: ";?>
<?php contact_list() ?>
<p></p>
<input type="submit">
</form>

Change
echo "<option value='' >" .htmlspecialchars($row['Primary_Contact']). "</option>";
to
echo "<option value='".$row['Id']."'>" .htmlspecialchars($row['Primary_Contact']). "</option>";
And I am guessing your dropdows is between a form tag.
in the top of the action file type this and tell us what you get
<?php
print_r($_REQUEST);
exit;
?>

echo "<option value='' >" .htmlspecialchars($row['Primary_Contact']). "</option>";
When you post like that, option value is empty. Add the id of the contact.
As they said, we see no <form> tag, we "assume" you add it before that piece. But the answer to "is not posting" is because value from <option> is empty.

Related

PHP selection of drop down menu not storing in variable

I have a simple table with staff names stored in the column f_operator_name.
I have a drop down menu in a php form with these staff names available for selection. Here is a snippet of the relevant the code:
<?php
echo "<h2>Operator: <select name=f_operator_id></h2>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['operator'];
}
?>
When someone selects the staff name I want it to be stored in a variable. I'm testing the submit form at the bottom which is intended to print out the name that has been selected ( in the variable operator), but it's not printing anything out. Can anyone see any issues?
EDIT *** Here's the updated code after some advice from Barmar with the variable information also, for some reason the echo statement still isn't working:
<?php
$connection = pg_connect("host=10.100.51.42 port=5432 dbname=reportingdb user=rai_gui password=password");
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['f_operator_id'];
}
?>
You can't put <select> inside <h2> and then put the <option>s and </select> outside it. HTML elements have to be nested properly, and <option> has to be inside <select>.
Change it to:
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
And the index in $_POST has to match the name of the <select>, so $_POST['operator'] should be $_POST['f_operator_id'].

POSTING an ID from one table to another

I have two tables in db, input table nd output table. input table got two columns $ID (autoincreament), $question. table 2 got two columns $Q_ID, Answer.
i made a php page (1) where i printed out the table 1 content (QUESTION) and i added a text area to get answers from visitors, along with it i also added a echo "hidden" field for ID from table one.
Now this php page 1 redirects to php page 2, where i POST the text area input into the answer column in table 2. But unfortunately, the ID is not able to get posted in q_id column of table 2...
I tried a lot but no hope..
My PHP script #1 as follows::
<div class="name">
<?php
$sql = "SELECT * FROM input";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$index = 0;
while($row = $result->fetch_assoc()) {
$index++; // stuff inside foreach goes here
?>
<div id="q">
<B><big><font color= #ba4a00> Q:</font></big> <?php echo $row["question"]; ?>
<?php
echo '<button class="add" id="add_'.$index.'"><B>Add Answer</B></button>';
echo '<form style="display:none;" name="answer_'.$index.'" method="post" action="output.php">';
echo '<input type="hidden" name="questionid" value="<?php echo $row[id]?>"/>';
echo '<textarea type="text" class="addtext" name="addtext" required id="addtext_'.$index.'" placeholder="Please type your answer here.." ></textarea>';
echo '<button onClick="addsubmit('.$index.');" type="submit" id="addsubmit_'.$index.'" class="addsubmit"><B>Submit</B></button>';
echo '</form>';
?>
</div>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
</div>
My PHP script #2 as follows::
<?php include('1.php'); ?>
<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "*******";
$dbname = "the_database";
$addtext = $_POST['addtext'];
$questionid = $_POST['questionid'];
$date = date_default_timezone_set('Asia/Kolkata');
$date = date('M-d,Y H:i:s');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO output (question_id, answer, date)
VALUES ('$questionid', '$addtext', '$date')";
if ($conn->query($sql) === TRUE) {
echo '<script language="javascript">';
echo 'alert("Your Answer has been Succesfully posted")';
echo '</script>';
echo '';
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
i want the respective question id to be passed to table 2 along with its answer.
Any help is greatly appreciated..
Give single-quote in $row['id']. Change this <?php echo $row[id] >? to <?php echo $row['id'] >?
echo '<input type="hidden" name="questionid" value="'. $row['id'].'"/>';
Already you are echoing the form using the ECHO Attribute in the PHP but after that also you have echoed it and that is wrong.
You have to make the code like this.
echo '<input type="hidden" name="questionid" value="'. $row['id'].'"/>';
Try this and share the thought about the code..
try enclosing id in double quotes in php 1:
echo '<input type="hidden" name="questionid" value="'.$row['id'].'"/>';
yip... looking a this a bit more closely you've got a bit of a problem with mixing single and double quotes in PHP #1.
for example, on this line the echo command ends after the second single quote after add_
echo '<button class="add" id="add_'.$index.'"><B>Add Answer</B></button>';
You need to use either single or double quotes consistently.

Fill drop down list on page load php

I have two input text fields where user has to specify the begin and end of the fly.
<input type="text" name="start" placeholder="Start destination">
<input type="text" name="end" placeholder="End destination">
I would like to change that and give user to chose start and end destination from database.
<select>
<option value="$id">$name</option>
</select>
I know how to get done if i read database and input values manually, but i know its posible if page loads and execute my SELECT QUERY.
So i have to create dropdown list and fill that with a values from database.
This dropdown list has to be filled when the page load.
Some idea for this ???
I am working with php.
Thank you in advance !!
EDIT : I get done this only with php.
<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "flights";
$conn = mysql_connect("$db_host","$db_username","$db_password") or die ("no conn");
#mysql_select_db("$db_name") or die ("no database");
if ($conn = true) {
// echo "";
}
//cyrilic
$sql = "SET NAMES 'utf8'";
mysql_query($sql);
//query for end
$sql="SELECT Distinct end from flights_table;";
$result=mysql_query($sql);
echo "<select name=\"city\">";
echo "<option>end destination</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['end']."'>".$row['end']." </option>";
}
echo "</select>";
?>
This php fires when page loads. Those select options i have putted in a form, and when form is submited, it fires php itself. I am getting selected options this way :
$startfly=$_POST['end'];
I am doing this for starting the flight :)
Thank you guys !
Try this :
At the top of page include your database connection file :
<?php
require "connection.php";
?>
Then :
<?php
$selectStart = "Start : <select name='start'>";
$selectEnd = "End : <select name='end'>";
$query = mysql_query("SELECT * FROM someTable ORDER BY dateField ASC");
if(mysql_num_rows($query) > 0)
{
while($row = mysql_fetch_assoc($query))
{
$selectStart .= "<option value='".$row['startItem']."'>".$row['startItemName']."</option>";
$selectEnd .= "<option value='".$row['endItem']."'>".$row['endItemName']."</option>";
}
}
$selectStart = "</select>";
$selectEnd = "</select>";
?>
In your HTML :
<form action='destinationPage.php' method='post'>
<?php
echo $selectStart;
echo $selectEnd;
?>
<input type='submit' value='Submit' />
</form>

PHP drop down list not generating

I need help regarding my PHP drop down list. I've created a Database call state. I'm trying to populate a state drop down on an html page using php connecting to mysql. Here is my code for the html page:
<form action="CreateUser.php" method="POST">
<label type='text'>State:</label>
<select name='state'>
<option value='0'>--Choose a State--</option>
<?php
$dbTable='states'
$QueryResult=msql_query('Select * from "$dbTable"');
while($Row = mysql_fetch_assoc($QueryResult))
{
?>
<option value="<?php echo $Row['StateID']; ?>">
<?php echo {$Row['StateName']}; ?>
</option>
<?php } ?>
</select>
<input id='movebutton' type = "Submit" name="submit" value="submit"/>
</form>
And here is my PHP code for the dbconnection:
$DBName = "business";
$DBConnect = #mysql_connect("localhost", "root", "");
if($DBConnect === FALSE)
{
echo "<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysql_errno() . ": " . mysql_error() . "</p>";
}
else
{
$DB = mysql_select_db($DBName, $DBConnect);
if(!$DB)
{
echo "<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysql_errno() . ": " . mysql_error() . "</p>";
mysql_close($DBConnect);
$DBConnect = FALSE;
}
}
Can someone tell me what I'm doing wrong? I've checked on this forum and also on YouTube regarding PHP dropdown. I'm new to php and still learning.
The query is invalid. It should be as follows:
$QueryResult=msql_query("Select * from `$dbTable`");
Update your query like $QueryResult=msql_query("Select * from '$dbTable'"); and also
Remove Curly braces inside option, or replace this code
<option value="<?php echo $Row['StateID']; ?>">
<?php echo $Row['StateName']; ?>
</option>

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