问题恢复由ajax传递给php的变量

时间:2019-03-27 11:56:02

标签: php jquery ajax

我对getSegnalazioniMappa.php进行了“ POST” ajax调用。 当我尝试恢复传递的变量时,我注意到:未定义的变量。

JavaScript代码:

$(document).ready(function(){
    $('#gravita').change(function(){
         var index = document.getElementById("gravita").value;  

         $.ajax({
              method: "POST", 
              data:{index:index},
              url: "getSegnalazioniMappa.php",
              processData: false,
              success: function(data){
                 console.log(data);
              },
              error: function(e) {
                 alert(e.responseText);
              },
              dataType: "JSON"//set to JSON   
         }); 
    });
});

这是getSegnalazioniMappa.php

<?php
   require('../../../setup/database_connection.php');
   if(isset($_POST['index'])){  //this one is always false
    $index = $_POST['index'];
   }
?>

HTML

 <select name="gravita" onchange="updateTable(this.value)" style="width: 130px;" class="form-control" id="gravita" required>
        <option value="all" selected>Tutto</option>
        <option value="bassa">Bassa</option>
        <option value="media">Media</option>
        <option value="alta">Alta</option>
 </select>

2 个答案:

答案 0 :(得分:1)

由于未收到服务器的任何响应,因此在错误中未定义。您可以在控制台中看到。

还定义了onchange="updateTable(this.value)"的地方?

$(document).ready(function(){
    $('#gravita').change(function(){
         var index = $(this).val();  

         $.ajax({
              method: "POST", 
              data:{index:index},
              dataType: "JSON",
              url: "getSegnalazioniMappa.php",
              processData: false,
              success: function(data){
                 console.log(data);
              },
              error: function(xhr,textStatus,err) {
                 console.log("readyState: " + xhr.readyState);
                  console.log("responseText: "+ xhr.responseText);
                  console.log("status: " + xhr.status);
                  console.log("text status: " + textStatus);
                  console.log("error: " + err);
              },
                 
         }); 
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="gravita"  style="width: 130px;" class="form-control" id="gravita" required>
        <option value="all" selected>Tutto</option>
        <option value="bassa">Bassa</option>
        <option value="media">Media</option>
        <option value="alta">Alta</option>
 </select>

答案 1 :(得分:0)

您的PHP代码应如下所示:-

<?php
   if(isset($_POST['index'])){
    echo $index = $_POST['index'];
   }
?>

ajax请求应为:-

<script type="text/javascript">
$(document).ready(function(){
    $('#gravita').change(function(){
         var index = document.getElementById("gravita").value;  
         $.ajax({
              method: "POST", 
              data:{index:index},
              url: "getSegnalazioniMappa.php",
              processData: true,
              success: function(data){
                 alert(data);
                 console.log(data);
              },
              error: function(e) {
                 alert(e.responseText);
              },
              dataType: "JSON"//set to JSON   
         }); 
    });
});   
</script>

processData应该为true。