Jump to content

I don't understand these errors


php_novice2007

Recommended Posts

This is my code, it allows the user to alter file contents

 

<?php
$data = $_POST['choice'];
list($front,$middle, $back)=split('[._]',$data);

if ($back == "delete") {
	//go through photoList.txt, look for $front.$middle, delete that
	//delete $front.$middle from image folder
	//delete $front.".txt" from annotation and title folder
} else {	//$back = alter
?>
	<form method="post" action="<?=$_SERVER['PHP_SELF'] ?>">
      <table border=0>
	<tr><td>New Title:</td>
      <td valign=middle><input type="text" size=40 name=title></td></tr>
        	<tr></tr>
        	<tr></tr>
       	<tr><td>New Annotations:</td>
            <td valign=middle><textarea cols=50 rows=5 name=annotation></textarea></td></tr>
        	<tr></tr>
        	<tr></tr>
        	<tr>
            <td><button type=submit name=submit value=upload><font color=darkblue><b>Alter</b></font></button></td>
            <td><button type=submit onclick=self.close()><font color=darkblue><b>Cancel Alter</b></font></button></td>
        	</tr></table></form>

<?php	
	if(!isset($_POST['Submit'])){ /* if the $_POST['Submit'] var does not exist, then the form has not been submitted, so we need to show the form */
		//do nothing
	}else{ 
		// it does exist so we need to process the form.
		$title = $_POST['title'];
		$annot = $_Post['annotation'];
		$handle1 = fopen("./titles/".$front.".txt", 'w');
		fwrite($handle1,$title);
		fclose($handle1);

		$handle2 = fopen("./annotations/".$front.".txt", 'w');
		fwrite($handle2,$annot);
		fclose($handle2);
	}
	}
?>	

 

The errors generated are:

 

Notice: Undefined index: choice in I:\webpages\alterordelete1.php on line 2

 

Notice: Undefined offset: 2 in I:\webpages\alterordelete1.php on line 3

 

Notice: Undefined offset: 1 in I:\webpages\alterordelete1.php on line 3

 

What does undefined offset mean..? The name of the above php file is called alterordelete1.php.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/43072-i-dont-understand-these-errors/
Share on other sites

hm actually the page that calls alterordelete1.php is alterordelete.php, and it has a form on that...

 

alterordelete.php

<?php
$myFile = "photoList.txt"; 
$handle = fopen($myFile, 'r'); 
while (!feof($handle)) { 
	$data = fgets($handle); 
	if (!($data == "")) {
		list($front,$back)=split('[.]',$data);

?>
		<img src="./images/<?php echo $data ?>" width="300" height="200"></img>
		<br>
		<Form action="alterordelete1.php" method="post">
		<INPUT type="radio" name="choice" value="<?php echo $data ?>_alter">Alter<BR>
		<INPUT type="radio" name="choice" value="<?php echo $data ?>_delete">Delete<BR>
		<input type=submit value="submit"><br><hr>
<?php
	} 
}
fclose($handle); 
?>

 

so shouldn't $_POST['choice'] be defined..?

You're getting these warnings because the $_POST array isn't filled until after the form is submitted. If you're just invoking this script from the URL or via a link and not from a POSTed form you will get the warnings.  You need to check to see if the array references exists before doing the assignment.

 

<?php
$data = (isset($_POST['choice'])?$_POST['choice']:''; //if the $_POST['choice'] is set, use it, otherwise set $data to the empty string
list($front,$middle, $back)=split('[._]',$data);
?>

 

 

In your later "if", it's much nicer to check for the positive and do something then the way you're doing it.

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

 

Ken

 

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.