Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:54:45+00:00 2026-06-06T12:54:45+00:00

I am pulling two random numbers to create a math equation in a symfony

  • 0

I am pulling two random numbers to create a math equation in a symfony form. The problem is when the form is submitted, the random numbers are updated, making the form submission invalid. How can I keep the initial number values loaded, so the form will process, but after successful process, load a new set?

actions.class

 public function executeIndex(sfWebRequest $request)
  {
    $num1 = rand(0 , 20);
    $num2 = rand(0 , 20);
    $realAnswer = $num1 + $num2;

    $show = $num1 . " + " . $num2 . " = ";

    $this->getResponse()->setSlot("displayNum", $show);

    $this->form = new UserForm();

    echo $num1 . "<br>" . $num2 . "<br>" . $realAnswer;

    if ($request->isMethod('post') && ($request->getPostParameter('captcha[answer]') == $realAnswer))
    {
        $this->processForm($request, $this->form);  
    }
  }

I am using a partial to render the form -> _form.php

<tr height="40">
        <td width="100" valign="middle">
            <?php echo get_slot("displayNum", "default value if slot doesn't exist"); ?>
        </td>
        <td width="400" colspan="4" valign="middle">
        <input type="text" id="captcha" class="url" name="captcha[answer]" style="width:100px" />
        </td>
    </tr>

Example: When the page initially loads, two random numbers are generated (ex. 10 & 15). This renders
10 + 15 = (input field)

The user inserts 25 and clicks save. This was correct, but because the form executes the index action again, there is a new set of random numbers making the “if” condition false.

UPDATE:

Per j0k’s suggestion I have made the changes to the action.

public function executeIndex(sfWebRequest $request)
    {
      $user = $this->getUser();

    if (!$request->isMethod('post'))
    {
      // first display of the form, generate nums
      $num1       = rand(0 , 20);
      $num2       = rand(0 , 20);
      $realAnswer = $num1 + $num2;

      // store them in session
      $user->setAttribute('realAnswer', $realAnswer);
      $user->setAttribute('num1', $num1);
      $user->setAttribute('num2', $num2);
    }
    else
    {
      // the form is submitted, retrieve nums from the session
      $num1       = $user->getAttribute('realAnswer', null);
      $num2       = $user->getAttribute('num1', null);
      $realAnswer = $user->getAttribute('num2', null);
    }

    //setup slot
     $show = $num1 . " + " . $num2 . " = ";
     echo $realAnswer . "-Actual<br>" . $request->getPostParameter('captcha[answer]') . "-User submitted";

    $this->form = new UserForm();

    if ($request->isMethod('post') && (($request->getPostParameter('captcha[answer]') == $realAnswer)))
    {
        $this->processForm($request, $this->form);  
    }
  }

which should work. Looking at the variables, it looks like the page is pulling the session variable and not adding new random numbers on the second post. weird.

RESOLVED

It was a code error. I had the variables crossed up.

else
    {
    // the form is submitted, retrieve nums from the session
     $num1       = $user->getAttribute('num1', null);
     $num2       = $user->getAttribute('num2', null);
     $realAnswer = $user->getAttribute('realAnswer', null);
../
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:54:46+00:00Added an answer on June 6, 2026 at 12:54 pm

    Session is a good point.

    If the form isn’t posted, store the result in the session to be able to check it after.

    public function executeIndex(sfWebRequest $request)
    {
      $user = $this->getUser();
    
      if (! $request->isMethod('post'))
      {
        // first display of the form, generate nums
        $num1       = rand(0 , 20);
        $num2       = rand(0 , 20);
        $realAnswer = $num1 + $num2;
    
        // store them in session
        $user->setAttribute('realAnswer', $realAnswer, 'captcha');
        $user->setAttribute('num1', $num1, 'captcha');
        $user->setAttribute('num2', $num2, 'captcha');
      }
      else
      {
        // the form is submitted, retrieve nums from the session
        $num1       = $user->getAttribute('num1', null, 'captcha');
        $num2       = $user->getAttribute('num2', null, 'captcha');
        $realAnswer = $user->getAttribute('realAnswer', null, 'captcha');
      }
    
      $show = $num1 . " + " . $num2 . " = ";
    
      $this->getResponse()->setSlot("displayNum", $show);
    
      $this->form = new UserForm();
    
      if ($request->isMethod('post') && ($request->getPostParameter('captcha[answer]') == $realAnswer))
      {
        $this->processForm($request, $this->form);
      }
    }
    

    And don’t forget to empty the related values in session if the form is valid.

    protected function processForm(sfWebRequest $request, $form)
    {
      // bind form
    
      if ($form->isValid())
      {
        // clear the session
        $this->getUser()->getAttributeHolder()->removeNamespace('captcha');
      }
    }
    

    Ps: if you are looking for a transparent captcha, I recommend you this method. I’ve tested it with really great success.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am pulling two fields and putting them together with a -. When I
I have an INSERT query that is pulling data from two tables and inserting
I have two jobs pulling from a mysql table. They both want to get
How can one programmatically sort a union query when pulling data from two tables?
I have two functions that pulling some of content from html and returning it
I've been pulling my hair out for two days trying to put a MySQL
I've got two tables, Feeds and Import in an SQLite DB. I'm pulling all
after pulling my hair out for two days trying to figure this issue out
I have two threads. Thread A is pulling some elements from queue and thread
When pulling dates from a view using JPA and EclipseLink I'm getting dates two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.