admin管理员组

文章数量:1391811

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.

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.

Share Improve this question asked Dec 1, 2009 at 17:43 JProJPro 6,55613 gold badges59 silver badges85 bronze badges 2
  • It can be done on server side. – Ivan Nevostruev Commented Dec 1, 2009 at 17:48
  • You might have just done this for the example, but the first character of ID shouldn't be a number. As per the W3C spec, it should be in [a-zA-Z]. This could potentially lead to issues with some of the solutions below. – John McCollum Commented Feb 13, 2010 at 12:22
Add a ment  | 

2 Answers 2

Reset to default 6

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);

?>

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./ajax/libs/jquery/1.3.2/jquery.min.js"></script>

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).

本文标签: javascriptemail div content in phpStack Overflow