homepage Welcome to WebmasterWorld Guest from 54.197.215.146
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Trouble understanding a variable error
horseatingweeds




msg:3406785
 10:25 pm on Jul 27, 2007 (gmt 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>

 

dreamcatcher




msg:3406816
 11:05 pm on Jul 27, 2007 (gmt 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

horseatingweeds




msg:3406826
 11:21 pm on Jul 27, 2007 (gmt 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.

WesleyC




msg:3406853
 12:29 am on Jul 28, 2007 (gmt 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]

horseatingweeds




msg:3406859
 12:46 am on Jul 28, 2007 (gmt 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.

dreamcatcher




msg:3406946
 6:11 am on Jul 28, 2007 (gmt 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

WesleyC




msg:3408575
 3:40 pm on Jul 30, 2007 (gmt 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]

dreamcatcher




msg:3408789
 6:23 pm on Jul 30, 2007 (gmt 0)

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

dc

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved