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 8866195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:43:39+00:00 2026-06-14T16:43:39+00:00

I have PHP function inside a class that returns a math problem: public function

  • 0

I have PHP function inside a class that returns a math problem:

public function level1() {

    //level 1 and 2
    //single digit addition and subtraction
    //randomly choose addition or subtraction
    //1 = addtion, 2 - subtraction
    $opperand = rand( 1, 2 );

    //if the problem is a subtraction, the program will keep generating problems if a negative problem is generated
    //if opperand is a subtraction, do generate both numbers while the answer is negative

    if ( $opperand == 2 )
        {
        do {

            //randomly generate first number
            $number1 = rand( 1, 9 );

            //randomly generate second number
            $number2 = rand( 1, 9 );

            //compute the answer
            $answer = $number1 - $number2;

            //change variable to actual opperand
            $opperand = "-";
        } while ( $answer < 0 );
        }
    else
        {//addition problem
        //randomly generate first number
        $number1 = rand( 1, 9 );

        //randomly generate second number
        $number2 = rand( 1, 9 );

        //compute the answer

        $answer = $number1 + $number2;

        //change variable to actual opperand
        $opperand = "+";
        }//end if/else

    return array( $number1 . " " . $opperand . " " . $number2 . " ", $answer );

I call this function from ajaxHandler.php (Which I call from ajax)

   $problemData = $MathEngine->level1();
    return $problemData;

The php will always return an array, but I cannot figure out how to manipulate or even view the results as an array in javascript. Is there a way to do this? I’ve used the standard Get ajax call before so that’s not new. when I try to reference the ajax response text as an array, I either get nothing (when i click the button) or ‘undefined’

           var problemData = ajaxRequest.responseText;

           alert( problemData[0] )
  • 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-14T16:43:40+00:00Added an answer on June 14, 2026 at 4:43 pm
    // php - this will produce a json string
    echo json_encode(array( $number1 . " " . $opperand . " " . $number2 . " ", $answer ));
    
    // and in javascript - parse json string to javascript object
    var problemData = JSON.parse(ajaxRequest.responseText);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP function that extract dat of an invoice from DB. The
I have a php function which returns JSON in response to a ajax request
I have a PHP function that builds a JSON array via $jsonArray= array(); for
I have a PHP function that takes a variable number of arguments (using func_num_args()
i have a php function which returns a random json encoded color <?php function
I have a PHP function that requires can take 3 parameteres... I want to
I have a PHP function that I want to make available publically on the
Is it possible to have a PHP function that is both recursive and anonymous?
i have simple form that uploads image to server and returns php string containing
Let's assume that we have a PHP class Page , which generates PHP Page

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.