2012-11-28 35 views
1

即時通訊在jQuery的noob,但學習速度很快。jquery - 不能發送「@」在變量

我一直在使用$ .post將值從一個html表單發送到一個php文件。而且每個人都可以工作,直到輸入的電子郵件地址包含「@」符號(當然他們都會這樣做),然後它就會中斷!

即時通訊正在試驗我的jquery很多。而即時嘗試做的是不使用html中的表單標籤。

梅,閱讀代碼。你會明白我的意思。我拿出所有的驗證和錯誤報告,所以它不是一個史詩TL:讀博士......

<table id='contactform'> 
    <tr> 
     <td> 
      <label for='name'>Name</label> 
      <input id='name' type='text'> 
     </td> 
     <td> 
      <label for='phone'>Phone</label> 
      <input id='phone' type='tel'> 
     </td>  
    </tr> 
    <tr> 
     <td colspan='2'> 
      <label for='email'>Email</label> 
      <input id='email' type='email'> 
     </td> 
    </tr> 
    <tr> 
     <td colspan='2'> 
      <label for='message'>Message</label> 
      <textarea id='message'></textarea> 
     </td> 
    </tr> 
    <tr> 
     <th colspan='2'> 
      <button id='sendmessage'>SEND MESSAGE</button> 
     </th> 
    </tr> 
</table> 

的JS,減驗證檢查...

$(document).ready(function() { 
$("input#name").focus(); 
$("button#sendmessage").click(function() { 
    $("table#contactform").hide(); 
    $("div#popin").show(); 
    $("div#popin").html("<img src='images/loader.gif'>"); 
    sendmessage(); 
}); 
}); 

function sendmessage() { 
$.post(
    "sendmessage.php", 
    {nameval:$("input#name").val(), phoneval:$("input#phone").val(), emailval:$("input#email").val(), messageval:$("textarea#message").val()}, 
    function(data) { 
     if (data == true) { 
      $("div#popin").html("<p>We have received your message and will reply asap</p>"); 
     } 
     else { 
      $("div#popin").html("<p>Something went wrong! Please contact [email protected]******</p>"); 
     } 
    } 
); 
} 

於是,一切變得發送到php文件罰款。它讀取帖子值,將它們通過電子郵件發送給我,並將數據「真實」返回給js,直到電子郵件中出現「@」爲止!

我知道我可以使用serialize_data()或任何它,但我希望沒有使用形式。哦,是啊,這...工作

var emailval = $("input#email").val().replace("@", "(at)"); 
$.post(
    "funcs/contact.php", 
    {nameval:$("input#name").val(), phoneval:$("input#phone").val(), emailval:emailval, messageval:$("textarea#message").val()}, 

但無論怎樣我取代「@」着,當我在php文件中使用str_replace函數,它只是再次打破。

有沒有人知道我要去哪裏錯了?

這裏的PHP ...

<?php 
$name = $_POST["nameval"]; 
$phone = $_POST["phoneval"]; 
$email = $_POST["emailval"]; 
$message = $_POST["messageval"]; 
$emailbody = "Name - $name\nPhone - $phone\nEmail - $email\n\n$message"; 
$recipient = "[email protected]*****"; 
$subject = "contact form"; 
$result = mail($recipient, $subject, $emailbody, "From:$email"); 
if ($result) { 
    echo true; 
} 
else { 
    echo false; 
} 
?> 
+0

嘗試發佈它之前逃脫的電子郵件。 'escape(emailval)' – phpisuber01

+1

你最後幾行讓我困惑。 「當我在php中使用str_replace時再次打破」是什麼意思?在PHP str_replace只是返回一個字符串。沒有什麼可以打破的。當@是字符串時什麼不工作?我的猜測是問題出在你的PHP代碼上。如果contact.php只包含print_r($ _ POST),那麼輸出是什麼? ? –

+0

我的意思是,我在js中使用replace(),然後在php中使用str_replace將其交換回來,並且php文件不返回'true'... – pkt666

回答

0

你可以看一下encodeURIComponent方法()JavaScript方法,不需要將它在服務器端進行解碼:

emailval:encodeURIComponent($("input#email").val())