OrangeTux Posted August 9, 2010 Share Posted August 9, 2010 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? Quote Link to comment Share on other sites More sharing options...
schilly Posted August 9, 2010 Share Posted August 9, 2010 Try this: $titel = $_POST["titel[$n]"]; or $titel = $_POST['titel[' . $n . ']']; When you use a string with single quotes, PHP will not evaluate any variables inside it. Quote Link to comment Share on other sites More sharing options...
linus72982 Posted August 9, 2010 Share Posted August 9, 2010 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. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 9, 2010 Share Posted August 9, 2010 $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 Link to comment Share on other sites More sharing options...
linus72982 Posted August 9, 2010 Share Posted August 9, 2010 $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? Quote Link to comment Share on other sites More sharing options...
OrangeTux Posted August 10, 2010 Author Share Posted August 10, 2010 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. Quote Link to comment Share on other sites More sharing options...
schilly Posted August 10, 2010 Share Posted August 10, 2010 It means the variable isn't in that array. Print out your POST array to see what it looks like. print_r($_POST); Quote Link to comment Share on other sites More sharing options...
OrangeTux Posted August 10, 2010 Author Share Posted August 10, 2010 print_r($_POST); doesn' t work, but echo '<pre>' . print_r($_POST, true) . '</pre>'; gives Array ( ) Does this mean that $_POST is empty? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 10, 2010 Share Posted August 10, 2010 Make sure you have submitted your form first otherwise $_POST wont contain any data. Quote Link to comment Share on other sites More sharing options...
schilly Posted August 10, 2010 Share Posted August 10, 2010 print_r($_POST); doesn' t work Depending on your HTML format it may or may not show up when rendered by the browser but it will be in your source code. Quote Link to comment Share on other sites More sharing options...
OrangeTux Posted August 11, 2010 Author Share Posted August 11, 2010 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 Quote Link to comment Share on other sites More sharing options...
schilly Posted August 11, 2010 Share Posted August 11, 2010 There is no method set on your form tag. <form action="edit.php" method="post"> Quote Link to comment Share on other sites More sharing options...
OrangeTux Posted August 12, 2010 Author Share Posted August 12, 2010 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 Quote Link to comment Share on other sites More sharing options...
linus72982 Posted August 12, 2010 Share Posted August 12, 2010 Yeah, I see where I made my mistake, glad you got it working Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.