Web Analytics Made Easy -
StatCounter how can i put this in to one file - CodingForum

Announcement

Collapse
No announcement yet.

how can i put this in to one file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • how can i put this in to one file

    i have coded up a simple email sending script works great dose all the erro checking and everythink and i found a bit of code to make me a capta so i can check they are people and not bots

    but i got one problem with my code at the moment i am posting it off to a diffent php page to handel the email and the error checking it all work fine but once my script has found some that is wrong the users need to go back and it ends up not having the infromation that they put in the html fields not good a user is not lucky want to start the message all over again so am i right in thinking if i do it all in one file it would not delete the users data in the fields if they where to be an error i am thinking of isset could that been used here is my code i am using thank you for your help
    html form
    <center><strong>Contact us!</strong>
    <p>ALL fields are required.</p>&nbsp;&nbsp; Name:
    <form method=\"post\" action=\"contact.php\">&nbsp;
    <input name=\"name\" size=\"23\"><br>&nbsp;&nbsp;&nbsp; E-mail:<br>&nbsp;
    <input name=\"email\" size=\"23\"><br>&nbsp;
    Subject:<br><input name=\"subject\" size=\"23\"><br>&nbsp; Message:<br>&nbsp;
    <textarea name=\"message\" cols=\"40\" rows=\"5\">Enter your comments here...
    </textarea><br>
    <br>
    Please Enter the letter and Number you see in the box below
    <img src=\"captcha_image.php\" width=\"100\" height=\"20\">:<input name=\"captcha\" size=\"14\"><br>
    <input value=\"Submit\" type=\"submit\"></form></center>
    contact.php

    ession_start();

    if ($_POST["captcha"] != $_SESSION["pass"])
    {
    die('CAPTCHA Failed, press the Back button on your browser and try again.');
    }
    $to = "[email protected]"; //edit this line to YOUR email
    // do not edit below this line!
    $from = $_POST["email"];
    $subject = $_POST["subject"];
    $name = $_POST["name"];
    $message = $_POST["message"];
    $messagesent = "Name: $name \r\nMessage: $message \r\nSender's IP: $HTTP_SERVER_VARS[REMOTE_ADDR] \r\nemail: $from";
    $headers = "From: $from";
    if (!preg_match("/^([a-zA-Z -])+$/",$name))
    {
    die('Name must consist of letters only.');
    }
    if (!preg_match('/^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i',$from))
    {
    die('Invalid e-mail address.');
    }
    mail($to,$subject,$messagesent,$headers);

    echo "Form has been submitted successfully we will get back to you with in the next 24 hours";
    ?>
    thank you

  • #2
    PHP Code:
    session_start();

    if(isset(
    $_POST['submit']{

    if (
    $_POST["captcha"] != $_SESSION["pass"])
    {
    die(
    'CAPTCHA Failed, press the Back button on your browser and try again.');
    }
    $to "[email protected]"//edit this line to YOUR email
    // do not edit below this line!
    $from $_POST["email"];
    $subject $_POST["subject"];
    $name $_POST["name"];
    $message $_POST["message"];
    $messagesent "Name: $name \r\nMessage: $message \r\nSender's IP: $HTTP_SERVER_VARS[REMOTE_ADDR] \r\nemail: $from";
    $headers "From: $from";
    if (!
    preg_match("/^([a-zA-Z -])+$/",$name))
    {
    die(
    'Name must consist of letters only.');
    }
    if (!
    preg_match('/^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i',$from))
    {
    die(
    'Invalid e-mail address.');
    }
    mail($to,$subject,$messagesent,$headers);

    echo 
    "Form has been submitted successfully we will get back to you with in the next 24 hours";
    }
    ?>

    <center><strong>Contact us!</strong>
    <p>ALL fields are required.</p>&nbsp;&nbsp; Name:
    <form method=\"post\">&nbsp;
    <input name=\"name\" size=\"23\"><br>&nbsp;&nbsp;&nbsp; E-mail:<br>&nbsp;
    <input name=\"email\" size=\"23\"><br>&nbsp;
    Subject:<br><input name=\"subject\" size=\"23\"><br>&nbsp; Message:<br>&nbsp;
    <textarea name=\"message\" cols=\"40\" rows=\"5\">Enter your comments here...
    </textarea><br>
    <br>
    Please Enter the letter and Number you see in the box below
    <img src=\"captcha_image.php\" width=\"100\" height=\"20\">:<input name=\"captcha\" size=\"14\"><br>
    <input value="Submit" type="submit" name="submit"></form></center> 
    Should work.

    Comment


    • #3
      Don't forget your <?php

      PHP Code:
      <?php
      session_start
      ();

      if(isset(
      $_POST['submit']{

      if (
      $_POST["captcha"] != $_SESSION["pass"])
      {
      die(
      'CAPTCHA Failed, press the Back button on your browser and try again.');
      }
      $to "[email protected]"//edit this line to YOUR email
      // do not edit below this line!
      $from $_POST["email"];
      $subject $_POST["subject"];
      $name $_POST["name"];
      $message $_POST["message"];
      $messagesent "Name: $name \r\nMessage: $message \r\nSender's IP: $HTTP_SERVER_VARS[REMOTE_ADDR] \r\nemail: $from";
      $headers "From: $from";
      if (!
      preg_match("/^([a-zA-Z -])+$/",$name))
      {
      die(
      'Name must consist of letters only.');
      }
      if (!
      preg_match('/^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i',$from))
      {
      die(
      'Invalid e-mail address.');
      }
      mail($to,$subject,$messagesent,$headers);

      echo 
      "Form has been submitted successfully we will get back to you with in the next 24 hours";
      }
      ?>

      <center><strong>Contact us!</strong>
      <p>ALL fields are required.</p>&nbsp;&nbsp; Name:
      <form method=\"post\">&nbsp;
      <input name=\"name\" size=\"23\"><br>&nbsp;&nbsp;&nbsp; E-mail:<br>&nbsp;
      <input name=\"email\" size=\"23\"><br>&nbsp;
      Subject:<br><input name=\"subject\" size=\"23\"><br>&nbsp; Message:<br>&nbsp;
      <textarea name=\"message\" cols=\"40\" rows=\"5\">Enter your comments here...
      </textarea><br>
      <br>
      Please Enter the letter and Number you see in the box below
      <img src=\"captcha_image.php\" width=\"100\" height=\"20\">:<input name=\"captcha\" size=\"14\"><br>
      <input value="Submit" type="submit" name="submit"></form></center>
      BTW the center tag won't validate....
      <div style="text-align:center;">will....
      Last edited by sea4me; Apr 14, 2009, 12:14 AM.

      Comment


      • #4
        Originally posted by sea4me View Post
        BTW the center tag won't validate....
        <div style="text-align=center;">will....
        Think you meant <div style="text-align:center;">
        You can not say you know how to do something, until you can teach it to someone else.

        Comment


        • #5
          Originally posted by timgolding View Post
          Think you meant <div style="text-align:center;">
          What are you talking about???

          That was what I had!!

          lol JK

          Comment


          • #6
            line 6 and 29 bring up errors if i take out the } on 20 that erro gose but it not liking the exta if on like 6 if ($_POST["captcha"] != $_SESSION["pass"])

            thank you all for your help

            Comment


            • #7
              The errors on line 4. It's

              PHP Code:
              if(isset($_POST['submit']{ 
              Should be

              PHP Code:
              if(isset($_POST['submit'])){ 
              What error message are you getting on line 29?

              What are you talking about???

              That was what I had!!

              lol JK
              hehe lol
              You can not say you know how to do something, until you can teach it to someone else.

              Comment


              • #8
                line 29 is gone now but now for some reason my captcha is not showing is that because the session is not running ?

                Comment


                • #9
                  Yes you need to set your session up somewhere on the form part
                  You can not say you know how to do something, until you can teach it to someone else.

                  Comment


                  • #10
                    PHP Code:
                    <?php
                    if(isset($_POST['submit'])){  

                    if (
                    $_POST["captcha"] != $_SESSION["pass"])
                    {
                    die(
                    'CAPTCHA Failed, press the Back button on your browser and try again.');
                    }
                    $to "[email protected]"//edit this line to YOUR email
                    // do not edit below this line!
                    $from $_POST["email"];
                    $subject $_POST["subject"];
                    $name $_POST["name"];
                    $message $_POST["message"];
                    $messagesent "Name: $name \r\nMessage: $message \r\nSender's IP: $HTTP_SERVER_VARS[REMOTE_ADDR] \r\nemail: $from";
                    $headers "From: $from";
                    if (!
                    preg_match("/^([a-zA-Z -])+$/",$name))
                    {
                    die(
                    'Name must consist of letters only.');
                    }
                    if (!
                    preg_match('/^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i',$from))
                    {
                    die(
                    'Invalid e-mail address.');
                    }
                    mail($to,$subject,$messagesent,$headers);

                    echo 
                    "Form has been submitted successfully we will get back to you with in the next 24 hours";
                    }
                    ?>
                    <?php
                    echo'<center><strong>Contact us!</strong>
                    <p>ALL fields are required.</p>&nbsp;&nbsp; Name:
                    <form method=\"post\">&nbsp;
                    <input name=\"name\" size=\"23\"><br>&nbsp;&nbsp;&nbsp; E-mail:<br>&nbsp;
                    <input name=\"email\" size=\"23\"><br>&nbsp;
                    Subject:<br><input name=\"subject\" size=\"23\"><br>&nbsp; Message:<br>&nbsp;
                    <textarea name=\"message\" cols=\"40\" rows=\"5\">Enter your comments here...
                    </textarea><br>
                    <br>
                    Please Enter the letter and Number you see in the box below
                    <img src=\"captcha_image.php\" width=\"100\" height=\"20\">:<input name=\"captcha\" size=\"14\"><br>
                    <input value="Submit" type="submit" name="submit"></form></center>'
                    ;
                    even if i do this the captcha still dont show so i am not even sure if it sends the email yet ? any ideas on what i am doing wrong

                    Comment


                    • #11
                      Where is the captcha image stored relative to your document root. Have you even checked that it works on its own by putting something like this into browser.

                      Code:
                      http://yourdomain.com/captcha_image.php
                      See if that shows an image. Does the form show up and can you click submit and when you do so does the next page show up with a die message. your form tag has no action in it it needs to read something like this

                      Code:
                      <form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">&nbsp;
                      or

                      Code:
                      <form method=\"post\" action=\"\">&nbsp;
                      I have no idea where that will submit to if you leave it out.
                      Also I don't see where your setting your session data. For one this line

                      PHP Code:
                      if ($_POST["captcha"] != $_SESSION["pass"]) 
                      won't work unless you put

                      PHP Code:
                      <?php
                      session_start
                      ();
                      At the top of your script. For two nowhere in your script are you setting the value of $_SESSION["pass"]. Or does captcha_image.php do that for you? You could try

                      PHP Code:
                      echo $_SESSION["pass"]; 
                      And see if anything is getting through? but place that under session_start(). Hope this helps
                      Last edited by timgolding; Apr 15, 2009, 09:04 AM.
                      You can not say you know how to do something, until you can teach it to someone else.

                      Comment

                      Working...
                      X