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 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答