Flidiot
Flidiot

Reputation: 135

Ajax request JQuery Sending div

I have an ajax request to save my mail template in the database. However I can't send my div with the ajax request ? I've been looking on the internet for 2 hours now but I couldn't find a solution

the ajax file :

function saveEmail(){
    var mail3 = $jQ('templateContent');
    $jQ.ajax({
        method: 'POST', 
        url: 'ajax/saveMail.php',
        data: {mail2: mail3.html(), test: "test"}
    }).done(function( data ) {
        console.log(data);
    });

}  

saveMail.php :

echo '<pre>';
print_r($_POST);
echo '</pre>';

exit();

The only POST value i get is test example:

    <pre>Array
    (
        [test] => test
    )
    </pre>

some usefull info :

mail3 = `[prevObject: jQuery.fn.jQuery.init[1], context: document, selector: "templateContent", jquery: "1.10.2", constructor: function…]`

Upvotes: 1

Views: 1098

Answers (3)

DhirendraBisht
DhirendraBisht

Reputation: 110

var mail3 = $jQ('templateContent');

Should be something like
$jQ('.templateContent'); or $jQ('#templateContent');

Upvotes: 3

Preeti Joshi
Preeti Joshi

Reputation: 989

The following gives the div's contents in the ajax data call. Note the jquery selector for the div i.e. var dataToSend = $("#templateContent").text();

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>    
<script>
function myFunction() {
    var dataToSend = $("#templateContent").text();
    alert(dataToSend);
    $.ajax({
        method: 'POST', 
        url: 'ajax/saveMail.php',
        data: {mail2: dataToSend, test: "test"}
    }).done(function( data ) {
        console.log(data);
    });
}
</script>
</head>
<body>

<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>

<button onclick="myFunction()">Click me</button>

<div id="templateContent"><span><p>This is supposed to be sent in email!</P><span></div>



</body>
</html>

Upvotes: 0

kupendra
kupendra

Reputation: 1003

You forget to use templateContent as a element class or id

Try to use .templateContent or #templateContent.

So you can get content of element by using it's id or class.

Upvotes: 1

Related Questions