2013-03-23 129 views
1

這似乎確實是一個受歡迎的問題!首先,我真誠道歉重複這個問題。我已經嘗試了幾個修復程序在不同的問題中使用,並沒有發現任何可以解決我的問題。我的PHP聯繫表不會發送

我創建了一個網站,基本上希望從聯繫人表單向我發送查詢。我將嘗試定義我的問題: 1.我期望出現一條錯誤消息,其中提到'名稱是必填字段'或類似名稱,但這不是。 2.按提交時,頁面將我帶到一個空白頁面,該頁面將我指向我的服務器上的mail_form.php。

值得注意的是,我的服務器是123-reg誰說他們確實允許用戶腳本,但也提供了他們自己的例子 - 我試過這個沒用。

我不是100%肯定你會問看什麼,所以我會爲我的jsfiddle和相關位:

我的jsfiddle:http://jsfiddle.net/c9Sug/

我的PHP:

<?php 


    $yourEmail = "[email protected]"; // the email address you wish to receive these mails through 
    $yourWebsite = "HTTP://WWW.SSWEBTEST.CO.UK"; // the name of your website 
    $thanksPage = 'thankyou.html'; // URL to 'thanks for sending mail' page; leave empty to keep message on the same page 
    $maxPoints = 4; // max points a person can hit before it refuses to submit - recommend 4 




$error_msg = null; 
$result = null; 

function isBot() { 
    $bots = array("Indy", "Blaiz", "Java", "libwww-perl", "Python", "OutfoxBot", "User-Agent", "PycURL", "AlphaServer", "T8Abot", "Syntryx", "WinHttp", "WebBandit", "nicebot"); 

    $isBot = false; 
    foreach ($bots as $bot) 
    if (strpos($_SERVER['HTTP_USER_AGENT'], $bot) !== false) 
     $isBot = true; 

    if (empty($_SERVER['HTTP_USER_AGENT']) || $_SERVER['HTTP_USER_AGENT'] == " ") 
     $isBot = true; 

    exit("Bots not allowed.</p>"); 
} 

if ($_SERVER['REQUEST_METHOD'] == "POST") { 
    function clean($data) { 
     $data = trim(stripslashes(strip_tags($data))); 
     return $data; 
    } 

    $points = (int)0; 

    $badwords = array("adult", "beastial", "bestial", "blowjob", "clit", "cum", "cunilingus", "cunillingus", "cunnilingus", "cunt", "ejaculate", "fag", "felatio", "fellatio", "fuck", "fuk", "fuks", "gangbang", "gangbanged", "gangbangs", "hotsex", "hardcode", "jism", "jiz", "orgasim", "orgasims", "orgasm", "orgasms", "phonesex", "phuk", "phuq", "porn", "pussies", "pussy", "spunk", "xxx", "viagra", "phentermine", "tramadol", "adipex", "advai", "alprazolam", "ambien", "ambian", "amoxicillin", "antivert", "blackjack", "backgammon", "texas", "holdem", "poker", "carisoprodol", "ciara", "ciprofloxacin", "debt", "dating", "porn", "link=", "voyeur"); 
    $exploits = array("content-type", "bcc:", "cc:", "document.cookie", "onclick", "onload", "javascript"); 

    foreach ($badwords as $word) 
     if (strpos($_POST['comments'], $word) !== false) 
      $points += 2; 

    foreach ($exploits as $exploit) 
     if (strpos($_POST['comments'], $exploit) !== false) 
      $points += 2; 

    if (strpos($_POST['comments'], "http://") !== false || strpos($_POST['comments'], "www.") !== false) 
     $points += 2; 
    if (isset($_POST['nojs'])) 
     $points += 1; 
    if (preg_match("/(<.*>)/i", $_POST['comments'])) 
     $points += 2; 
    if (strlen($_POST['name']) < 3) 
     $points += 1; 
    if (strlen($_POST['comments']) < 15 || strlen($_POST['comments'] > 1500)) 
     $points += 2; 

    foreach ($_POST as $key => $value) 
     $_POST[$key] = trim($value); 

    if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) { 
     $error_msg .= "Name, e-mail and comments are required fields. \n"; 
    } elseif (strlen($_POST['name']) > 15) { 
     $error_msg .= "The name field is limited at 15 characters. Your first name or nickname will do! \n"; 
    } elseif (!preg_match("/^[a-zA-Z-'\s]*$/", stripslashes($_POST['name']))) { 
     $error_msg .= "The name field must not contain special characters. \n"; 
    } elseif (!preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i', strtolower($_POST['email']))) { 
     $error_msg .= "That is not a valid e-mail address. \n"; 
    } elseif (!empty($_POST['url']) && !preg_match('/^(http|https):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i', $_POST['url'])) 
     $error_msg .= "Invalid website url."; 

    if ($error_msg == NULL && $points <= $maxPoints) { 
     $subject = "Automatic Form Email"; 

     $message = "You received this e-mail message through your website: \n\n"; 
     foreach ($_POST as $key => $val) { 
      $message .= ucwords($key) . ": " . clean($val) . "\r\n"; 
     } 
     $message .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n"; 
     $message .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n"; 
     $message .= 'Points: '.$points; 

     if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) { 
      $headers = "From: $yourEmail \r\n"; 
      $headers .= "Reply-To: {$_POST['email']}"; 
     } else { 
      $headers = "From: $yourWebsite <$yourEmail> \r\n"; 
      $headers .= "Reply-To: {$_POST['email']}"; 
     } 

     if (mail($yourEmail,$subject,$message,$headers)) { 
      if (!empty($thanksPage)) { 
       header("Location: $thanksPage"); 
       exit; 
      } else { 
       $result = 'Your mail was successfully sent.'; 
      } 
     } else { 
      $error_msg = 'Your mail could not be sent this time.'; 
     } 
    } else { 
     if (empty($error_msg)) 
      $error_msg = 'Your mail looks too much like spam, and could not be sent this time. ['.$points.']'; 
    } 
} 
function get_data($var) { 
    if (isset($_POST[$var])) 
     echo htmlspecialchars($_POST[$var]); 
} 
?> 

我validation.css:

.forms li { 
    overflow: hidden; 
    list-style-type:none; 
} 


em.error { 
    clear: both; display: block; 
    font-size: 12px; font-style:italic; color: #f00; 
    margin: -10px 0 10px 175px; width:200px; 
} 

input, textarea { padding:5px; } 

我validation.js:

/* 
* Simple jQuery Form Validation Plugin 
* http://github.com/davist11/jQuery-Simple-Validate 
* 
* Copyright (c) 2010 Trevor Davis (http://trevordavis.net) 
* Dual licensed under the MIT and GPL licenses. 
* Uses the same license as jQuery, see: 
* http://jquery.org/license 
* 
* @version 0.1 
*/ 
;(function(b){b.fn.simpleValidate=function(l){var g=b.extend({},b.fn.simpleValidate.defaults,l);return this.each(function(){var d=b(this),a=b.meta?b.extend({},g,d.data()):g,h=a.errorText.search(/{label}/);d.bind("submit",function(i){var f=false;d.find(a.errorElement+"."+a.errorClass).remove();d.find(":input."+a.inputErrorClass).removeClass(a.inputErrorClass);d.find(":input.required").each(function(){var e=b(this),j=b.trim(e.val()),k=e.siblings("label").text().replace(a.removeLabelChar,""),c="";if(j=== 
""){c=h>-1?(c=a.errorText.replace("{label}",k)):(c=a.errorText);f=true}else if(e.hasClass("email"))if(!/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/.test(j)){c=h>-1?(c=a.emailErrorText.replace("{label}",k)):(c=a.emailErrorText);f=true}c!==""&&e.addClass(a.inputErrorClass).after("<"+a.errorElement+' class="'+a.errorClass+'">'+c+"</"+a.errorElement+">")});if(f)i.preventDefault();else if(a.completeCallback!==""){a.completeCallback(d);a.ajaxRequest&&i.preventDefault()}})})}; 
b.fn.simpleValidate.defaults={errorClass:"error",errorText:"{label} is a required field.",emailErrorText:"Please enter a valid {label}",errorElement:"strong",removeLabelChar:"*",inputErrorClass:"",completeCallback:"",ajaxRequest:false}})(jQuery); 

非常感謝你提前!

+0

嗯。至於我 - 所有是正確的(在js/html方面),我沒有使用這個插件,但我用[這一個](http://bassistance.de/jquery-plugins/jquery-plugin-validation/)在不同的項目中有好幾次都運行良好。在這裏,我看到表單立即提交,沒有突出顯示錯誤填充的字段。 – Andron 2013-03-23 18:53:36

+0

我找不出爲什麼那個不會工作,所以我重新編制它,並使用dreamweavertutorial.co.uk給出的示例創建一個新的聯繫表單。感謝大家熱衷於看我的劇本! – 2013-03-24 22:16:19

+0

你似乎拒絕了「hardcode」這個詞,我認爲這不是任何人對發誓的定義。 – meagar 2013-04-02 19:16:11

回答

0

不久前我做了一件非常相似的事情,並對同樣的問題感到非常沮喪。發現電子郵件正被髮送到我的垃圾郵件文件夾。你應該檢查那裏,看看你是否確實收到電子郵件。即使所有相同的標題都已填滿,它們仍然會在那裏結束。