开发者

execute javascript before return in php

开发者 https://www.devze.com 2023-01-02 19:05 出处:网络
I do开发者_JS百科 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:

I do开发者_JS百科 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


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.


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.


file_get_contents("http://your-server/project-path/php/captcha/captchaimage_show.php?".random(1,100));

Will solve your problem

chears!!!


Regarding your code:

 if(!$Error){

(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).

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT > mean the mail will actually reach the intended destination.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号