使用 jQuery/Javascript 在电子邮件表单中使用 contenteditable

发布于 2024-12-26 04:34:33 字数 1896 浏览 2 评论 0原文

我从堆栈中的其他地方获取了一些脚本来创建一个联系表单,当用户提交时该表单会向我发送电子邮件。

HTML:

<form onsubmit="return getContent()">
    <fieldset>
        <input name="name" value="NAME" style="max-width:15%" type="text"/>
        <input name="email" value="EMAIL" style="max-width: 30%" type="text"/>
        <div id="message" contenteditable="true" name="message" value="expanding textarea">HELLO</div>
        <textarea id="my-textarea" value="MESSAGE" style="display:none"></textarea>
        <input type="submit" style="margin-right:0" value="Submit" />
    </fieldset>
</form>

SCRIPTS:

    function getContent(){
        document.getElementById("my-textarea").value = document.getElementById("message").innerHTML;
    }

    $('form').submit( function() {
    $.ajax({
        type: "POST",
        url: "email.php",
        data: $(this).serialize(),
        success: function() {
            // Update page with success message
        }
    });
    return false;
});

PHP:

<?php 

$recipient = "[email protected]"; //recipient 

$Name = ($_POST['name']); //senders name 
$email = ($_POST['email']); //senders e-mail adress 
$mail_body = ($_POST['message']); //mail body 

$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields 

mail($recipient, $subject, $mail_body, $header); //mail command :) 
?>

脚本应该获取 contenteditable div 中输入的内容并将其传递给隐藏的文本区域,然后提交到我的电子邮件。但是,它目前仅传递 "name""email" 字段,并将电子邮件正文留空。

我在这里错过了什么?

编辑:在上面的代码隐藏文本区域中犯了一个小错误,值=“MESSAGE”而不是“SUBJECT”。尽管如此,代码仍无法正常工作。

I've taken a few scripts from elsewhere on the Stack to create a contact form that emails me when the user submits.

HTML:

<form onsubmit="return getContent()">
    <fieldset>
        <input name="name" value="NAME" style="max-width:15%" type="text"/>
        <input name="email" value="EMAIL" style="max-width: 30%" type="text"/>
        <div id="message" contenteditable="true" name="message" value="expanding textarea">HELLO</div>
        <textarea id="my-textarea" value="MESSAGE" style="display:none"></textarea>
        <input type="submit" style="margin-right:0" value="Submit" />
    </fieldset>
</form>

SCRIPTS:

    function getContent(){
        document.getElementById("my-textarea").value = document.getElementById("message").innerHTML;
    }

    $('form').submit( function() {
    $.ajax({
        type: "POST",
        url: "email.php",
        data: $(this).serialize(),
        success: function() {
            // Update page with success message
        }
    });
    return false;
});

PHP:

<?php 

$recipient = "[email protected]"; //recipient 

$Name = ($_POST['name']); //senders name 
$email = ($_POST['email']); //senders e-mail adress 
$mail_body = ($_POST['message']); //mail body 

$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields 

mail($recipient, $subject, $mail_body, $header); //mail command :) 
?>

The script SHOULD get the content entered in the contenteditable div and pass it through to the hidden textarea, then submit to my email. However, it's currently only passing through the "name" and "email" fields and leaving the body of the email blank.

What have I missed here?

EDIT: Made a small error in the code hidden textarea above, value="MESSAGE" not "SUBJECT". Still, code not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨笨の傻瓜 2025-01-02 04:34:34

提交时,可内容编辑的内容不会通过表单传递。
但是,看起来您正在使用一些 JS 来获取可编辑内容 div 的内容并将其放入名为“subject”的文本区域中。

因此,只能通过 $_POST['subject'] 访问它。

另外,您正在 html 中执行 onsubmit="" ,并在 JS 文件中执行 on Submit 。我认为两者都不起作用,你最好将两者结合起来(即只使用 JS 文件)。

如果没有启用 JS,它也无法工作,我认为这是一个更大的问题。

Contenteditable content doesn't get passed through the form when submitted.
But, it looks like you're using some JS to get the content of the editable content div and put it into the textarea with a name of "subject".

Therefore, it will be accessible through $_POST['subject'] only.

Also, you're doing an onsubmit="" in the html and a on submit in the JS file. I don't think both will work, you'd be better combining the two (i.e. just use the JS file).

It also won't work without JS enabled, which I'd say is a bigger problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文