Contact PHP Script Form Question

Discussion in 'PHP' started by novashun, Aug 2, 2011.

  1. #1
    It just hangs and doesnt execute. I dont understand. Here is my contact.php - The actual html page just blows out all white and it doesnt redirect either.. Any advice?

    <?php
    
    /* Set e-mail recipient */
    
    $myemail  = "myemail@yahoo.com";
    
    
    
    /* Check all form inputs using check_input function */
    
    $yourname = check_input($_POST['yourname'], "Enter your name");
    
    $email  = check_input($_POST['email'], "Write a subject");
    
    $type    = check_input($_POST['type']),"Write a type");
    
    $phone  = check_input($_POST['phone']),"Write a phone");
    
    $processing   = check_input($_POST['processing']),"Write a processing");
    
    $sales = check_input($_POST['sales']),"Write a sales");
    
    $country = check_input($_POST['country']),"Write a country");
    
    $comments = check_input($_POST['comments'], "Write your comments");
    
    
    
    /* If e-mail is not valid show error message */
    
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    
    {
    
        show_error("E-mail address not valid");
    
    
    
    
    
    /* Let's prepare the message for the e-mail */
    
    $message = "Hello!
    
    
    
    Your contact form has been submitted by:
    
    
    
    Name: $yourname
    
    E-mail: $email
    
    Phone: $phone
    
    business type: $type
    
    country: $country
    
    processing: $processing
    
    sales volume: $sales 
    
    Comments:
    
    $comments
    
    
    
    End of message
    
    ";
    
    
    
    /* Send the message using mail() function */
    
    mail($yourname, $email, $type, $phone, $processing, $sales, $country, $comments  );
    
    
    
    /* Redirect visitor to the thank you page */
    
    header('Location: thanks.htm');
    
    exit();
    
    
    
    /* Functions we used */
    
    function check_input($data, $problem='')
    
    {
    
        $data = trim($data);
    
        $data = stripslashes($data);
    
        $data = htmlspecialchars($data);
    
        if ($problem && strlen($data) == 0)
    
        {
    
            show_error($problem);
    
        }
    
        return $data;
    
    }
    
    
    
    function show_error($myError)
    
    {
    
    ?>
    
        <html>
    
        <body>
    
    
    
        <b>Please correct the following error:</b><br />
    
        <?php echo $myError; ?>
    
    
    
        </body>
    
        </html>
    
    <?php
    
    exit();
    }
    ?>
    PHP:
    Appreciate any advice :cool:
     
    novashun, Aug 2, 2011 IP
  2. arpit13

    arpit13 Well-Known Member

    Messages:
    294
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #2
    i guess u didn't had '}' for email check.
    
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    
    {
    
        show_error("E-mail address not valid");
    
    }
    
    PHP:
     
    arpit13, Aug 3, 2011 IP
  3. freelanceinphp

    freelanceinphp Member

    Messages:
    134
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #3
    Have you tried Arpit solutions? still you have any error in above code?
     
    freelanceinphp, Aug 3, 2011 IP
  4. novashun

    novashun Well-Known Member

    Messages:
    821
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    138
    #4

    Nice catch - I closed it with the } -- but it still doesnt redirect or send an email.. any other suggestions. Freelance get out of my thread - I dont need any paid support.
     
    novashun, Aug 3, 2011 IP
  5. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #5
    There are several errors like the ones with the check_input() functions where you put more brackets than needed and the mail() function where I still don't know what you wanted to do. I tried to change them, I hope that catched them all.

    <?php
    
    /* Set e-mail recipient */
    $myemail  = 'myemail@yahoo.com';
    
    /* Check all form inputs using check_input function */
    $yourname = check_input($_POST['yourname'], 'Enter your name');
    $email    = check_input($_POST['email'], 'Write a subject');
    $type     = check_input($_POST['type'],'Write a type');
    $phone    = check_input($_POST['phone'],'Write a phone');
    $processing = check_input($_POST['processing'],'Write a processing');
    $sales    = check_input($_POST['sales'],'Write a sales');
    $country  = check_input($_POST['country'],'Write a country');
    $comments = check_input($_POST['comments'], 'Write your comments');
    
    /* If e-mail is not valid show error message */
    if ( !preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) ) {
    
        show_error('E-mail address not valid');
    }
    
    /* Let's prepare the message for the e-mail */
    $message = "Hello!
    
    Your contact form has been submitted by:
    
    Name: $yourname
    E-mail: $email
    Phone: $phone
    business type: $type
    country: $country
    processing: $processing
    sales volume: $sales
    Comments:
    
    $comments
    
    End of message";
    
    /* Send the message using mail() function */
    mail( $myemail, 'Subject of the message', $message, 'From: noreply@example.com');
    
    /* Redirect visitor to the thank you page */
    header('Location: thanks.htm');
    
    exit();
    
    /* Functions we used */
    function check_input( $data, $problem = '' ) {
    
    	$data = trim($data);
    	$data = stripslashes($data);
    	$data = htmlspecialchars($data);
        
    	if ( $problem && strlen($data) == 0 ) {
        	
    		show_error($problem);
    	}
    	return $data;
    }
    
    function show_error( $myError ) { ?>
    
        <html>
        <body>
        <b>Please correct the following error:</b><br />
        <?php echo $myError; ?>
        </body>
        </html>
    <?php
    
    exit();
    }
    ?>
    PHP:
     
    bogi, Aug 3, 2011 IP
  6. arpit13

    arpit13 Well-Known Member

    Messages:
    294
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #6
    to be true Bogi's code is quiet understandable, i didn't even got wat u actually want to do :p .

    try that code it shud work
     
    arpit13, Aug 4, 2011 IP
  7. Andre91

    Andre91 Peon

    Messages:
    197
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Did you try Bogi's code? And freelance wasn't offering you paid support. He simply asked if the dude's ("arpit") code above his post worked :?
     
    Andre91, Aug 6, 2011 IP
  8. ModulesGarden

    ModulesGarden Active Member

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Why don't you just enable errors for the tests? Or put some echo'es to check where the script stops (if its not a code error) ?
     
    ModulesGarden, Aug 7, 2011 IP
  9. FilesIN

    FilesIN Member Affiliate Manager

    Messages:
    94
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    33
    #9
    Some times preg_match function not working properly try to validate without it
     
    FilesIN, Aug 7, 2011 IP
  10. sinha.sidhi

    sinha.sidhi Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    check there is lot of syntax error...
     
    sinha.sidhi, Aug 29, 2011 IP