【发布时间】: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