【问题标题】:How to set a php variable inside a javascript function?如何在 javascript 函数中设置 php 变量?
【发布时间】:2018-04-12 06:54:51
【问题描述】:

我想创建一个 OTP 发送器和检查器。
代码是这样的:

<?php
	$Mail = "someone@gmail.com";
		$OTP = mt_rand();
		$subject = "OTP from HelloBuddy.co.in";
		$txt = "You have recieved OTP from HelloBuddy.co.in. The OTP is $OTP. Please enter the OTP in the website. Please note that the OTP will expire in 100 seconds.";
    if (mail($Mail,$subject,$txt)){
			echo "<p>OTP has been successfully sent<br>The OTP will expire in <span id='xpire'>100</span> seconds.<br>Please enter the OTP here:<input type='number' name ='OTP'></p>";
			echo "<script>
					var a = 99;
					var x = setInterval(cdown, 1000);
					function cdown(){
						if (a>=0) {
							document.getElementById('xpire').innerHTML = a;
							a=a-1;
						} else {
							clearInterval(x);
							clearOTP()
						}
					function clearOTP{
					$OTP = '';
          }
          </script>";
    }else{
   die(ERROR);
}
?>

我正在使用 xampp 服务器并已正确配置所需文件。
我想在 100 秒后清除 OTP,但无法清除,可能是因为它位于 javscript 函数中。
如果可能,请提供 AJAX。

【问题讨论】:

  • 您可以在 javascirpt 中使用 php 变量,方法如下:“”。
  • 但我的 javscript 代码也在 中。在另一个 ??? 里面可以有
  • @HyperMaths.com 最好的方法是在会话中存储 OTP。
  • 我想指出,将 html/js/php/css 混合在一起是一种不好的做法,会使代码难以阅读、修改并为难以发现的缺陷创造空间。请查看关注点分离、MVC 框架和模板。
  • 谢谢你。我已经正确格式化了。

标签: javascript php ajax one-time-password


【解决方案1】:

您不能使用 javascript 设置 php 变量。

PHP 将在服务器端执行,而 javascript 在客户端执行。所以所有的 PHP 代码在 javascript 被调用之前就已经被执行了。

$OTP 变量尚未保存。因为代码在服务器端执行并且将被垃圾收集。如果你想保存和删除 OTP,你应该使用某种数据库。

【讨论】:

  • 有没有ajax替代方案
  • 我知道这是问题所在。
【解决方案2】:

在脚本前使用 php 标签,然后连接 php 变量

试试这个,

<?php
$Mail = "someone@gmail.com";
    $OTP = mt_rand();
    $subject = "OTP from HelloBuddy.co.in";
    $txt = "You have recieved OTP from HelloBuddy.co.in. The OTP is $OTP. Please enter the OTP in the website. Please note that the OTP will expire in 100 seconds.";
if (mail($Mail,$subject,$txt)){
        echo "<p style='color:green; font-size:30px; text-align:center; text-decoration: underline;'>OTP has been successfully sent<br>The OTP will expire in <span id='xpire'>100</span> seconds.<br>Please enter the OTP here:<form method ='POST' action = ''  style='text-align:center'><input style='color:green; font-size:30px; text-align:center;text-decoration: underline;'type='number' name ='OTP'><input style='color:white; background-color:#4CAF50;border:2px #4caf50 solid; font-size:30px; cursor:pointer;' type='submit' value='Check'></form></p>";
        ?><script>
                var a = 99;
                var x = setInterval(cdown, 1000);
                function cdown(){
                    if (a>=0) {
                        document.getElementById('xpire').innerHTML = a;
                        a=a-1;
                    } else {
                        clearInterval(x);
                        clearOTP()
                    }
                function clearOTP{<?=$OTP ='';?>}
      </script>

<?
    }
?>

【讨论】:

  • 你不能详细说明
  • 如果mail()有问题怎么办?像 else{die(ERROR)} 的意思。到时候也会执行这个功能吗?
  • mail() 有问题是什么意思?您的邮件功能有错误?
  • 如果有问题
  • 如果您的 mail() 有问题,您的 if 条件中的脚本将不会执行
【解决方案3】:

php 变量只能在 JS 中使用,不能被操作(初始化/更新)。 上面的任务可以通过ajax来使用,在js中保持otp的值并重置,使用ajax来执行一些其他的操作,比如OPT验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多