通过 php post 在 html dom 中发送文本


Send via php post a text in html dom

>我在html5中有两个表单,它们有一个标题(事件)。当用户订阅事件时,我将发送两封电子邮件:一封发送到我自己的邮件,另一封发送给用户邮件。在给用户的邮件中,我会写下他已成功订阅。它有效。但我还想在电子邮件中添加事件的名称(h1标题的内容)。我该怎么办?实际上,它在电子邮件消息中的"事件:"中留下了一个空白字段。

索引.php:

<form  name="contact1" action="index.php" method="POST" id="contact1">    
    <h1 id="event">Event 1</h1>
    <div>Name: <input type="text" name="name" id="name"   required /></div> 
    <div>Email: <input type="email" name="email" id="email"  required /></div>
    <div><input type="submit" name="submit" value="Submit" /></div> 
</form>
<form name="contact2" action="index.php" method="POST" id="contact2">    
    <h1 id="event">Event 2</h1>
    <div>Name: <input type="text" name="name" id="name"   required /></div> 
    <div>Email: <input type="email" name="email" id="email"  required /></div>
    <div><input type="submit" name="submit" value="Submit" /></div> 
</form>

<div id="results"></div>

在关闭body之前,我插入 jquery 代码以将值发送给联系人.php

$(document).ready(function() {
$("form").submit(function() {
    // Getting the form ID
    var  formID = $(this).attr('id');
    var formDetails = $('#'+formID);
    $.ajax({
        type: "POST",
        url: 'contact.php',
        event:$("#event").text(),
        data: formDetails.serialize(),
        success: function (data) {  
            // Inserting html into the result div
            $('#results').html(data);
        },
        error: function(jqXHR, text, error){
        // Displaying if there are any errors
            $('#result').html(error);           
    }
});
    return false;
});
});

这是联系人.php代码:

<?php
    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }
if(isset($_POST['email'])){
    $responso = "Success: request sent.";
    $email_to = "admin@admin.com"; // Send to admin
    $email_subject = "New user"; // Object Email to admin
    $first_name = $_POST['name']; // name of user  
    $email_from = $_POST['email'];   // email of user
    $event = $_POST['event']; // name of the event

    $email_message .= "Name: ".clean_string($first_name)."'n";
    $email_message .= "Email Address: ".clean_string($email_from)."'n";
    $email_message .= "Evento: ".clean_string($event)."'n";

    // First mail to admin
    $headers = 'From: '.$email_from."'r'n". 'Reply-To: '.$email_from."'r'n" . 'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers); 
    // Second mail to new user      
    $email_subject2 = "Confirmation"; // Object Email to new user
    $email_message2 .= "Successful subscribed to ".clean_string($event)."'n";
    $email_message2 .= "dear ".clean_string($first_name)."'n";
    $headers2 = 'From: '.$email_to."'r'n". 'Reply-To: '.$email_to."'r'n" . 'X-Mailer: PHP/' . phpversion();
    @mail($email_from, $email_subject2, $email_message2, $headers2); 
}else{
    $responso = "Error: Insert your email";
    } 
die( $responso );  
?>

jQuery的$.ajax在其选项中没有event属性。
您可能希望与表单一起发送该数据

$(document).ready(function() {
    $("form").submit(function() {
        var event_data  = encodeURIComponent( $("#event").text() );
        $.ajax({
            type: "POST",
            url: 'contact.php',
            data: $(this).serialize() + "&event=" + event_data,
            success: function(data) {
                $('#results').html(data);
            },
            error: function(jqXHR, text, error) {
                $('#result').html(error);
            }
        });
        return false;
    });
});