Javascript 在函数中使用ajax值。
实际上,我需要知道如何在php函数中使用ajax值我可以查看输出。但是当我试图在函数中获得相同的值时,我不能Javascript 在函数中使用ajax值。,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,实际上,我需要知道如何在php函数中使用ajax值我可以查看输出。但是当我试图在函数中获得相同的值时,我不能 告诉我怎么修 我认为您应该添加global关键字,因为您不在同一范围内。 比如: <?php echo $getname;//value from ajax function mail_send() { $to = "sample@gmail.com"; $subject = "admin reference"; $message ="
告诉我怎么修 我认为您应该添加
global
关键字,因为您不在同一范围内。
比如:
<?php
echo $getname;//value from ajax
function mail_send()
{
$to = "sample@gmail.com";
$subject = "admin reference";
$message ="hai <b> $getname </b>";//here i cant get the name from ajax
}
?>
您应该将变量作为参数传递给函数:
<?php
function mail_send()
{
global $getname;
$to = "sample@gmail.com";
$subject = "admin reference";
$message ="hai <b> $getname </b>";//here i cant get the name from ajax
}
?>
这与AJAX无关,只是PHP中的普通变量作用域。在函数外部创建的变量在函数内部不可见,除非使用
global
声明。但是参数通常更好,因为它允许函数从代码的不同部分使用不同的变量。“hai$getname”
是有效的,因为双引号允许将$getname
解析为变量,所以这不是问题所在。这个问题可能与变量的范围有关。是的,这就是为什么我要问,如何解决它(ji:)你能告诉我代码的哪一部分实际上正在进行调用/调用mail_send()?将其余的代码放在一起会有很大帮助,因为我怀疑你只发布了一个片段。如果($time_difference>20){$email=mail_send();}虽然答案没有错,但使用global
是否不受鼓励?可能是的。我对PHP的良好实践并不感兴趣,重要的是理解范围。经典的“多语言”选项是将该值作为参数添加到函数中。@Kiruthiga由于使用全局
似乎不受欢迎,您可能需要尝试Barmar命题。谢谢
function mail_send($getname)
{
$to = "sample@gmail.com";
$subject = "admin reference";
$message ="hai <b> $getname </b>";//here i cant get the name from ajax
}
mail_send($getname);