Hi
I have a problem With the $_POST

Here is the code i have

i called this albums.php

<?php
Echo"
<table cellpadding='5' width='100%' border='1' bgcolor='white'>
<tr>
<td width='50%' align='left'>
Usere Alben die schon bestehen.
</td>
<td align='right'>
<form action='createalbum.php' method='POST'>	
	<table>
		<tr>
		<td align='left'>
		<font face='arial' size='2'>
		Albumauswahlliste.</font>
		</td>
		</tr>
		<tr>
		<td align='left'>
		
			 
		 <select name='albumname'>
 
  <option value='' selected='' >
<option value='Weinachtfeier'>Weinachtfeier</option>
<option value='Vereinsauftritte'>Vereinsauftritte</option>
<option value='Auftaktsitzungen'>Auftaktsitzungen</option>
<option value='Prinzenfuhrungen'>Prinzenfürungen</option>
 <option value='Kindersitzungen'>Kindersitzungen</option>
<option value='Kostumball'>Kostümball</option>
<option value='sonstiges'>sonstiges</option>
</select>
 
</select><input type='submit' name='submit' value='Senden. '>
	 		<a href='logout.php'>Abmelden</a><br>
		</td>
		</tr>
		</table></form>
</td>
</tr>
<tr>
<td colspan='2'>
</td>
</tr>
</table>
";	
	}
	else
	
	echo "bla bla";

i call this createalbum.php

<?php
if(isset($_SESSION['user']))
		{
		require("connect.php");

$id = $_SESSION['id'];
$albumname = $_POST['albumname'];
If ($_POST['albumname']=="")
{
echo "
	<form action='albums.php'
	<div  style='background-color: white; border: solid red 3px; width:52%;'><br>Sie haben kein album gewählt <br>aus der Albumauswahlliste.<br>Klicken Sie auf OK, um zurückzukehren.<br><br><input type='submit' value='OK' ><br>&nbsp;</div></form>
	";//do not reload page	
	die;
}
if ($albumname);
{
if (strlen($albumname)>100)
echo "albumname ist zu lang sie darf nicht mehr als 100 zeichen sein.";
else
{	
echo"
<form action='albumcreated.php' method='POST'>
<table calpadding='7' width='100%' border='1'>

<tr>
<td width='40%'>
<font face='arial' size='2' color='white'>Geben Sie eine kurze Beschreibung<br> des Fotos für das Album</font><br><font face='arial' size='3' color='white'><b>$albumname</b></font>. 

<input type='text' name='$albumname' value='$albumname'>
<textarea name='albumdescription' name='beschrijving' maxlengt='200'></textarea><br>
</td>
<td>
<font face='arial' size='2' color='white'>Suche nach neuen Album Cover</font><br>
<input type='hidden' name='MAX_FILE_SIZE' value='102400'><input type='file' name='userfile' accept='image/jpeg'><input type='submit' value='Senden'>


</td>


</tr>
</table>
</form>
";
}
}

}
else
	
	echo "bla bla":
?>

and the last one called albumcreated.php this is where the $_POST is not working.
and it gives me the folowing error
Notice: Undefined index: $albumnaam on line 6

<?php
	session_start();
	
	if(isset($_SESSION['user']))
		{
		$albumnaam=($_POST["$albumname"]); //this is lin 6		
$conn1=mysql_connect("localhost","bla", "blabla"); 
mysql_select_db("fotos") or die($error);
if (!$conn1) {
    die('Could not connect: ' . mysql_error());
}	
$sql1 ="CREATE TABLE IF NOT EXISTS $albumnaam (
	id int(11) NOT NULL auto_increment,
	albumname VARCHAR(25) NOT NULL,
	PRIMARY KEY (id)
	)";
	mysql_query($sql1) or die(mysql_error());
	
	$create = mysql_query("INSERT INTO $albumnaam VALUES('','$albumnaam')");
	echo"datatable created";
	echo"$albumnaam naam toegevoegd in table $albumnaam";
	 mysql_close($conn1);
	 }
	 ?>

if i put the code from albumcreated.php into createalbum.php just before echo"
<form action='albumcreated.php' it works perfect.

can someone exlain why this is not working ?

Thanks in advice John

Dani AI

Generated

A few clarifications and a short checklist that explains what actually caused the "Undefined index" problem and how to avoid it going forward.

The notice means the POST key your script tried to read did not exist in the incoming request — common causes are a mismatched name attribute, the form not using method="POST", or reading the wrong variable name in the target script. was on the right track: avoid using a PHP variable as the form field name (it easily produces a mismatch). Confirm what the browser actually sent (open DevTools > Network and inspect the form payload, or temporarily dump the request with var_dump($_POST) on the receiving page). (php.net)

Make sure session handling and input reads are done safely and in the right order. Always call session_start() at the top of the script (before sending output). Use a guarded read (either isset() / ?? or filter_input()) so missing fields don’t emit notices. Example pattern to read safely (showing intent, not a full app):

session_start();                  // first line
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $album = filter_input(INPUT_POST, 'album', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? '';
}

Using filter_input() avoids directly indexing $_POST without checks. (php.net)

A few security and design notes: do not build SQL by concatenating raw user input and avoid creating one DB table per album. Use a normalized schema (albums table + photos table) and parameterized queries (PDO or mysqli) — the old mysql_* extension is removed in modern PHP, and prepared statements are the standard defense against SQL injection. (php.net)

Quick checklist to resolve the exact thread problems:

  • Verify the <input name="..."> value exactly matches the key you read with $_POST/filter_input.
  • Ensure the form has method="POST" (and enctype="multipart/form-data" for file uploads).
  • Avoid dynamic field names unless you handle them explicitly on the receiver.
  • Watch for simple logic bugs (for example an accidental semicolon like if ($albumname);).
  • Use isset() or ?? to supply safe defaults and stop notices.

This explains why the page worked when you echoed the logic into the same script (the variable existed there) but failed after redirecting to a separate handler that expected a different/missing key. ’s final comment suggests the rename/fix worked — those steps above will make it robust and future-proof.

Recommended Answers

All 4 Replies

it's better not to use variable for $_POST

gives a different name: i put "aname" for example

line 31 of createalbum.php
<input type='text' name='$albumname' value='aname'>

line 6 of albumcreated.php
$albumnaam=($_POST["aname"]); //this is lin 6

let me know how it works.

it's the same error

Notice: Undefined index: aname in line 6

line 31 of createalbum.php
<input type='text' name='aname' value='$albumname'> //does this input box show the correct variable $albumname?

line 6 of albumcreated.php
$albumnaam=$_POST["aname"]; //this is lin 6

echo $albumnaam; // does this give you the correct result?

Thank you.
for your help this is working now.

Learned a bit more.

Greetings John.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.