Jump to content

Recommended Posts

could someone see where the error is coming from? it appears only when i submit the form

 

Notice: Undefined index: ads_title in C:\wamp\www\ads\post_ads.php on line 23

 


<?php
include("config.php");
include("functions/db_functions.inc.php");
include("functions/gn_functions.inc.php");
if(count($_POST)){
	$postData = $_POST;
	$postData = cleanData($postData);
        $error  = postErrorCheck($postData);

    printr($_POST);

if(count($error) == 0) {


}
    }
?>

<form action="post_ads.php" name="postads" method="POST" enctype="multipart/form-data"/>
Ad Title*: <br/>
<input type="text" name="ads_title" value="" maxlength="100"/>
<?php
		if($error['ads_title'] !='') ///////ERROR IS ON THIS LINE
		{
			echo '<div class="error_msg">'.$error['ads_title'].'</div>';
		}

?>
<br/>
Upload Image: <br/>
	<input type="file" name="ads_image" value=""/>
<br/>
Your Name*: <br/>
	<input type="text" name="postersname" value=""/>
<br/>
Choose Category*: <br/>
<select name="category"/>
	<option value="0"/>Select Category</option>
	<option value="Events_Announcements"/>Events/Announcements</option>
	<option value="Career_Jobs"/>Career/Jobs</option>
	<option value="Buy_Sell"/>Buy/Sell</option>
	<option value="Realestate"/>Real Estate</option>
	<option value="Services"/>Services</option>
	<option value="Vehicles"/>Vehicles</option>
	<option value="Software"/>Software</option>
	<option value="Pets"/>Pets</option>
	<option value="Music"/>Music</option>
	<option value="Personals"/>Personals</option>
	<option value="Other"/>Other</option>
</select>
<br/>
Choose Type*: <br/>
<select name="ads_type"/>
	<option value="0"/>Select Ad Type</option>
	<option value="Offered"/>Offered</option>
	<option value="Wanted"/>Wanted</option>
	<option value="Other"/>Other</option>
</select>
<br/>
Choose State*: <br/>
<select name="state"/>
	<option value="0"/>Select State</option>
	<option value="Assam"/>Assam</option>
	<option value="Arunachal Pradesh"/>Arunachal Pradesh</option>
	<option value="Meghalaya"/>Meghalaya</option>
	<option value="Mizoram"/>Mizoram</option>
	<option value="Manipur"/>Manipur</option>
	<option value="Nagaland"/>Nagaland</option>
	<option value="Tripura"/>Tripura</option>
	<option value="Other"/>Other</option>
</select>
<br/>
City/Town*: <br/>
	<input type="text" name="ads_location" value=""/>
<br/>
Your E-mail*: <br/>
	<input type="text" name="email" value=""/>
<br/>
Your Phone: <br/>
	<input type="text" name="phone" value=""/>
<br/>
Ad Description*: <br/>
	<textarea name="description" cols="40" rows="15"/></textarea>
<br/>
	<input type="submit" name="submit" value="Post Ad"/>
</form>

Link to comment
https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/
Share on other sites

A notice is not an error -- it is a "notice". 

 

It's very clear what the notice is telling you.

 

You are attempting to make a boolean evaluation using $error['ads_title'] however, this array element doesn't exist.

 

In a situation like this you can use isset() first to shortcircuit evaluation.

 

if (isset($error['ads_title']) && $error['ads_title'] != '') 

 

friends it happening for all fields only when submited the form with value in it, i got the same code working on one of my other script which does not output any error at all, here is my function

 

function postErrorCheck($data){	
	   $errormsg = array();

	   if($data['ads_title'] == '') {
	   $errormsg['ads_title'] = 'Advertisement title is required!';
	   }else if(strlen($data['ads_title']) > 100) {
	   $errormsg['ads_title'] = 'Title cannot be more than 100 characters';
		}	


		return $errormsg;
}

Well the problem exists in the code, whether you happen to stumble upon it in testing or not.  If you try and access an element of an array that doesn't exist you'll get a notice, unless you either insure that it exists, or check using isset() to short circuit the access as I showed you.

Ok, well, as I stated previously it's not an error.  You can control the "level" of error reporting your site provides, and furthermore it should be logged and not displayed on a production site.  Really these notices exist to help you write really clean code, but in many cases, even if you don't write the cleanest level of code, you will not have issues.

 

See error_reporting

 

 

The setting you want:

 

error_reporting(E_ALL & ~E_NOTICE);

 

For a production server, you want your php.ini (depending on the php version) to set this either to be off, or to send to stderr.  You don't want to have to set this in your scripts. 

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.