dpylt7626401 2019-04-12 10:55
浏览 84

通过ajax将数据从javascript传递到php,动态选择一个mysql表并在javascript中检索查询数据

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 !

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题