horseatingweeds Posted July 27, 2007 Share Posted July 27, 2007 My code is below. I'm having trouble getting the form validation error to display. I'm new at php so I'm a bit confused. I'm getting a 'notice' : Undefined index: submit breeder-list-1.php <?php $response; $valid = validate_form(); if ($_POST['submit']) { if ($valid === true) { include ('listing-review.php'); } else { include ('breeder-form.php'); validate_form(); } } 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> <br /> <?php echo '$response'; ?> <form name='form1' id='form1' enctype='multipart/form-data' action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post" > <fieldset> <legend>Contact Information</legend> Listing Title:<br /> <input type='text' name='title' size='30' maxlength='70' value='<?php echo $_POST['title']; ?>' /><br /> </fieldset> <input type='submit' name='submit' value='Next ->' /> </form> listing-review.php <div id='listing-review'> <?php echo '<strong>' . $_POST['title'] . '</strong> ' . $_POST['radAvailability']; ?> <table> <tr valign='top'> <td> <?php echo $_POST['country'] . ", " . $_POST['state']; ?><br /> <?php echo $_POST['email']; ?><br /> <?php echo $_POST['phone']; ?> </td> <td width='150px'> <?php foreach($_POST['chkActivities'] AS $activity) echo $activity . '<br />'; ?> </td> <td> <?php echo $_POST['description']; ?> </td> </tr> </table> <form> <input type='submit' name='submit' value='Edit'/> </form> </div> Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 27, 2007 Share Posted July 27, 2007 try to use isset if (isset($_POST['submit'])) Quote Link to comment Share on other sites More sharing options...
horseatingweeds Posted July 27, 2007 Author Share Posted July 27, 2007 I think the problem is with the $response variable. It's turning up undefined. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 27, 2007 Share Posted July 27, 2007 Teng84's suggestion is right for changing this line: if ($_POST['submit']) to this: if (isset($_POST['submit'])) That will get rid of the notice message. it is not to do with the response variable. When your script runs and you haven't submitted the form yet, PHP will check to see if the $_POST['submit'] variables holds a non-false value, eg TRUE, a string, a number etc. It wont check to make sure that the variable exists yet. When it goes to check that variables value it wont be able to as it doesn't exist and this PHP reports back saying xyz is Underfined. Using Teng84's suggestion now makes PHP check whether the variable exists using isset. isset checks for variable existence. Quote Link to comment Share on other sites More sharing options...
horseatingweeds Posted July 27, 2007 Author Share Posted July 27, 2007 I should have said, I took the advice and made the change from if ($_POST['submit']) to if (isset($_POST['submit'])), yet the error message does not display and I get the notice: Notice: Undefined variable: response in "root"\breeder-form.php on line 3 However, I changed in breeder-form.php <?php echo $response; ?> to <?php echo validate_form(); ?> and now it seems to work. Is this a good practice? Is there a better way? Also, another question. I have an edit button which is another submit button to bring the user back to the form. I'd like the user's inputs to be put back into the fields. I had the values in the fields set to <?php echo $_POST['name'] ?> but this would somehow put <br /> in the fields. How should I redisplay the form without re-submitting. I'm assuming the re-submit is removing the posted inputs.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.