Jump to content

Help understanding why I'm having a variable error.


horseatingweeds

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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....

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.