1

Ill try and make this as clear as possible. I have a contact form on a page and a remote PHP file "random.php" that generates 2 random numbers:

<?php
    $randomNum = rand(0,9);
    $randomNum2 = rand(0,9);
    echo $randomNum . ' + ' . $randomNum2;
    $randomNumTotal = $randomNum + $randomNum2;
?>

the parent page calls it as such:

$(function() {
    $.get( 'PHP/random.php', function ( data ) {
    $('#ResetNumbers").val(data);    
});

and has an input:

<input type="text" class="numbers" name="ResetNumbers" id="ResetNumbers" value=""    size="6" readonly>

No problems to this point, the php file output's the two random numbers and they are displayed on the page. Here's the problem: I have a second text box where you have to add the two numbers to make it validate.

<input type="text" class="numbers" name="ResetMath"  id="ResetMath" size="6" maxlength="6" tabindex="27">

It never works. It always says its not added correctly. here is the relevant part of my validation rule which is in another remote file:

ResetMath: {
    required: true, 
    equal: <?php echo $randomNumTotal; ?>
}

Since this is in a remote file (don't want validation rules on the parent page) it doesn't seem to be getting the same numbers. How do I unscrew this mess?

4
  • does remote file where u write validation rule getting the value of $randomNumTotal'..check using console.log()`.. u must get that value on validation page Commented Feb 24, 2011 at 4:33
  • just stick console.log() where in the validation script? Commented Feb 24, 2011 at 4:38
  • Are you using the jQuery validation plugin; i.e. docs.jquery.com/Plugins/Validation/validate? Commented Feb 24, 2011 at 5:37
  • @Wayne yes thats what I'm using Commented Feb 24, 2011 at 16:50

2 Answers 2

1
ResetMath: {
    required: true, 
    equal: <?php echo $randomNumTotal; ?>
}

doesn't seem right. It should be:

ResetMath: {
    required: true, 
    equalTo: $('#ResetNumbers')
}

The API is here.

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

5 Comments

I don't believe that will get the SUM of the two numbers? It works when its all on one page, the validation function and the $randomNum + $randomNum2 function. It stores the sum in $randomNumTotal then checks against that
Oh I see. But if you want to use equalTo, the value has to stored in some field, which I assumed to be $('#ResetNumbers').
I need to store it somewhere but can't seem to get it to work
OK, so put it in some (new) hidden input field, and then set the value for equalTo to the jQuery expression that will select that field.
I couldn't store the number server side (not smart enough) so I used a custom validation rule client side to create a string of integers, remove the "+" and separate them, then combine and validate against that. your idea got me going. thx Wayne!
0

How does your validation rule get its value for $randomNumTotal? is this in the same request as the one which returns the values for $randomNum and $randomNum2? If not, you have a problem - (different random numbers for each).

2 Comments

yes that is where my problem lies. I am going to echo the $randomNumTotal to the parent page as well and see if that helps. Possibly populate a hidden input with that value and compare against it?
Yeah, I've considered the hidden input for a similar project (this is for a CAPTCHA, I assume). The issue here is that it wouldn't be that hard for a robot to find the $randomNumTotal if it resides in the frontend code. But if you're not that worried about that, it'll work fine.

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.