Welcome to WebmasterWorld Guest from 54.198.200.120

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Trouble understanding a variable error

     
10:25 pm on Jul 27, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 4, 2006
posts:85
votes: 0


I'm new to php and can't understand why I'm getting this error: Undefined variable: error in root/breeder-form.php

Can anyone see what I'm doing wrong?

(breeder-list-1.php)
<?php
$response;
$valid = validate_form();
if (isset($_POST['submit']))
{
if ($valid === true)
{
include ('listing-review.php');
}
else
{
include ('breeder-form.php');
$error = $valid;
}
}
else
{
include ('breeder-form.php');
}

function validate_form()
{
$response = true;
if ($_POST['title'] == '')
{
$response = 'Please enter a title for your listing.';
}
return $response;
}

?>

(breeder-form.php)
<h4>Please fill out the following form to creat your listing:</h4>

<?php echo $error;?>
<form name='form1' id='form1' enctype='multipart/form-data'
action = "<?php echo $_SERVER['PHP_SELF'];?>" method = "post" >
<fieldset>
<legend>Contact Information</legend>
Listing Title:

<input type='text' name='title' size='30' maxlength='70' value='<?php echo $_POST['title'];?>' />

</fieldset>
<input type='submit' name='submit' value='Next ->' />
</form>

(listing-review.php)
<div id='listing-review'>
<?php echo '<strong>' . $_POST['title'] . '</strong> ' . $_POST['radAvailability'];?>
<form>
<input type='submit' name='submit' value='Edit'/>
</form>
</div>

11:05 pm on July 27, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


Hi horseatingweeds,

You are trying to access a variable before its set. Look into isset [uk2.php.net] and change this line:

<?php echo $error;?>

to this:

<?php echo (isset($error)? $error : '');?>

Basically you are echoing the value to the page before the variable exists. Hope that makes sense.

dc

11:21 pm on July 27, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 4, 2006
posts:85
votes: 0


Is this correct? It's turning out funny in my text editor.

<?php echo (isset($error)? $error : ");?>

Is the " suposed to be there? Either way I'm getting errors.

12:29 am on July 28, 2007 (gmt 0)

Full Member

5+ Year Member

joined:July 13, 2007
posts:235
votes: 0


Yes, that is correct. That syntax is basically a "short if/then statement".

Basically, you could write it the same way by saying...

if ( isset( $error ) )
{
echo $error;
}
else
{
echo '';
}

It does the same thing in the end; it's just a matter of how the statement is formatted.

The syntax of the shortened if statement goes as follows:

($variable)? "Variable is true!" : "Variable is false!";

where $variable can be anything from a simple variable to a complex expression in the same style as something in an if statement. The statement will return "Variable is true!" if $variable evaluates to true, and "Variable is false!" if $variable evaluates to false.

Generally this syntax is used in places such as this where you don't want to write a multi-line if/else block, and simply want either one phrase or the other returned.

Edit: on looking at it further, there was a syntax error in there.

Old: <?php echo (isset($error)? $error : ");?>
New: <?php echo (isset($error))? $error : "";?>

[edited by: WesleyC at 12:31 am (utc) on July 28, 2007]

12:46 am on July 28, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 4, 2006
posts:85
votes: 0


Thanks, that clears things up. I'm not getting errors anymore but when I leave the name filed blank, the the error from function validate_form() isn't getting over to $error.

The first if statement:

$valid = validate_form();
if (isset($_POST['submit']))
{
if ($valid === true)
{
include ('listing-review.php');
}
else
{
include ('breeder-form.php');
$error = $valid;
}
}

either brings the input data to a display page or re-displays the form with errors, only it's not displaying the error.

I still can't find the trouble.

6:11 am on July 28, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


WesleyC, there wasn`t an error in the code I posted, but there is in the new you posted. That would throw a parse error.

Its called a Ternary Operator [uk2.php.net].

dc

3:40 pm on July 30, 2007 (gmt 0)

Full Member

5+ Year Member

joined:July 13, 2007
posts:235
votes: 0


Pardon me, you're correct. I'm still thinking in Javascript syntax. :P

There's a few downsides to having to alternate between languages several times a day...

Edit: Though I think your snippet accidentally used a single double quote instead of two single quotes...

[edited by: WesleyC at 3:42 pm (utc) on July 30, 2007]

6:23 pm on July 30, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


No problem. It was two single quotes, but maybe looks different due to the board formatting.

dc

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members