Jump to content

Notice: Undefined variable


stublackett

Recommended Posts

Hi,

 

I'm constantly getting this PHP Error : Notice: Undefined variable $variablename

 

The $variablename changes to whatever I call it

 

What I'm trying to do is get a form to process itself and check for the errors before proceeding....

 

Here is my code

<form action="" name="addevent" method="post">
<form method="post" action="addevent.php" >  
<fieldset>
<legend>Add Event</legend>  
<h2>Event Details.........</h2>
<ol>  
<li>  
<label for="title">Event Title : </label>  
<input name="title" type="text" class="text" id="title" size="35" />
</li>  
<em><?php echo $error1 ; ?>
<li>  
<label for="location">Event Location : </label>  
<input name="location" type="text" class="text" id="location" size="35" />  </li>  
<li>  
<label for="date">Event Date : </label>  
<select name="day" class="text" id="day">
  <option selected="selected">Day :</option>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
  <option>9</option>
  <option>10</option>
  <option>11</option>
  <option>12</option>
  <option>13</option>
  <option>14</option>
  <option>15</option>
  <option>16</option>
  <option>17</option>
  <option>18</option>
  <option>19</option>
  <option>20</option>
  <option>21</option>
  <option>22</option>
  <option>23</option>
  <option>24</option>
  <option>25</option>
  <option>26</option>
  <option>27</option>
  <option>28</option>
  <option>29</option>
  <option>30</option>
  <option>31</option>
</select>
<select name="month" class="text" id="month">
  <option selected="selected">Month :</option>
  <option>January</option>
  <option>February</option>
  <option>March</option>
  <option>April</option>
  <option>May</option>
  <option>June</option>
  <option>July</option>
  <option>August</option>
  <option>September</option>
  <option>October</option>
  <option>November</option>
  <option>December</option>
</select>
<select name="year" class="text" id="year">
  <option>Year :</option>
  <option>2008</option>
  <option>2009</option>
  <option>2010</option>
</select><em></em></li>
<li>  
<label for="description">Event Description : </label>
<textarea name="description" cols="33" rows="6" class="text" id="description"></textarea>
</li>
</ol>  
</fieldset>  
<fieldset class="submit">  
<input class="submit" type="submit" value="Submit" />  
<input class="reset" type="reset" />
</fieldset> 
</form>

<?php 
//Collect Form Vars
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];

//Check Forms' Post Vars
  
if (!empty($_POST['title'])) {
$title = $_POST['title'];
}else{
$title = NULL;
$error1 = '<p><font color="red">You need to enter a title for your event</font></p>';
}

if (!empty($_POST['location'])) {
$location = $_POST['location'];
}else{
$location = NULL;
echo '<p><font color="red">You need to enter a location for your event</font></p>';
}

if (!empty($_POST['description'])) {
$description = $_POST['description'];
}else{
$description = NULL;
echo '<p><font color="red">You need to enter a description for your event</font></p>';
}

// If everything is filled out print the message.
if($title && $location && $description) {

{ 
// If all is ok, Insert into DB
$sql = "INSERT INTO $db_table(title,location,day,month,year,description) values ('$title','$location','$day','$month','$year','$description')"; 
// Incase needed($result = mysql_query($sql ,$db));
($result = mysql_query($sql ,$db) or die(mysql_error()));

echo "Thank you! Event has been added to the site!<br>You'll be redirected to the content management page in (5) Seconds";

echo "<br />";

echo "<meta http-equiv=Refresh content=5;url=cms.html>";

}
}
?>

 

How do I pair the ERROR Message next to the area where the value is !empty  ??? ???

Link to comment
Share on other sites

JavaScript has nothing to do with this.  Also, JavaScript is not a substitute for server-side validation.

 

The error results because you are echo'ing $error1 before assigning anything to it.  By default variables in PHP hold null and if you use the value of an undeclared variable you get null, with the option to have PHP complain about the error or not.

 

Try adding this at the top of your script:

$error1 = '';

Link to comment
Share on other sites

You need to validate all data when it reaches the server anyway, to detect/prevent sql injection attempts, to prevent broken queries when a field is empty, or to prevent incomplete data from being entered into your database.

 

For your posted code, the message about Undefined variable: error1 is because your form is unconditionally referencing that variable before it is ever set by the php validation code that is later in the file.

 

The messages about the day/month/year being undefined is because your logic is not checking if the form has been submitted before it access the form data.

 

