Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Form Permanently Reporting "Error"

    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:

    PHP Code:

    <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
    Last edited by --zero; 12-07-2008 at 04:12 AM.

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Quote Originally Posted by --zero View Post
    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 [php]...[/php] or [code]...[/code] tags - it makes it much easier to read.

  • #3
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah I did notice about the code tag after posting so apologies for that!

    I actually found the solution on another recent thread - Help needed with Mailing List PHP script.

    Whee yay it works now

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by --zero View Post
    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

  • #5
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And here is a copy of my code that now works:

    PHP Code:
    <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 {
       }

    ?>
    Last edited by --zero; 12-07-2008 at 04:13 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •