user7393973 user7393973 - 1 year ago 59
HTML Question

How to use PHP withouth reloading the page

I have the following files with their respective code:

Controler.php



<iframe id="frame1" style="display:none"></iframe>
<iframe id="frame2" style="display:none"></iframe>
<button onClick="document.getElementById('frame1').src='Create.php'">Create</button>
<button onClick="document.getElementById('frame2').src='Drop.php'">Drop</button>


Create.php



<?php
mysqli_query(new mysqli('localhost','root'),'create database `db1`')
?>


Drop.php



<?php
mysqli_query(new mysqli('localhost','root'),'drop database `db1`')
?>


What they do is make a button that creates a database when clicked and another one that deletes that same database (named
db1
). My problem is getting it to work without having to reload the page.

Answer Source

In this case you should use AJAX. Onclick you can send a xmlhttprequest (JS) to a separate php file, which is executed.

Example:

function test()
  {
    xhr = new XMLHttpRequest();
    xhr.open("GET","your_ajax_file.php",true);
    xhr.send();
  }

your_ajax_file.php can create / drop the database.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download