PHP - Database Form dropdown Undefined index

html
mysql

#1

Im trying to create a form with drop down which is populated from database, when i select the value and i press submit i get an error as described below please i need some help.

<?php
require_once ('conn.php');
$sql = mysqli_query(db_connect(), "SELECT Title FROM $table WHERE NOT Status='Links_added' ORDER BY Title ASC");

?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<h2>Test</h2>


<form method="post" action="pass.php" name="resources">


	Name: <select  name="title_name">
		<?php
		echo "<option value=name1>" . "Select Title" . "</option>";
		while ($row = $sql->fetch_assoc()){
			echo "<option value='" .$row['Title'] ."'>" . $row['Title'] . "</option>";
		}
		?>
	</select>
	<br><br>
	<input type="submit" name="submit" value="Submit">

</form>
</body>
</html>

pass.php

<?php
var_dump($_POST);
    echo $_POST["title_name"];

Error

array(0) { }
Notice: Undefined index: title_name in pass.php on line 3

When i add to pass.php this :

<?php
if (isset($_POST['submit'])) {

    echo $_POST["title_name"];
}

I dont get any response, why there is no data after i select and submit the form ?!


#2

you have to check every variable for existence before using it. e.g. with isset, or empty, or the coalesce operator. var_dump it when you are not sure about what it contains.


#3

i have done all those but there is no data in the $_POST


#4

Did you try

var_dump($_POST);

to see what is actually arriving? (ETA, I see you did, sorry).

In any case, I don't understand this line:

echo "<option value=\"name2\">" . $row['Title'] . "</option>";

which surely means that the form will pass "name2" for every single option in the list?

Could there be anything in your included file that is upsetting the form somehow? Maybe move that include to outside the form definition?


#5

I changed the code but still no success.


#6

have a look at getallheaders()


#7

What did you change it to? How does it look now?


#8

You can look the question above, it is updated too.


#9

What environment are you running in? I can't see anything in your code that would cause the problem, as long as there's nothing in your included file to unset the array. I did pick up on a problem with PHPStorm causing something similar.

The workaround in your pass.php was something like:

<?php
$postdata = file_get_contents("PHP://input");
var_dump($postdata);
?>

#10

If POST isn't getting sent might it be because the form is broken?

ie. Is the HTML for the form as it looks in view-source - not the PHP file - like you expect it to be and valid?


#11

You are missing the quotes for the value which shouldn't even have a value for this part in the first place.


#12

Is that a requirement? I thought that was a good thing, but not actually required unless the parameter value contains a space or some other character that might cause confusion.


#13

I usually put attribute values inside quotation marks out of long habit. But going by the W3C HTML5 documentation:

Attributes can be specified in four different ways:
- Empty
- Unquoted
- Single-quoted
- Double-quoted

https://www.w3.org/TR/html5/syntax.html#elements-attributes


#14

Same here, but as you confirmed, it's not a reason for the form to not work.


#15

What does the full code for pass.php look like? And also what is $table? You have it as "SELECT Title FROM $table WHERE NOT Status='Links_added' ORDER BY Title ASC" So what is $table set as?


#16

I've presumed that $table was defined in the database include file.


#17

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.