Wednesday 15 April 2015

javascript - Place PHP results inside HTML page -


I have a PHP file that has code to echo some HTML, I want to provide a PHP file to some end users I can be done this way:

  & lt; Php include 'file.php'; ? & Gt;   

Unfortunately for my users the example index.html and this will not work. I do not want to tell my users to change the HTML file in PHP. Another way is to modify the .htaccess file:

  & lt; Files index.html & gt; AddType application / x-httpd-php .html & lt; / Files & gt;   

I do not want to ask them about it too. So what are my other options? Is it possible to show resonant results in HTML file? Maybe with the help of some javascript?

You can do this with AJAX. It may seem a bit daunting, but compared to a lot of thinking it is very simple. In fact, it is very easy.

Ajax goes into your Javascript code, and looks like this:

  $ ('# stselect'). Change (function () {var sel_stud = $ (this) .val (); // Alert ('You chose:' + sel_stud); $ .ajax ({type: "post", url: "your_php_file.php", Data: 'theOption =' + Sel_stud, success: function (whatigot) {warning ('server side response:' + whatigot);} / / END success fn}); // end $ .jax}); // END dropdown change event   

Note that the data of the PHP file comes in your HTML document in the success of AJAX calls , and Wherever should be dealt with, this is where this is where you put the data in the DOM.

For example, suppose that there is a DIV with your code in the HTML document id = "myDiv" to insert data from PHP into the HTML document, change the line : Warning ('server side response:' + whatigot); with:

  $ ('#myDiv') HTML (whatIgot).   

Presto! Your DIV now has the resonant data from the PHP file.


Ajax can be started with a control value (as in the example given above), or simply on document load:

  $ (Function () {// warning ('document is ready'); $ .ajax ({type: "post", url: "your_php_file.php", data: 'iemsending =' + this_var_val, success: function (whatigot) {// Alert ('server-side feedback:' + whatigot);} / / END success fn}); // END $ .jax}); // END document.ready   

Look at the ideas on how it works.

Note that the above examples use jQuery, and therefore tags for this reference are required for your page:

     

No comments:

Post a Comment