PHP Ajax,无法使用AJAX将PHP值从一页传递到另一页

时间:2020-10-01 06:44:58

标签: javascript php jquery ajax

我有一个php代码,使用ajax将值发布到另一个php页面,但是提供的值未反映在UI或控制台中。

$(document).ready(function() {
  $('#CustomerName').click(function() {
    customername = $(this).text();
    load_data(customername);
    //  console.log(customername); //  works 
  });



  function load_data(Customername) {
    // console.log(Customername); // works 
    $.ajax({
      url: "getvalueafterclick.php",
      method: "POST",
      data: {
        Customername: Customername,
        EnvironmentType: "BA"
      },
      success: function(data) {
        $('#result').html(data);
      },
      error: function(jqXHR, textStatus, errorThrown) {
        alert("some error occured->" + jqXHR.responseJSON);
      }
    });
  }
});
<?php
// perform actions for each file found
foreach (glob("CustomerConfigFiles/*.json") as $filename) {
      echo ' <a href="#" id ="CustomerName" class="mm-active">
      <i class="metismenu-icon pe-7s-rocket"></i>'.substr(substr($filename,20), 0, -5).'</a>';
}
?>

因此,当我在功能和onclick中提供console.log时,它返回值,但是当我尝试传递CustomerName时,它仅返回null。

getvalueafterclick.php

<?php
$pat =$_POST['EnvironmentType'];
$lat =$_POST['Customername'];
echo "<script>console.log('Customer Name: " . $lat . "');</script>"; 
echo "<script>console.log('ENVIRON: " . $pat . "');</script>"; 

?>

这是我得到的输出: Console Image

1 个答案:

答案 0 :(得分:1)

将id更改为CustomerName的类,我已经添加/更改了您的代码,请查看它是否有助于解决您的问题。

        $(document).ready(function() {
      // changed # to . for class
          $('.CustomerName').click(function() {
            customername = $(this).text();
            load_data(customername);
            //  console.log(customername); //  works 
          });
    
    
    
          function load_data(Customername) {
            // console.log(Customername); // works 
            $.ajax({
              url: "getvalueafterclick.php",
              method: "POST",
              data: {
                Customername: Customername,
                EnvironmentType: "BA"
              },
              success: function(data) {
                $('#result').html(data);
               // console added to check what it is giving
                console.log(data);
              },
              error: function(jqXHR, textStatus, errorThrown) {
                alert("some error occured->" + jqXHR.responseJSON);
              }
            });
          }
        });
    <?php

     //your code
    // perform actions for each file found
// here in link change id to class for customer name 
    foreach (glob("CustomerConfigFiles/*.json") as $filename) {
          echo ' <a href="#" class="CustomerName" class="mm-active">
          <i class="metismenu-icon pe-7s-rocket"></i>'.substr(substr($filename,20), 0, -5).'</a>';
    }

// my testing code 

        $data =["a","b","c","d","e","f","g","h"];
     // perform actions for each file found
    // here in link change id to class for customer name 
        foreach ($data as $filename) {
              echo ' <a href="#" class="CustomerName" class="mm-active">
              <i class="metismenu-icon pe-7s-rocket"></i>'.$filename.'</a>';
        }
   
    


   ?>


    getvalueafterclick.php
      <?php
      $pat =$_POST['EnvironmentType'];
      $lat =$_POST['Customername'];
      echo "Customer Name:" . $lat . "-"; 
      echo "ENVIRON: " . $pat ; 

?>