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

  • SEARCH
  • Home
  • 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 6634513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:56:19+00:00 2026-05-25T22:56:19+00:00

I am trying my hand on PHP. And I’ve landed at the client-server connectivity.

  • 0

I am trying my hand on PHP. And I’ve landed at the client-server connectivity.

Now I have a server-side script that applies random math to an x and a y value that it gets via an XMLHttpRequest:

<?php
    $farr = array(
        create_function('$x,$y', 'return "x + y = ".($x+$y);'),
        create_function('$x,$y', 'return "x - y = ".($x-$y);'),
        create_function('$x,$y', 'return "x * y = ".($x*$y);'),
        create_function('$x,$y', '$returnVal = "x / y = "; if($y==0){$returnVal.="NaN (y value = 0)";}else{$returnVal.=($x/$y);}return $returnVal;')        
    );

    $x = $_GET["x"];
    $y = $_GET["y"];
    $i = rand(0,3);
    try{
        $f = $farr[$i];
        $result = $f($x,$y);
        echo "".$result;
    } catch(Exception $e) {
        echo "An exception occurred while accessing functions";
    }
?>

The client calls this script when a user presses the “Send” button on my form:

<html>
    <head>
        <title>
            Welcome
        </title>
        <script type="text/javascript">
            function calculate(x, y){
                var xmlhttp;
                if( x.length==0){
                    document.getElementById("result").innerHTML = "x is not set";
                    return;
                }
                if(y.length==0){
                    document.getElementById("result").innerHTML = "y is not set";
                    return;
                }
                if(window.XMLHttpRequest){
                    xmlhttp = new XMLHttpRequest();
                } else {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function(){
                    if(xmlhttp.readyState==4 && xmlhttp.status==200){
                        document.getElementById("result").innerHTML = xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET", "math.php?x="+x+"&y="+y, true);
                xmlhttp.send(null);
            }
        </script>
    </head>
    <body>
        <form>
            x: <input type="text" name="x"/><br/>
            y: <input type="text" name="y"/><br/>
            <input type="submit" value="Send" onclick="calculate(x.value, y.value)"/> <input type="reset" value="Reset"/>
            <p>Result: <div id="result">something</div></p>
        </form>
    </body>
</html>

It is intended that the result is placed in the div at the end of the form. However, I see the result for a brief amount of time, and then it’s gone again. I suspect the page from reloading, but isn’t that the purpose of the XMLHttpRequest, that only the part in the onreadystatechanged callback get’s refreshed? And not the whole page?

  • 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-05-25T22:56:20+00:00Added an answer on May 25, 2026 at 10:56 pm

    that’s because you are using a submit button.. change

    <input type="submit" to <input type="button"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying my hand at OO PHP, and currently have three files. I
I am trying to create a php script that will retrieve the WOW factor
I'm trying to place this menu on the left hand side of the page:
I'm trying to make a function that will take short hand hex color to
I have a php script, which downloads, rescales and stores an image using the
I have some PHP source code that I'm hosting with hosting company XYZ. I'm
Trying to sort an array in PHP that is being populated from a CSV.
I'm trying my new hand at Code Igniter and have this issue come up.
Here is what I am trying to achieve in PHP: I have this string:
I'm trying my hand at google appengine and using the datastore with php and

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.