SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Form

  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form

    Hi all,
    I have this form page as sidees.php file that sends to itself but it shows the error "You have introducted an invalid code. Please click here and try again"
    as the code is not present. How do I make this conditional? Thanks!
    HTML Code:
    <?php
    $lastpage = $_SERVER['HTTP_REFERER']; 
    
    
    if (($_SESSION['6_letters_code'] == $_POST['6_letters_code']) && !empty($_SESSION['6_letters_code'])) {
        $error = FALSE;
        
        if (!preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/', $email)) {
            echo '<h4>Invalid email address</h4>';
            $error = TRUE;
        }
    
        if ($name == '') {
            echo '<h4>No name</h4>';
            $error = TRUE;
        }
    
        if ($email_subject == '') {
            echo '<h4>No subject</h4>';
            $error = TRUE;
        }
    
        if ($message == '') {
            echo '<h4>No message</h4>';
            $error = TRUE;
        }
        
        if ($error === FALSE) {
            $to = '';
            $name = $_POST['name'];
            $email_subject = $_POST['email_subject'];
            $message = $_POST['message'];
    	$from = '';
            $email_body = <<<MSG
    You have received a new message from $name
    
    Here is the message:
    Name: $name
    Email:$email
    Subject: $email_subject
    Message: $message
    Body: $email_body
    
    MSG;
    
            mail($to, $email_subject, $email_body);
    
            echo '<p>Your message has been sent correctly.</p>';
        }
    } else {
     echo "<p>You have introducted an invalid code. Please <a href=\"$lastpage\">click here and try again</a></p>";   
    } 
    ?>
    	<div id="sidebar">
    		<br /><br /><br /><br /><br />
    <div class="block" id="fyi">
    		<h3 class="head">Header</h3>
    	  <div class="block-content">
    		 <form method="POST" name="contact_form" action="sidees.php"> 
    Name: <input type="text" name="name"><br> 
    Subject: <input name="email_subject" type="text"><br> 
    Email: <input type="text" name="email"><br> 
    Message: <textarea name="message" style="width: 144px">
    </textarea><br> 
    
    <img src="captcha_code_file.php" /><br>
    Enter your code here<input id="6_letters_code" name="6_letters_code" type="text" ><br>
    <input type="submit" value="Enviar"><br>
    </form>
            </div>
    	</div>

  2. #2
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well obviously this line is failing:

    if (($_SESSION['6_letters_code'] == $_POST['6_letters_code']) && !empty($_SESSION['6_letters_code'])) {

    You need to make it pass.
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Alien so how do I do that? Cheers

  4. #4
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Bayliss Trevor View Post
    Thanks Alien so how do I do that? Cheers
    You wrote the line, not me. I don't know what your code is supposed to do.
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  5. #5
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I want it to do is not show the message. So I just need to put a condition on that message so it only shows if the form has been sent. How do I do that Alien or anyone? Thanks

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    I see that you're using $_SESSION but doesn't appear to have resumed the session beforehand.

    http://php.net/manual/en/function.session-start.php
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the answer, so is this the idea?

    PHP Code:
    <?php
    $lastpage 
    $_SERVER['HTTP_REFERER']; 

    session_start($_SESSION);
    if ((
    $_SESSION['6_letters_code'] == $_POST['6_letters_code']) && !empty($_SESSION['6_letters_code'])) {
        
    $error FALSE;

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Yes, that's the idea.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Paul I'll give it a try

  10. #10
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    session_start($_SESSION); <-- remove $_SESSION from here.
    reading documentation entry for the function before it use is great help


Bookmarks

Posting Permissions

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