duanca3415 2013-11-28 10:56
浏览 75

网站上有两个php联系表单,一个重定向另一个没有。 两者都提交罚款

This place has been a real lifesaver at times so thank you all for your efforts in making this place so great. However, I have an issue that I can't find an answer to on here.

I have a website that has a contact form on the home page, and a contact form on some inner pages. I need each one to redirect to a different thank you page (for PPC conversion tracking purposes)

The one on the inner page works fine, submits info and redirects, but the one on the home page only submits info but no redirect. Each form uses a separate php file for the form action and a separate thank you page. Here is the code, can you see a problem? I'm thinking it may be something to do with the headers?

Home Page Code:

<form action="landing-page-form.php" method="post">
            <h2>Get A Quote Today!</h2>
            <p><label for="name">Name</label><input name="name" id="name"></p>
            <p><label for="tel">Tel</label><input name="tel" id="tel"></p>
            <p><label for="email">Email</label><input name="email" id="email"></p>
            <p><label for="service">Service</label><select name="service" id="service"><option value="Kitchen Design">Kitchen Design</option><option value="Bathroom Design">Bathroom Design</option><option value="Central HEating">Central Heating</option><option value="Appliances">Kitchen Appliances</option><option value="Brochure Request">Free Brochure Request</option><option value="Home Visit Request">Free Home Visit Request</option></select></p>
            <p><input type="checkbox" name="chk" id="chk" value="1"> <label class="chk" for="chk">check the checkbox</label></p>
            <p><button name="submit" type="submit">Send My Enquiry!</button></p>
        </form>

Here is the relating php

<?php if ($_POST) {
header('Content-type: application/json');
$fields = array();
$msg = array();
foreach($_POST as $k=>$v) $_POST[$k] = trim($v);
$pattern = "/^[a-zA-Z0-9][a-zA-Z0-9\.-_]+\@([a-zA-Z0-9_-]+\.)+[a-zA-Z]+$/";
if(preg_match($pattern, $_POST['email'])) $email_ok = true;
else $email_ok = false; 
if(!$_POST['email'] || !$email_ok)  $fields[] = "email";
if(!$_POST['name'])  $fields[] = "name";
if(!$_POST['tel'])  $fields[] = "tel";

if(count($fields)) {
    $status = "error";
    if(!$_POST['name'] || !$_POST['email'] || !$_POST['email']) $msg[] = "Please specify your name, telephone and email address.";
    if(!$email_ok) $msg[] = "Provided e-mail address is invalid.";
    $message = implode("<br>", $msg);

    $response['msgStatus'] = $status;
    $response['message'] = $message;
    $response['errorFields'] = $fields;
} else {
    $mailContent = "Name: {$_POST['name']}
tel: {$_POST['tel']}
E-mail: {$_POST['email']}
Service: {$_POST['service']}
Checkbox: {$_POST['chk']}";
    $ok = mail("email@email.com", "Message from Home Page Landing Page Form", $mailContent, "From:<".$_POST['email'].">");
    if($ok) {
        $response['msgStatus'] = "ok";
        header( 'Location: /thanks.php' ) ;
    } else {
        $response['msgStatus'] = "error";
        $response['message'] = "Could not send your request due to an error.";
    }
}
echo json_encode($response);

} ?>

Here is the inner page HTML:

<form action="quick-contact-form.php" method="post">
        <h6>Quick Contact</h6>
        <p><label for="name">Name</label><input name="name" id="name"></p>
        <p><label for="tel">Tel</label><input name="tel" id="tel"></p>
        <p><label for="email">Email</label><input name="email" id="email"></p>
        <p><label for="service">Service</label><select name="service" id="service"><option value="Kitchen Design">Kitchen Design</option><option value="Bathroom Design">Bathroom Design</option><option value="Heating">Central Heating</option><option value="Appliances">Kitchen Appliances</option><option value="Brochure Request">Free Brochure Request</option><option value="Home Visit Request">Free Home Visit Request</option></select></p>
        <p><input type="checkbox" name="chk" id="chk" value="1"> <label class="chk" for="chk">check the checkbox</label></p>
        <p><button name="submit" type="submit">Send!</button></p>
    </form>

And the relating PHP

<?php if ($_POST) {
header('Content-type: application/json');
$fields = array();
$msg = array();
foreach($_POST as $k=>$v) $_POST[$k] = trim($v);
$pattern = "/^[a-zA-Z0-9][a-zA-Z0-9\.-_]+\@([a-zA-Z0-9_-]+\.)+[a-zA-Z]+$/";
if(preg_match($pattern, $_POST['email'])) $email_ok = true;
else $email_ok = false; 
if(!$_POST['email'] || !$email_ok)  $fields[] = "email";
if(!$_POST['name'])  $fields[] = "name";
if(!$_POST['tel'])  $fields[] = "tel";

if(count($fields)) {
    $status = "error";
    if(!$_POST['name'] || !$_POST['email'] || !$_POST['email']) $msg[] = "Please specify your name, telephone and email address.";
    if(!$email_ok) $msg[] = "Provided e-mail address is invalid.";
    $message = implode("<br>", $msg);

    $response['msgStatus'] = $status;
    $response['message'] = $message;
    $response['errorFields'] = $fields;
} else {
    $mailContent = "Name: {$_POST['name']}
tel: {$_POST['tel']}
E-mail: {$_POST['email']}
Service: {$_POST['service']}
Checkbox: {$_POST['chk']}";
    $ok = mail("email@email.com", "Message from Website Landing Page Form", $mailContent, "From:<".$_POST['email'].">");
    if($ok) {
        $response['msgStatus'] = "ok";
        header( 'Location: /thankyou.php' ) ;
    } else {
        $response['msgStatus'] = "error";
        $response['message'] = "Could not send your request due to an error.";
    }
}
echo json_encode($response);

} ?>

Any and all help, suggestions, feedback appreciated.

  • 写回答

1条回答 默认 最新

  • doulao2916 2013-11-28 12:18
    关注

    First of, check the path you are using and try to make it absolute, not relative. Second, check your .htaccess file - maybe there's some nasty rewrite rules.

    By the way, is it OK that you've got different redirect pages? header( 'Location: /thanks.php' ) ; and header( 'Location: /thankyou.php' ) ;?

    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止