简体   繁体   English

通过php发送html dom中的文本

[英]Send via php post a text in html dom

I Have two form in html5, which have a title (event). 我在html5中有两种形式,它们都有一个标题(事件)。 When the user subscribe to an event, I will send two email: one to my own mail and one to the user mail. 当用户订阅活动时,我将发送两封电子邮件:一封发送到我自己的邮件,另一封发送到用户邮件。 In the mail for the user, I will write that he is successfully subscribed. 在给用户的邮件中,我将写出他已成功订阅。 And it works. 而且有效。 But I'd like to add also the name of the event (the content of the h1 title) in the email. 但是我还要在电子邮件中添加事件的名称( h1标题的内容)。 How can I do? 我能怎么做? Actually it leave an empty field in "event: " in the email's message. 实际上,它在电子邮件消息的“ event:”中保留了一个空字段。

Index.php: index.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>

Before to close the body I insert the jquery code to send the values to contact.php 在关闭body之前,我插入jQuery代码以将值发送到contact.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;
});
});

This is the contact.php code: 这是contact.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's $.ajax doesn't have an event property in it's options. jQuery的$.ajax中没有event属性。
You probably wanted to send that data with the form 您可能想使用以下形式发送数据

$(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;
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM