Jump to content

A $_POST['titel[$n]'] problem


OrangeTux

Recommended Posts

I'm making a script for editing lines in a text file.

 

PHP code form form script:

			
				$n = 0;
				$bestand = "../test.txt"; 
				$gegevens = file($bestand);

				//count lines titel.txt
				$lines = count(file($bestand));

				while($n < $lines) {
					$items = explode("|", $gegevens[$n]);
					$titel = $items[0]; 		
					$beschrijving = $items[1];
					$n=$n+1;

					echo "<tr>
							<td><input type=\"text\" class=\"edit\" name=\"titel[".$n."]\" value=\" ".$titel." \" /></td>
							<td><textarea name=\"beschrijving[".$n."]\">".$beschrijving."</textarea></td>
						  </tr>";
				}

 

Code of processing script

<?php
$n = 1;

$bestand = "../test.txt";
$lines = count(file($bestand));

while($n < $lines){

	//Set variables
	$titel = $_POST['titel[$n]'];

	//Replace 'Enter' for '<br />'
	$_POST['beschrijving[$n]'] = preg_replace ("#\r?\n#", '<br />', $_POST['beschrijving[$n]']);
	$beschrijving = $_POST['beschrijving[$n]'];

	//Write $titel
	$handle = fopen($bestand, "a");
	fwrite($handle, "".$titel."|");
	fclose($handle);

	//Write $beschrijving
	$handle = fopen($bestand, "a+");
	fwrite($handle, "".$beschrijving." ");
	fclose($handle);

	$n=$n+1;
}
?>

 

De form scripts works fine, no problems. But the processing script give me the next notices:

 

Notice: Undefined index: titel[$n] in /var/www/NellekeTekent/cms/edit.php on line 10 Notice: Undefined index: beschrijving[$n] in /var/www/NellekeTekent/cms/edit.php on line 13 Notice: Undefined index: titel[$n] in /var/www/NellekeTekent/cms/edit.php on line 10 Notice: Undefined index: titel[$n] in /var/www/NellekeTekent/cms/edit.php on line 10 Notice: Undefined index: titel[$n] in /var/www/NellekeTekent/cms/edit.php on line 10 

 

I gues the problem is '$n' when setting $titel and $beschrijving.

But I don't know how to solve it.

Can you help me?

 

Link to comment
https://forums.phpfreaks.com/topic/210243-a-_posttiteln-problem/
Share on other sites

I didn't delve too far into the script, but I think your problem is with this line:

 

$titel = $_POST['titel[$n]'];

 

From my quick once-over, I think it should be:

 

$titel = $_POST['titel['.$n.']'];

 

The difference is the '. and .' added before and after the $n.  As it is part of a string, I don't think you can just insert a variable like that without adding it to the string.

$titel = $_POST['titel[$n]'];

Should be

$titel = $_POST['titel'][$n];

 

When you include square backets within a fields name it'll be sent as an array. To see how the _POST array is formatted you can use this

echo '<pre>' . print_r($_POST, true) . '</pre>';

  Quote

$titel = $_POST['titel[$n]'];

Should be

$titel = $_POST['titel'][$n];

 

When you include square backets within a fields name it'll be sent as an array. To see how the _POST array is formatted you can use this

echo '<pre>' . print_r($_POST, true) . '</pre>';

 

You confused me, wouldn't $_POST['titel'][$n] be a multidimensional array?  It seems like he's actually setting the name to something explicit, the variable being added to the text of the name.  If $n were 1, the name of the element would be titel1, for 2 it would be titel2, from there, $_POST['titel2'] should grab it.  It seems like $_POST['titel'][2] would never be set.  I'm still fairly new at all this, could you explain how you got here?

I changed to $titel = $_POST['titel['.$n.']']; but it still don't work.

 

Notice: Undefined index: titel[1] in /var/www/NellekeTekent/cms/edit.php on line 10 Notice: Undefined index: beschrijving[1] in /var/www/NellekeTekent/cms/edit.php on line 13

 

I don't know what the problem is.

The formcode produce a <input type="text" name="titel[1]" value="test" />

and the processcode needs a field named 'titel[1]'..

 

Can you help me, again.

Ofcourse I submit the form. Here are te complete scripts

 

edit_form.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/transitional.dtd">
<?php session_start(); require 'approve.php'; ?> 
<html>
<head>
	<title>Wijzigingen | NellekeTekent</title>
	<link rel="stylesheet" type="text/css" href="cms.css">
</head>

<body>
	<form action="edit.php">
		<table>
			<tr>
				<th>Titel</th>
				<th>Beschrijving</th>
			</tr>

			<?php			
				$n = 0;
				$bestand = "../test.txt"; 
				$gegevens = file($bestand);

				//count lines titel.txt
				$lines = count(file($bestand));

				while($n < $lines) {
					$items = explode("|", $gegevens[$n]);
					$titel = $items[0]; 		
					$beschrijving = $items[1];
					$n=$n+1;

					echo "<tr>
							<td><input type=\"text\" class=\"edit\" name=\"titel[".$n."]\" value=\" ".$titel." \" /></td>
							<td><textarea name=\"beschrijving[".$n."]\">".$beschrijving."</textarea></td>
						  </tr>";
				}
			?>
		</table>
		<input type="submit" value="Bewerken" name="edit" />
	</form>
</body>
<html>

 

edit.php

<?php
echo '<pre>' . print_r($_POST, true) . '</pre>';

$n = 1;

$bestand = "../test.txt";
$lines = count(file($bestand));

while($n < $lines){

	//Set variables
	$titel = $_POST['titel['.$n.']'];

	//Replace 'Enter' for '<br />'
	$_POST['beschrijving['.$n.']'] = preg_replace ("#\r?\n#", '<br />', $_POST['beschrijving['.$n.']']);
	$beschrijving = $_POST['beschrijving[['.$n.']'];

	//Write $titel
	$handle = fopen($bestand, "a");
	fwrite($handle, "".$titel."|");
	fclose($handle);

	//Write $beschrijving
	$handle = fopen($bestand, "a+");
	fwrite($handle, "".$beschrijving." ");
	fclose($handle);

	$n=$n+1;
}
?>

 

Hopefullym someone see what is wrong

Lol, a bit ashaming.

 

But it works. Thanks guys for your help.

 

  Quote

  Quote

$titel = $_POST['titel[$n]'];

Should be

$titel = $_POST['titel'][$n];

 

When you include square backets within a fields name it'll be sent as an array. To see how the _POST array is formatted you can use this

echo '<pre>' . print_r($_POST, true) . '</pre>';

 

You confused me, wouldn't $_POST['titel'][$n] be a multidimensional array?  It seems like he's actually setting the name to something explicit, the variable being added to the text of the name.  If $n were 1, the name of the element would be titel1, for 2 it would be titel2, from there, $_POST['titel2'] should grab it.  It seems like $_POST['titel'][2] would never be set.  I'm still fairly new at all this, could you explain how you got here?

 

And

$titel = $_POST['titel'][$n];

is the right code.

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.