jquery - 无法在变量中发送“@”

时间:2022-08-27 18:04:27

im a noob at jquery, but learning fast.

我是jquery的一个菜鸟,但学得很快。

ive been using $.post to send values from a html form to a php file. And everyone works until an email address is entered contain the "@" symbol (which they all do of course) then it breaks!

香港专业教育学院一直使用$ .post将值从html表格发送到php文件。每个人都工作,直到输入的电子邮件地址包含“@”符号(当然他们都这样做)然后它就会中断!

im being experimental with my jquery a lot. and what im trying to do is not use the form tags in the html.

我正在试验我的jquery很多。我试图做的是不使用html中的表单标签。

Meh, read the code. You'll see what I mean. I'm taking out all the validating and error reporting so it's not a epic tl:dr read...

嗯,阅读代码。你会明白我的意思。我正在取出所有的验证和错误报告,所以这不是一个史诗:博士读...

<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>

The js, minus validation checks...

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 webmaster@******</p>");
        }
    }
);
}

So, everything gets sent to the php file fine. It reads the post values, emails them to me fine, returns the data 'true' to the js... until there's a "@" in the email!!!

所以,一切都被发送到php文件。它读取帖子值,给我发好电子邮件,将数据'true'返回给js ...直到电子邮件中有“@”!

I know I could use serialize_data() or whatever it is, but I'm hoping to not to have use forms. Oh yea, and this worked...

我知道我可以使用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()},

But no matter what I replace the "@" with, when I use str_replace in the php file, it just breaks again.

但无论我用什么替换“@”,当我在php文件中使用str_replace时,它只会再次中断。

Does anyone know where I'm going wrong here?

有谁知道我在哪里错了?

Here's the php...

这是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 = "webmaster@*****";
$subject = "contact form";
$result = mail($recipient, $subject, $emailbody, "From:$email");
if ($result) {
    echo true;
}
else {
    echo false;
}
?>

1 个解决方案

#1


0  

You can look at encodeURIComponent() javascript method, no need to decode it at server side:

您可以查看encodeURIComponent()javascript方法,无需在服务器端对其进行解码:

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

#1


0  

You can look at encodeURIComponent() javascript method, no need to decode it at server side:

您可以查看encodeURIComponent()javascript方法,无需在服务器端对其进行解码:

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