Forum Moderators: coopster
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>
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
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]
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.
Its called a Ternary Operator [uk2.php.net].
dc
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]