You would need to rearrange the logic so that the the form processing code is only executed when the form has been submitted and the form and any validation errors are only echoed when necessary.

 

Also, your form's submit button has no name="..." parameter so there is no direct way for the code to check if it has been submitted.

 

 

 

Link to comment
Share on other sites

And since I know what the next question is going to be, here is a sample of how your program logic should flow to accomplish this -

 

<?php 
// create flag(s) for validation errors -
$errors = array(); // an array is generally used for this, where the index name would relate each error element to the field it corresponds to (should you want to individually output errors next to the field)

// check if the form has been submitted
if(isset($_POST['submit']))
{
//Collect Form Vars
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];

//Check Forms' Post Vars
  
if (!empty($_POST['title'])) {
	$title = $_POST['title'];
}else{
	$title = NULL;
	$errors['title'] = '<p><font color="red">You need to enter a title for your event</font></p>';
}

if (!empty($_POST['location'])) {
	$location = $_POST['location'];
}else{
	$location = NULL;
	$errors['location'] = '<p><font color="red">You need to enter a location for your event</font></p>';
}

if (!empty($_POST['description'])) {
	$description = $_POST['description'];
}else{
	$description = NULL;
	$errors['description'] = '<p><font color="red">You need to enter a description for your event</font></p>';
}

// If everything is filled out print the message.
if(empty($errors))
{
	// If all is ok, Insert into DB
	$sql = "INSERT INTO $db_table(title,location,day,month,year,description) values ('$title','$location','$day','$month','$year','$description')"; 
	// Incase needed($result = mysql_query($sql ,$db));
	($result = mysql_query($sql ,$db) or die(mysql_error()));

	echo "Thank you! Event has been added to the site!<br>You'll be redirected to the content management page in (5) Seconds";
	echo "<br />";

	echo "<meta http-equiv=Refresh content=5;url=cms.html>";
	exit;
}
}

// if the form was not submitted or there were validation errors, display the form -
if(!isset($_POST['submit']) || !empty($errors))
{
?>
<form action="" name="addevent" method="post">
<form method="post" action="addevent.php" >  
<fieldset>
<legend>Add Event</legend>  
<h2>Event Details.........</h2>
<?php
// check for any errors and display -
if(!empty($errors))
{
foreach($errors as $key => $error)
{
	echo "The $key field, contains this error: $error";
}
}
?>
<ol>  
<li>  
<label for="title">Event Title : </label>  
<input name="title" type="text" class="text" id="title" size="35" />
</li>  
<em>
<li>  
<label for="location">Event Location : </label>  
<input name="location" type="text" class="text" id="location" size="35" />  </li>  
<li>  
<label for="date">Event Date : </label>  
<select name="day" class="text" id="day">
  <option selected="selected">Day :</option>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
  <option>9</option>
  <option>10</option>
  <option>11</option>
  <option>12</option>
  <option>13</option>
  <option>14</option>
  <option>15</option>
  <option>16</option>
  <option>17</option>
  <option>18</option>
  <option>19</option>
  <option>20</option>
  <option>21</option>
  <option>22</option>
  <option>23</option>
  <option>24</option>
  <option>25</option>
  <option>26</option>
  <option>27</option>
  <option>28</option>
  <option>29</option>
  <option>30</option>
  <option>31</option>
</select>
<select name="month" class="text" id="month">
  <option selected="selected">Month :</option>
  <option>January</option>
  <option>February</option>
  <option>March</option>
  <option>April</option>
  <option>May</option>
  <option>June</option>
  <option>July</option>
  <option>August</option>
  <option>September</option>
  <option>October</option>
  <option>November</option>
  <option>December</option>
</select>
<select name="year" class="text" id="year">
  <option>Year :</option>
  <option>2008</option>
  <option>2009</option>
  <option>2010</option>
</select><em></em></li>
<li>  
<label for="description">Event Description : </label>
<textarea name="description" cols="33" rows="6" class="text" id="description"></textarea>
</li>
</ol>  
</fieldset>  
<fieldset class="submit">  
<input class="submit" type="submit" name="submit" value="Submit" />  
<input class="reset" type="reset" />
</fieldset> 
</form>
<?php
}
?>

Link to comment
Share on other sites

Is it always recommended to process a form within itself, Especially for validation?
Yes. The main point of having a single file is so that you don't need to duplicate any code or data. This also makes it possible to redisplay any values that where entered in the fields so that they are not lost when there are any validation errors.
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.