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
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.

Link to comment
Share on other sites

$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>';

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Lol, a bit ashaming.

 

But it works. Thanks guys for your help.

 

$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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.