在php中返回之前执行javascript
我确实有一个自定义 php 脚本,用于验证验证码并发送电子邮件。如果 php 脚本成功发送邮件,则返回“true”。这是通过以下方式完成的:
if(!$Error){
echo "true";
exit;
}
在返回 true 之前,我想执行一个 jQuery 命令来刷新验证码图像。我不应该在客户端这样做,因为从垃圾邮件发送者的角度来看,这可能存在风险。
要刷新验证码图像,命令是:
jQuery('#captcha').attr('src', ('php/captcha/captchaimage_show.php?' + Math.random()));
我需要在“echo”
Prashant返回任何结果之前从 php 脚本中调用此命令
I do have a custom php script that validates captcha code and sends an email. If php script is sending a mail successfully then it returns "true". This is done by:
if(!$Error){
echo "true";
exit;
}
Before returning true, I would like to execute a jQuery command that should refresh the captcha image. I shouldn't be doing this is client side as it might be risky from spammers point of view.
To refresh the captcha image, The command is:
jQuery('#captcha').attr('src', ('php/captcha/captchaimage_show.php?' + Math.random()));
I need to call this command from within php scripts before return any results by "echo"
Prashant
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要重新考虑您的方法。 PHP 和 JavaScript 运行在不同的计算机上,并通过无状态请求/响应协议进行通信。
任何 PHP 脚本都必须在 JS 运行之前完整运行。
仅当客户端发出 HTTP 请求时,才会在它们之间传递数据。
您无法在客户端以外的任何地方执行任何需要在客户端上运行的 JavaScript。
You need to rethink your approach. The PHP and JavaScript run on different computers and communicate via a stateless request/response protocol.
Any PHP script must run in its entirety before the JS runs.
Passing data between them can only occur when an HTTP request is made by the client.
You can't execute any JavaScript that needs to run on the client anywhere other than on the client.
我想说主要问题是通过 AJAX 发送邮件。您应该定期进行 POST,然后使用新的验证码重新加载整个页面。另一种可能的解决方案是通过 AJAX 发送邮件,然后在 AJAX 响应中返回“true”。或者甚至在响应中返回新的验证码并使用 jQuery 更改它。但我不知道它是否适用于您的验证码系统。
I would say that the main problem is sending the mail through AJAX. You should do a regular POST and then reload the whole page with a new captcha. Another possible solution would it be to send the mail through AJAX and then return the "true" in the AJAX response. Or even return the new captcha in the response and change it with jQuery. But I don't know if it will work with your captcha system.
会解决你的问题
哦!!!
Will solve your problem
chears!!!
关于您的代码:(
我猜 $Error 是您的邮件函数的返回类型。如果不是,请忽略这篇文章并让我知道,以便我可以将其删除)。
PHP.net:bool 邮件 ( 字符串 $to , 字符串 $subject , 字符串 $message)< /a>
仅当您想检查电子邮件是否已发送时,才不要依赖 mail() 返回值。
Regarding your code:
(I'm guessing $Error is the return type of your mail function. If not, disregard this post and let me know so I can take it off).
PHP.net:bool mail ( string $to , string $subject , string $message)
Don't rely on the mail() return value only if you want to check if the email was sent.