...

View Full Version : PHP Form Permanently Reporting "Error"



--zero
12-07-2008, 01:17 AM
I am using PHP to code my own mailing list form - something new for me to do.

So I am on a very steep learning curve and have come across some problems:

Here is my code to begin with:




<form name="mailing" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

// THE ACTUAL FORM HERE - I WON'T BOTHER POSTING THE CODE FOR THIS //

<input type="submit" value="Sign Up">
</div>
</form>
</div>
<?
$name=$_POST['name'];
$email=$_POST['email'];
$weighday=$_POST['weighday'];
$dailymot=$_POST['dailymot'];
$weeklymot=$_POST['weeklymot'];
$news=$_POST['news'];
$offers=$_POST['offers'];
$recipes=$_POST['recipes'];


if ($name == "") {print 'You forgot to give us your name!';
}

else if ($email == "") {print 'You forgot to give us your e-mail address!';
}

else

{

// mysql_connect ..... etc //

Now the actual coding works great - it displays the error messages and posts the data to my database no problems.

However, the problem is this - whether somebody submits the form or not the error message "you forgot to give us your name" is appearing. I can tell why - it's because $name =="" the minute the page loads so the script thinks there's an error. Question is - how do I stop this from happening and only appear when appropriate and once the submission is made?

My other question is how do I get the error messages / thank you message to appear where I want it to on the page rather than the first space available on the page?

Thanks in advance!

Chris

PappaJohn
12-07-2008, 01:59 AM
I can tell why - it's because $name =="" the minute the page loads so the script thinks there's an error. Question is - how do I stop this from happening and only appear when appropriate and once the submission is made?
Correct, that's why it's happening.

So, the idea is to test to see if the form has been submitted and only proceed with the processing if it has.

BTW: in the future, please wrap your code in
... or
... tags - it makes it much easier to read.

--zero
12-07-2008, 02:14 AM
Yeah I did notice about the code tag after posting so apologies for that!

I actually found the solution on another recent thread - http://codingforums.com/showthread.php?t=153386 :)

Whee yay it works now

oesxyl
12-07-2008, 03:01 AM
Yeah I did notice about the code tag after posting so apologies for that!
you can edit your post. Once you solved will be usefull when someone else will search the forum, :)

thank you and best regards

--zero
12-07-2008, 03:09 AM
And here is a copy of my code that now works:


<form name="mailing" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

// ACTUAL FORM HERE //

<input name="submitBtn" type="submit" id="submitBtn" value="Sign Up">
</div>
</form>
</div>


<?php
if (isset($_POST['submitBtn']) && $_POST['submitBtn'] == "Sign Up") {

$name=$_POST['name'];
$email=$_POST['email'];

// REST OF FORM ACTIONS HERE //


else {
}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum