2016-09-22 27 views
1

我真的在这里挣扎!我有一个联系表单,我将包括使用jquery插件,并使用php作为备份。这种形式并非全部定制,但现在我遇到了几天的麻烦。当我使用MAMP并点击提交验证完美的作品!在PHP和jQuery中。然后将我重定向到谷歌(最终一些其他网站)。 当我上传到我的公司网站下一个TEST文件夹。测试它我得到验证和一切工作完美。当我在jquery中修复错误后提交它会按照它应该推到谷歌。当我拿走jquery来测试php时,验证工作,然后当我点击提交时,它就保持在同一个index.php页面!我一直拉着我的头发尝试这么多不同的接触验证表单,而这个表单似乎工作得最好。我是一名平面设计师,具有足够的网络经验让我陷入麻烦与PHP!:)任何人都可以帮助一些建议?如何将我的php验证表格重定向到另一页

这里是我的index.php

<?php include('header_contact.php') ?> 


<?php include('testval.php');?> 




<div id="section1"> 

CONTACT FORM 

<div id="contact-form"> 




<!--<form id="contactform" name="contactform" action="<!--?php echo $_SERVER['PHP_SELF']; ?>" method="post" >--> 
<form id="contactform" name="contactform" action="index.php" method="post" > 



    <!--?php 
    if($sent === true) { 
     echo "<h2 class='success'>Thanks, your message has been sent successfully</h2>"; 
    } elseif($hasError === true) { 
     echo '<ul class="errorlist">'; 
     foreach($errorArray as $key => $val) { 
      echo "<li>" . ucfirst($key) . " field error - $val</li>"; 
     } 
     echo '</ul>'; 
    } 
    ?> 
    --> 

    <input name="Name" type="text" id="Name" placeholder="NAME" value="<?php echo (isset($Name) ? $Name : ""); ?>"><span class='text_error'><?php echo $nameErr;?></span> 

    <input name="Company" type="text" id="Company" placeholder="COMPANY" value=''> 

    <input name="Phone" type="tel" id="Phone" placeholder="PHONE" value='<?php echo htmlentities($Phone);?>'> 
    <span class='text_error'><?php echo $phoneErr;?></span> 

    <input name="Email" type="Email" placeholder="EMAIL" value="<?php echo (isset($Email) ? $Email : ""); ?>"> 
    <span class='text_error'><?php echo $emailErr;?></span> 

    <input name="webdesign" type="checkbox" value="Yes" /><label>Web Design: </label><br /> 

    <input name="webhosting" type="checkbox" value="Yes" /><label>Web Hosting: </label><br /> 

    <input name="wordpressdesign" type="checkbox" value="Wordpress Design" /><labeel>Wordpress Design: </labeel><br /> 

    <textarea id="Describe" class="mess" name="Describe" type="text" placeholder="DESCRIBE WHAT YOU NEED"><?php echo isset($_POST['Describe'])? $_POST['Describe'] : ''; ?></textarea> 
    <span class='text_error'><?php echo $describeErr;?></span> 

    <!--?php if(in_array('Describe', $validation)): ?><span class="error"><!--?php echo $error_messages['Describe']; ?></span><!--?php endif; ?> 
    <span class='text_error'> 
    --> 

    <input class="input-upload" type="file" name="Upload File"> 

    <input name="Human" placeholder="5 + 3 = ?" value="<?php echo isset($_POST['Human'])? 
    $_POST['Human'] : ''; ?>"> 
    <span class='text_error'><?php echo $humanErr;?></span> 

    <!-- 
    <!--?php if(in_array('Human', $validation)): ?><span class="error"><--?php echo $error_messages['Human']; ?></span><--?php endif; ?> 
    --> 

    <input id="submit" name="submit" type="submit" value="submit"> 




</form> 






</div><!--END CONTACT FORM--> 

</div><!--END SECTION1-->  







<?php include('footer.php') ?> 

这里是我的testval.php

<?php 

session_start(); 
    // define variables and set to empty values 
    $nameErr = $emailErr = $phoneErr = $describeErr = $humanErr = ""; 
    $Name = $Email = $Phone = $Describe = $Human = ""; 

    $hasError = false; 
    $sent = false; 

    if(isset($_POST['submit'])) { 
     $Name   = trim(htmlspecialchars($_POST['Name'], ENT_QUOTES)); 
     $Email   = trim($_POST['Email']); 
     $Describe  = trim(htmlspecialchars($_POST['Describe'], ENT_QUOTES)); 
     $Phone   = trim($_POST['Phone']); 
     $Email   = trim($_POST['Email']); 
     $webdesign  = trim($_POST['webdesign']); 
     $webhosting  = trim($_POST['webhosting']); 
     $wordpressdesign = trim($_POST['wordpressdesign']); 
     $Human   = trim($_POST['Human']); 
     $Number   = 8; 

     $fieldsArray = array(
      'Name' => $Name, 
      'Email' => $Email, 
      'Describe' => $Describe, 
      'Phone' => $Phone, 
      'Human' => $Human 
     ); 

     $errorArray = array(); 

     foreach($fieldsArray as $key => $val) { 
      switch ($key) { 
       case 'Name': 
        if(empty($val)) { 
         $hasError = true; 
         $nameErr = "Please enter your name."; 
        } 
       case 'Name': 
        if (!preg_match("/^[a-zA-Z ]*$/", ($val))) { 
         $hasError = true; 
         $nameErr = "The value must be alphanumeric."; 
        } 
        break; 
       case 'Phone': 
        if (!preg_match("/^[0-9]+$/", ($val))) { 
         $hasError = true; 
         $phoneErr = "Only numbers and white space allowed."; 
        } 
       case 'Phone': 
        if(empty($val)) { 
         $hasError = true; 
         $phoneErr = "Phone is required."; 
        } 
        break; 
       case 'Email': 
        if(!filter_var($Email, FILTER_VALIDATE_EMAIL)) { 
         $hasError = true; 
         $emailErr = "Email is required."; 
        } else { 
         $Email = filter_var($Email, FILTER_SANITIZE_EMAIL); 
        } 
        break; 
       case 'Describe' : 
        if (strlen($val) < 25) { 
         $hasError = true; 
         $describeErr = "Please enter at least 25 characters. This way I know more specifically what you need!"; 
        } 
       case 'Describe' : 
        if(empty($val)) { 
         $hasError = true; 
         $describeErr = "Message is required. This way I know what you need!"; 
        } 
        break; 
       case 'Human': 
        if (!preg_match("/[^\d]?8[^\d]?/", ($val))) { 
         $hasError = true; 
         $humanErr = "Not the right answer"; 
        } 
       case 'Human': 
        if (!preg_match("/^[0-9]+$/", ($val))) { 
         $hasError = true; 
         $humanErr = "Must be a number"; 
        } 
       case 'Human': 
        if(empty($val)) { 
         $hasError = true; 
         $humanErr = "Are you human?"; 
        } 
        break; 
      } 
     } 

     //CHECK BOX WRITE UP 
     if (isset($_POST['webdesign'])) { 
      $checkBoxValue = "yes"; 
      //is checked 
     } else { 
      $checkBoxValue = "no"; 
      //is unchecked 
     } 

     if (isset($_POST['webhosting'])) { 
     $checkBoxValue = "yes"; 
      //is checked 
     } else { 
      $checkBoxValue = "no"; 
      //is unchecked 
     } 

     if (isset($_POST['wordpressdesign'])) { 
     $checkBoxValue = "yes"; 
      //is checked 
     } else { 
      $checkBoxValue = "no"; 
      //is unchecked 
     } 

     //Validation Success! 
     //Do form processing like email, database etc here 
     if($hasError !== true) { 
      $priority = $_POST['priority']; 
      $type = $_POST['type']; 
      $message = $_POST['message']; 
      $formcontent=" From: $Name \n Company: $Company \n Email: $Email \n Phone: $Phone \n Describe: $Describe \n Web Design: $webdesign \n Web Hosting: $webhosting \n Wordpress Design: $wordpressdesign "; 
      $recipient = "[email protected]"; 
      $subject = "Contact form test with Jquery and PHP"; 
      $mailheader = "From: $Email \r\n"; 
      mail($recipient, $subject, $formcontent, $mailheader); 
      header("Location:http://www.google.com"); 
      exit();  
     } 
    } 

?><!--END PHP--> 

我拿出了jQuery,现在来测试PHP得到它的工作,但是当它是在index.php页面下面是

这里是代码。

<script type="text/javascript"> 
jQuery(document).ready(function($) { 
    $("#contactform").validate({ 
     rules: { 
      Name: { 
       required: true, 
       minlength: 4 
      }, 
      Email: { 
       required: true, 
       email: true 
      }, 
      Describe: { 
       required: true, 
       minlength: 25 
      }, 
      Phone: { 
       required: true, 
       number: true, 
       minlength: 7, 
       maxlength: 15 
      }, 
      Human: { 
       required: true, 
       number: true, 
       maxlength: 1, 
       range: [8,8] 
      } 
     }, 
     messages: { 
      Name: { 
       required: "Please enter your name", 
       minlength: "Your name seems a bit short doesn't it?" 
      }, 
      Email: { 
       required: "We need your email address to contact you", 
       email: "Please enter a valid email address" 
      }, 
      Describe: { 
       required: "Please enter your message", 
       minlength: "Please enter at least 25 characters. This way I know more specifically what you need!" 
      }, 
      Phone: { 
       required: "Please enter your phone number", 
       number: "Phone has to be numbers", 
       minlength: "Your phone number should be longer", 
       maxlength: "Number must be less!" 
      }, 
      Human: { 
       required: "Are you human?", 
       number: "has to be a number", 
       maxlength: "That's to long!", 
       range: "Please enter the correct number" 
      } 
     }, 
     errorElement: "div", //TAKE CONTROL OF ERROR MESSAGE IN CSS 

     //IF I CHOOSE TO ADD CSS AND WRAP ERROR WITH BOX 

     wrapper: "div", //WRAPPER AROUND THE ERROR MESSAGE 
     errorPlacement: function(error, element) { 
      error.insertAfter(element); 
      error.addClass('message'); //ADD A CLASS TO THE WRAPPER 
      /* 
      error.css('position', 'absolute'); 
      error.css('left', offset.left + element.outerWidth()); 
      error.css('top', offset.top); 
      */ 
    } 
    }); 
}); 
</script> 

任何帮助非常感谢!

+0

试验小学。你收到邮件了吗?用你的if(isset($ _ POST ['submit']))'把你的重定向放在第一个位置,看重定向是否工作。 – Daniel

+0

是的,它重定向这样做。把它放回最后给我一个电子邮件,内容只是根本不重定向页面。 –

+0

似乎您的重定向被一些发送到浏览器的错误中断。在你的php脚本的开头设置你的错误报告。 'error_reporting(-1);'看看php是否向浏览器吐出了一些东西。 – Daniel

回答

0

它看起来像你在你的header_contact.php解析HTML。在重定向之前,您无法解析HTML内容。尝试切换包含的PHP文件。

<?php include('testval.php');?> 

<?php include('header_contact.php') ?> 
+0

没有会议在任何地方。当我插入你发送的与上面相同的错误,而不是第6行时,它的行7now。因为将session_start()向下移动一个 –

+0

在第37行的'header_contact.php'中发生了一些事情,这导致了这种情况。你可以发布吗? – Daniel

+0

刚刚发布了它...看起来像标签是最后一个在第30行。我已经进入了下降到37.他们是空白 –

-2

试图改变自己的header位,在你test.php的最后的东西,看起来像这样:

header("Refresh:0; url=index.php"); 
+0

试过...它给我同样的问题 –

0
<!DOCTYPE HTML> 
<html> 


<head> 
    <meta charset="UTF-8 BOM"> 

      <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
      <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> 

    <title>Contact Form TEST</title> 


    <link rel="stylesheet" type="text/css" href="./css/style.css"> 
    <script type="text/javascript" src="./js/contact.js"></script> 
    <script type="text/javascript" src="./js/validation.js"></script> 
    <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script> 
    <script type="text/javascript" src="./js/jquery.validate.min.js"></script> 

    <!--MOO Script--> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.0/mootools-yui-compressed.js"></script> 





</head> 


<body> 
相关问题