James
James

Reputation: 110

Using PHP to convert JavaScript to HTML

I want to be able to use PHP to email a chart generated in JavaScript. If I can convert it to HTML I can use this,

<?php
$Now = date("n/j/Y g:i:s A");
  $to = "[email protected]";
  $subject = "Test" . " - " . $Now;
  $message = '<html>HTML here</html>';
  $from = "[email protected]";
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  $headers .= "From:" . $from;
  mail($to,$subject,$message,$headers);
?>

Does anyone know of a JavaScript to HTML converter? Or a way to take a page snapshot and download or email a webpage with JavaScript generated images? Thanks.

Upvotes: 0

Views: 182

Answers (1)

pje
pje

Reputation: 2468

Read the answer to the question that ziyuang posted in his comment. Once you read that you will realize that your question doesn't really make sense. HTML is simply a big long string served up by the web server when you make a request, and then that string is interpreted by your browser on the client side of the world. PHP is a server-side scripting language for creating dynamic HTML pages that can be served up to clients. JavaScript is an in browser scripting language that is used client-side (generally speaking).

To answer your original question, no there is no converter to convert between JavaScript and HTML, and it wouldn't make any sense if there was one. The point of JavaScript is to provide a way to interact with the DOM, and provide front end logic.

What I'm guessing is that you have some JavaScript UI widget (JQuery UI or the like), that is generating some chart for you. What this is actually doing when it runs in the browser is it is modifying the DOM (document object model). For more on the DOM read: http://www.w3.org/TR/DOM-Level-2-Core/introduction.html

If you wanted to email that chart to some email address, what you could do is get the inner html of the DOM node that the chart was created under. You could do this in JavaScript by doing something similar to the following:

// where myChart is the html element that contains the chart html
document.getElementById("myChart");
var chart = myChart.innerHTML;

You could then send this string back to your php script, which could then email it to whoever you wanted.

So, you can't convert JavaScript to HTML, but you can capture the HTML generated by the JavaScript, and "send" that back as a parameter to your php script on the server:

// Handling for IE
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
var url = "http://www.yourdomain.com/php_script.php?chart=" + chart;
xhr.open('GET', url, true); // 'true' makes this call asynchronous

Then create a JavaScript callback for when your request returns:

xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status < 400) {
        alert("success!");
    }
};

Hope this makes sense, cheers!

Upvotes: 1

Related Questions