我有这个php和js文件
PHP<?php
$user_id = '10';
echo $user_id;
?>
JS
$(document).ready(function() {
$.ajax({
url:"uid.php",
success:function(data){
alert(data);
}
});
});
我得到"10"作为警报
但是对于PHP和JS来说,没有警告
PHP<?php
class uidclass{
function uid_func($event, $arguments)
{
$user_id = '10';
echo $user_id;
}
}
?>
JS
$(document).ready(function() {
$.ajax({
url:"uid.php",
success:function(data){
alert(data);
}
});
});
Thanks in advance
您需要正确使用类和方法
<?php
class uidclass{
function uid_func($event, $arguments) {
$user_id = '10';
echo $user_id;
}
}
$bob = new uidclass;
$bob->uid_func(null,null); // since the parameters are not defined
或者像这样
<?php
class uidclass{
function uid_func($event = null, $arguments = null) {
$user_id = '10';
return $user_id;
}
}
$bob = new uidclass;
print $bob->uid_func(); // we have already set defaults in the function definition
或者甚至添加一个构造函数并在方法中打印
<?php
class uidclass{
public function uid_func($event = null, $arguments = null) {
$user_id = '10';
print $user_id;
}
public function __construct() {
$this->uid_func();
}
}
$bob = new uidclass();