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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:14:39+00:00 2026-05-28T03:14:39+00:00

I have the following form and javascript function on my web page. This is

  • 0

I have the following form and javascript function on my web page. This is a dummy function that I am using to test whether what I would like to do is possible.

What I am attempting to do is have a form send an AJAX request to the server, so that the server can update the database while the page itself continues along it’s predetermined path. I am in a tight time crunch, so I unfortunately do not have time to rewrite the entire page to better support this. The problem that I have is the xmlhttp object does not seem to return properly. I get a readyState of 4 but a status of 0. can someone please explain what I need to do?

Here’s my code:

ajax.php

<html>
<head>
<script type="text/javascript">
function test(){
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange=function(){
        document.getElementById("new").innerHTML+="<b>"+xmlhttp.readyState+"</b> "+xmlhttp.status;

        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("hello").innerHTML=xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET","response.php",true);
    xmlhttp.send();
}
</script>
</head>

<body>
<?php
if ($_POST){
  echo $_POST['hello'];
}
?>

<form action="ajax.php" method="post">
<input type="text" name="hello" />
<input type="submit" value="submit" onclick="test();" />
</form>

<div id="hello"></div>
<h3>debug</h3>
<div id="new"></div>
</body>
</html>

response.php

<?php
echo "Hello there";
?>

EDIT

Please note that I do not want to prevent the default behavior. In fact, the forn must be submitted as usual. I simply want to add an AJAX request to the process.

  • 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-28T03:14:40+00:00Added an answer on May 28, 2026 at 3:14 am

    You can’t trigger an AJAX request and allow the form to submit at the same time. Incomplete AJAX requests are cancelled when the page reloads (as is the case when the form is submitted). You’ll either have to not submit the form at all, or wait until your AJAX call has completed before submitting the form. If you wanted to go the second route, you could make the following changes to your code:

    <html>
    <head>
    <script type="text/javascript">
    function test(){
        var xmlhttp = new XMLHttpRequest();
    
        xmlhttp.onreadystatechange=function(){
            document.getElementById("new").innerHTML+="<b>"+xmlhttp.readyState+"</b> "+xmlhttp.status;
    
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementById("hello").innerHTML=xmlhttp.responseText;
                **document.getElementById("ajaxform").submit();**
            }
        }
    
        xmlhttp.open("GET","response.php",true);
        xmlhttp.send();
    }
    </script>
    </head>
    
    <body>
    <?php
    if ($_POST){
      echo $_POST['hello'];
    }
    ?>
    
    <form action="ajax.php" method="post" **id="ajaxform"**>
    <input type="text" name="hello" />
    <input type="submit" value="submit" onclick="test();**return false;**" />
    </form>
    
    <div id="hello"></div>
    <h3>debug</h3>
    <div id="new"></div>
    </body>
    </html>
    

    Changes/additions are marked with **.

    Note that there are a few practices in there I don’t like, in particular using the onsubmit, etc attributes of HTML tags to attach Javascript event handlers.

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

Sidebar

Related Questions

I have a page, test.php, with the following code: <html> <body> <form> <script type=text/javascript>
I am doing some basic form validation. I have the following JavaScript function: function
I'm new to web programming and I have the following form that I wish
I have a web form that contains lots of JavaScript functionalities to manipulate the
I have the following Javascript code: function checkIfValid(){ var form = document.createuserform; var valid
I have the following Javascript code: function checkIfValid(){ var form = document.createuserform; if(form.fName.value ==
I have a javascript function that builds an URI fragment using some captured parameters,
I have the following HTML/JS/jQuery Code. This code represents a login form that is
i have the following javascript to post a form through ajax without refreshing the
I have the following form <form name=myForm id=myForm method=post enctype=multipart/form-data action=script.php> and this jQuery

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.