"place php results inside html page" Answer’s


You can do this with AJAX. It might look a bit challenging, but it is frankly much simpler than many think. In fact, it's pretty easy.

Ajax goes in your javascript code, and looks like this:

$('#stSelect').change(function() {
    var sel_stud = $(this).val();
    //alert('You picked: ' + sel_stud);

        type: "POST",
        url: "your_php_file.php",
        data: 'theOption=' + sel_stud,
        success: function(whatigot) {
            alert('Server-side response: ' + whatigot);
        } //END success fn
    }); //END $.ajax
}); //END dropdown change event

Note that the data from the PHP file comes into your HTML document in the success function of the AJAX call, and must be dealt with there. So that's where you insert the received data into the DOM.

For example, suppose your HTML document has a DIV with the id="myDiv". To insert the data from PHP into the HTML document, replace the line: alert('Server-side response: ' + whatigot); with this:


Presto! Your DIV now contains the data echoed from the PHP file.

The ajax can be triggered by a change to an control's value (as in the above example), or just at document load:

$(function() {
//alert('Document is ready');

        type: "POST",
        url: "your_php_file.php",
        data: 'Iamsending=' + this_var_val,
        success: function(whatigot) {
            //alert('Server-side response: ' + whatigot);
        } //END success fn
    }); //END $.ajax
}); //END document.ready

Look at this example for ideas on how it works.

Note that the above examples use jQuery, and therefore require this reference in the tags of your page:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
Wednesday, March 31, 2021
answered 10 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :