【问题标题】:PHP Ajax , Not able to pass a PHP value from one page to another using AJAXPHP Ajax,无法使用 AJAX 将 PHP 值从一个页面传递到另一个页面
【发布时间】:2020-10-01 06:44:58
【问题描述】:

我有一个使用 ajax 将值发布到另一个 php 页面的 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>';
}
?>

所以当我给 console.log in 函数和 onclick 时,它返回值但是当我尝试传递 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>"; 

?>

这是我得到的输出:

【问题讨论】:

  • 你知道id和class的区别吗?
  • 是的@EmielZuurbier,我发布这个是为了知道答案,你又在问我问题
  • ID和类的区别在于ID可以用来标识一个元素,而一个类可以用来标识多个元素。 @EmielZuurbier
  • 是的,所以只有循环中的第一个元素才能使用 click 函数,因为您的 id 不会是唯一的。但问题在于元素的输出。目前是 PHP,但我们无法想象生成的 HTML 会是什么样子,锚元素的 text() 值是什么。不要向我们展示您生成 HTML 的 PHP,而是展示实际的 HTML。
  • 请显示您的 ajax 请求标头、正文和响应。

标签: javascript php jquery ajax


【解决方案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 ; 

?>

【讨论】:

    猜你喜欢
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    相关资源
    最近更新 更多