0

I have these two sessions vars on my php that generate a random number:

<?php
session_start();
if (!isset($_SESSION['num1']) && !isset($_SESSION['num2'])) {
$_SESSION['num1'] = rand(1,5);
$_SESSION['num2'] = rand(1,5);
}
?>

And on the same page I have this label that shows num1 + num2:

<label><?=$_SESSION['num1']?> + <?=$_SESSION['num2']?> ?</label>

What I would like to do is to turn the img that is right on the side of the label into a link, that will update num1 and num2 vars without refreshing the page, is it possible?

Should i create those vars in a separeted file like captcha.php, and then include it on the label showing the generated numbers with an echo on captcha.php or something like that? Or do I need to use ajax?

Can you guys give me a hand on this, i'm designer, trying to create a portfolio, and this form is driving me crazy! Thanks


@Koterpillar As you said, on my captcha.php i would have these:

<?php
session_start();
if (!isset($_SESSION['num1']) && !isset($_SESSION['num2'])) {
$_SESSION['num1'] = rand(1,5);
$_SESSION['num2'] = rand(1,5);
}
?>

Then i will include the captcha php into a div lets say #captcha. The captcha is done with a simple question like 1+3=, there is no drawing image or anything like that, its a small webpage, i think i wont need those noise and lines on the captcha, the validation is made on php submit form. but how to call the captcha php with ajax to update that div? Its something like this?

$("#captcha").click(function() {

$.ajax({
type: "POST",
url: "includes/captcha.php",
data: ,
success: function(){

},
});
}

Thanks for you help!!

2
  • ajax is the right idea..BTW do you use jquery ? Commented Mar 11, 2013 at 9:04
  • yes i'm using jquery and php only... but i think i will need this ajax thing! Or theres is another solution? Thanks for your help Commented Mar 11, 2013 at 19:22

3 Answers 3

1

You have to use Ajax if you want to "update num1 and num2 without refreshing the page". Suggest something like:

<div id='captcha'><?php include 'captcha.php'; ?></div>

and then Ajax-updating captcha div by requesting the same captcha.php:

$.get('includes/captcha.php', function (data) {
    $('#captcha').html(data);
});

(Obviously, rate-limiting, drawing the image, etc. is still needed.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help @Koterpillar, i really appreciated!! Could you help me writing this ajax updating if you have time? I'll edit my question so you can lead me to the right direction. Thanks
0

Session will run on one time... that mean when you open the page if session is set.... then set the session values and check the session values... I think you need to update your page after the loading .... then you need to use Ajax...That beacons session is server side.. without server request you can't set or get values from session..

Thank you...

2 Comments

Thansk for you reply @D.d.Tharanga! Unfortunately, i tryed, but i couldn't understand what you said. Its kind hard for a designer with no knowledge on php, ajax, jquery, to do something like that. But thanks man, i really appreciated your reply. if you could give some examples i don't know, maybe it would get easier :D
I think this link help u to find the details .... w3schools.com/php/php_ajax_intro.asp w3schools.com/php/php_sessions.asp
0

If you want updates without refreshing the page, then yes, you need to use Ajax.

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.