What I want to do is have different buttons, and when each one of it is clicked, it makes a different query in a MySQL from the others. For example, button A → query A, button B → query B, and so on (it doesn’t matter what query right now). I have no idea how to do this (i searched a lot but I didn’t find something useful). If you could give me some directions, i would be very grateful. This is what I have so far:
EDITED: I know how to send data from a form and how to make MySQL queries with PHP. What I DON’T know is how to assign this differents queries to differents buttons (for example, a button to create a database, other one for delete, other one for create a table, etc). I dind’t add the queries because they are not important right now, I just want to do if exists a way to do (for example,a javascript function that check if the button is pressed, then make the ajax call, open a connection, make the querie, return a result and close the connection).
window.addEventListener("load", process);
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
}
catch(e){
var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP");
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++){
try {
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e){}
}
}
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
function process(){
if (xmlHttp){
try {
xmlHttp.open("GET", "server_script.php?", true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(null);
}
catch (e){
alert("Can't connect to server:\n" + e.toString());
}
}
}
function handleRequestStateChange(){
if (xmlHttp.readyState == 4){
if (xmlHttp.status == 200){
try {
handleServerResponse();
}
catch(e){
alert("Error reading the response: " + e.toString());
}
}
else {
alert("There was a problem retrieving the data:\n" +
xmlHttp.statusText);
}
}
}
function handleServerResponse(){
document.getElementById("ResponseDiv").innerHTML = xmlHttp.responseText;
}
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if($conn -> connect_error) {
die("Connectio failed: " . $conn->connect_error);
}
echo "Connected sucesfully";
$conn->close();
?>