简体   繁体   English

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

[英]PHP Ajax , Not able to pass a PHP value from one page to another using AJAX

I have a php code that post value using ajax to another php page , but the value provided is not reflecting in UI nor in console .我有一个 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>'; } ?>

So when i give console.log in function and onclick , it returns value but when i try to pass CustomerName , it just returns null .因此,当我在函数中提供 console.log 和 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>"; 

?>

Here is output i get :这是我得到的输出: 控制台图像

Change id to class for CustomerName , I have added/ changed your code, see if it helps to solve your problem.将 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