0

I want to get value of $security_code which is in captchaCode.php file, in captcha.php file through javascript.I can not include this file in captcha.php file . Here is the code of Captcha_code.php and this code not comes inside any function of this file:

$captcha = new CaptchaCode();
$security_code = str_encrypt($captcha->generateCode(6));

And here is my javascript function, through this function i want to get the value of $security:

function refresh_captcha()
{
    var img = document.getElementById('captcha_img');
    img.src = '<?php echo "/captcha_images.php"?>';
    jQuery("#captcha_img").attr("src",img.src);
    jQuery("#security_code").val("<?php echo $security_code;?>");
}

Actually this code is for getting new encrypted value when captcha is refreshed without refreshing the page.

8
  • you need to use ajax. Commented Sep 10, 2013 at 7:18
  • 2
    Why do you need to output a static string with php? Why not just write the string in JavaScript? Commented Sep 10, 2013 at 7:18
  • Variable should be present in the current file, so you need to include it. Otherwise you have to send request to the .php file and use the answer which is given by it. Commented Sep 10, 2013 at 7:19
  • You have two options: Use Ajax to fetch PHP Value, or use cookies Commented Sep 10, 2013 at 7:19
  • Could you provide me ajax code..i donot understand how to do this Commented Sep 10, 2013 at 7:19

4 Answers 4

1

You have to change the image source assignment related code. No need to assign PHP tag. Just assign the PHP file name instead like this.

function refresh_captcha()
{
    var img = document.getElementById('captcha_img');
    img.src = '/captcha_images.php';
    jQuery("#captcha_img").attr("src",img.src);
    /*This line will not work without Ajax request*/
    /*jQuery("#security_code").val("<?php echo $security_code;?>");*/
}
Sign up to request clarification or add additional context in comments.

7 Comments

@itachi, No, javascript cannot communicate with PHP. It will only have the first value it originally receives.
@Starx erm.... here javascript doesn't need to do a thing. randomness will be ensure by php, not javascript.
@itachi, ????? I am really seeing your point. How does javascript not do a thing here?
The request to captcha_images.php will be made only once, isn't it?
my mistake. i thought OP was mentiong about captcha image only. in that case, ajax is overkill, otherwise, ajax is must.
|
0

Send an ajax request to a page to output only the code like:

File: outputcode.php (demo only)

$captcha = new CaptchaCode();
$security_code = str_encrypt($captcha->generateCode(6));
echo $security_code;
exit;

Next, grab that value using AJAX, like for example in jQuery

$("#refresh_code").on('click', function() {
   $.get("outputcode.php", function(data) {
      //data will now hold the new code
   });
});

Hope this gives you an idea.

3 Comments

ajax is overkill here. if you want the image to change, changing the img src attribute with a random string is enough.
its giving error that $ is not defined. If i use jQuery in place of $ then agian its giving error that jQuery is not defined...
@user2746093, You need to include jQuery library first.
0

If you cannot include the files, you need to use ajax request.

captcha.php

$captcha = new CaptchaCode();
$security_code = str_encrypt($captcha->generateCode(6));
echo json_encode($security_code);

in your js:

<img src="" id="captcha_img" onclick="refresh_captcha()"/>
<script>

    function refresh_captcha()
{
    $.ajax({
        url : 'captcha.php',
        type : 'POST',
        dataType : 'json',
        success : function (security_code) {
            var source = security_code;
            var img = document.getElementById('captcha_img');
            img.src = source;
            jQuery("#captcha_img").attr("src",img.src);
            jQuery("#security_code").val(source);
        }
    });

}
</script>

The onclick event on the img could be totally wrong, but for the excercise purpose, I've put it there.

Depends on what $security_code is, you can for example, not use JSON.

23 Comments

Now its not refreshing the captcha image
How it should refresh? It refreshes on click on the image in this example.
m clicking on image but its not refreshing the captcha
it's changing the src attribute, as far as I can test it, if it's not changing the actual image, there should be another problem
is there any other way to get the php variable javascript?
|
0

In your php file captcha.php,

    $captcha = new CaptchaCode();
    $security_code = str_encrypt($captcha->generateCode(6));?>
    <script>
         var code = '<?= $security_code ?>';
    </script><?php // rest of the php codes

In your js file

function refresh_captcha()
{
    var img = document.getElementById('captcha_img');
    img.src = code;
    jQuery("#captcha_img").attr("src",img.src);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.