views:

268

answers:

3

Hi, Is it possible to take the content that is under a specific <div> and email that content?

For example: If I have something like this:

<div id="1">
<ul>
 <li>a</li>
 <li>b</li>
<ul>
</div>

Basically I want to just reference <div> and take the whole content and email it. is there any way to cache the contents in the div? Is this anyway possible? ( javascript? php?)

Thanks.

+5  A: 

It really depends on what further requirements you have. You could have a jQuery script send the contents to a PHP script, which then emails them out:

$.post("email.php", { data : $("div#1").html() }, function(result){
  /* handle results */
});

And then your email.php script looks similar to the following (Don't use the following code as-is):

<?php

  $to = "[email protected]";
  $subject = "HTML Data";
  $message = $_POST["data"];
  $headers = "From: The Server <[email protected]>" . "\r\n" .
             "Content-type: text/html" . "\r\n";

  mail($to, $subject, $message, $headers);

?>
Jonathan Sampson
+1  A: 

As stated above, jQuery is probably the easiest way for you. You can add jQuery to your page with the following line:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;

You can also reference your content by using just $('#1').html(), or whatever the ID of your div is (in your example, it's 1).

jnunn
A: 

All I get from this script is an email where the message contained is 'null' I tried it even with the simplest div possible and still nothing. Can anyone tell me what I am doing wrong?

Thanks

Robin I Knight