views:

102

answers:

4

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

+5  A: 

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.

David Dorward
But I need to refresh the captcha image after mail is sent. I could do this by executing the jQuery statement as mentioned above after jQuery.post command that is sending the mail. I am new to web programming and I thinkif I do this by client side, it can be ignored/removed then spammers can keep sending mails by changing email input field.
@user354051: Your problem is that you are allowing a CAPTCHA to be used more than once. It won't matter whether you change what's displayed (client- or server-side) because spammers could still submit the old CAPTCHA. You need to delete it once it is used, and not allow the same CAPTCHA to be used again (unless it gets randomly generated in the future).
DisgruntledGoat
A: 

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.

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

Will solve your problem

chears!!!

Partha Sarathi Ghosh
A: 

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.

Jan Kuboschek