簡體   English   中英

PHP Ajax,無法使用 AJAX 將 PHP 值從一個頁面傳遞到另一個頁面

[英]PHP Ajax , Not able to pass a PHP value from one page to another using 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>'; } ?>

因此,當我在函數中提供 console.log 和 onclick 時,它返回值但是當我嘗試傳遞 CustomerName 時,它​​只返回 null 。

點擊后獲取值

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

?>

這是我得到的輸出: 控制台圖像

將 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 ; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM