Adding a redirect after submission, not sending if user does not agree

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BAFTUB
    New Member
    • Jul 2010
    • 2

    Adding a redirect after submission, not sending if user does not agree

    Hello, I am pretty new to PHP, and I had gotten a friend to help me write a php form.

    I have everything set how I need it to be, but I am missing a few important things.

    Users can just hit submit, and a blank message will be sent. I also have an agreement checkbox, which users can ignore, as the message will send even if it is not checked. Lastly, I need the page to display a "success message" when they hit submit.

    Here is the code I have so far:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <?php
    if($_POST['emailSub']) {
    $to = "baftub@gmail.c om";
    $subject = "Blog Application";
    $message = "Firstname: \t" . $_POST['firstname'] .
    "\nLastname :\t" . $_POST['lastname'] .
    "\nEmail:\t " . $_POST['email'] .
    "\nitem:\t" . $_POST['item'] .
    "\ncomment: \t" . $_POST['comment'];
    mail($to, $subject, $message);
    }
    ?>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Conten t Submission</title>
    <style type="text/css">
    <!--
    body {
    background-color: #121212;
    }
    body,td,th {
    color: #FFF;
    }
    -->
    </style></head>

    <body>
    <center>Pleas e fill out this form for any content you wish to contribute
    <br />
    <br />
    <br />
    <form name="input" action="" method="post">
    First name:
    <input type="text" name="firstname " />
    <br />
    <br />
    Last name:
    <input type="text" name="lastname" />
    <br />
    <br />
    Email:
    <input type="text" name="email" />
    <br />
    <br />
    Which item are you going to contribute to?
    <input type="text" name="item" />
    <br />
    <br />
    Please provide further details for your submission, any pictures or files.
    <br />
    <textarea cols="50" rows="4" name="comment"> </textarea>
    <br />
    <br />
    I agree that content submitted by myself is created by me, and does not breach any copyrights
    <br />
    <input type="checkbox" name="yes" value="yes" />
    <br />
    <br />
    <input type="submit" name="emailSub" >
    </form>
    </center>
    </body>
    </html>

    Thank you for the help :)
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Welcome to Bytes. I have read through your message and I don't see your question? What is not working?

    Comment

    • BAFTUB
      New Member
      • Jul 2010
      • 2

      #3
      Originally posted by TheServant
      Welcome to Bytes. I have read through your message and I don't see your question? What is not working?
      I do not know how to add a redirect after the user hits submit. I do not know how to add a success message after it is sent, and I do not know how to force them to agree to the terms, or else they cant submit it.

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Sure. Well you don't need to "redirect after a user submits". In your form you have action="". You if you put the current page's URL in that, then it will submit the form data to that page and do what I think you're after. You can also put another page URL which will look at the data and display the response.

        The mail() function returns a success or fail response, so you can do something like:
        Code:
        if(mail($to, $subject, $message)) {
        echo "Success";
        } else {
        echo "Fail";
        }
        Instead of echo, you can set a variable to the message and call that further down in a more appropriate HTML spot.

        With regards to forcing them to check a box, you can put in another message system:
        Code:
        if($_POST['yes']=="yes") {
        //send email
        } else {
        echo "You must accept the terms and conditions to continue";
        }
        As I say, instead of echo, set a variabel to the message and then call that variable in a pack you want your messages displayed in the HTML.

        Oh, and that is all a PHP solution, which is probably best, but you can use javascript to give the user feedback on the form as well, and in the future you should look at that.

        Comment

        Working...