i am stuck with ajax and php, i would like to pass information to a php file that communicate with mysql and retrieve data in a variables. For the moment i managed to pass data via javascript to php, i can't to do the opposite.
Here is the code HTML CODE
<form action="">
<select name="misura" onchange="show(this.value)">
<option value="">scegli la misura</option>
<option value="1">1</option>
<option value="2">2</option>
-->
<option value="husqvarna_272xp_catena_nuova_operatore_1">husqvarna_272xp_catena_nuova_operatore_1</option>
<option value="husqvarna_550xp_catena_usata_operatore_2">husqvarna_550xp_catena_usata_operatore_2</option>
<option value="husqvarna_550xp_catena_usata_operatore_1">husqvarna_550xp_catena_usata_operatore_1</option>
<option value="husqvarna_550xp_catena_nuova_operatore_2">husqvarna_550xp_catena_nuova_operatore_2</option>
<option value="husqvarna_550xp_catena_nuova_operatore_1">husqvarna_550xp_catena_nuova_operatore_1</option>
</select>
</form>
</form>
JAVASCRIPT/AJAX
function show(str){
var xhttp;
if(str == "") {
document.getElementById("txtHint").innerHTML ="";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200){
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xhttp.open("GET","database_web.php?q="+str, true);
xhttp.send();
}
PHP CODE IN THE FILE DATABASE.PHP
<?php
$q = intval($_GET['q']);
$con=mysqli_connect("localhost","USER","PASS","DB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$dati="SELECT * FROM ".$_GET['q']."";
$result = mysqli_query($con,$dati);
echo "<table>
<tr>
<th>Frequenza</th>
<th>dati_X</th>
<th>dati_Y</th>
<th>dati_z</th>
</tr>";
while($row=mysqli_fetch_array($result,MYSQLI_NUM)) {
echo "<tr>";
echo "<td>" . round($row[0],2) . "</td>";
echo "<td>" . round($row[1],2) . "</td>";
echo "<td>" . round($row[2],2) . "</td>";
echo "<td>" . round($row[3],2) . "</td>";
$prova = $row;
$xData[] = $row[0];
}
mysqli_close($con);
Convert to json in the file database.php
<script type="text/javascript">
// pass PHP variable declared above to JavaScript variable
// operatore 1 antivibrante
// misura 1 exp
var assex =
<?php echo json_encode($xData) ;?>;
console.log(assex);
</script>
In the console.log i can't view nothing and i can't understand why. My objective are to retrieve data from sql interrogation and create a graph dynamically with the data.
I made a page for testing in this address
http://www.rdgdesign.it/elaboration/ajax_web.php
Thanks for all !