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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:57:41+00:00 2026-06-17T08:57:41+00:00

I want to submit a form by php ajax and trying to keep things

  • 0

I want to submit a form by php ajax and trying to keep things simple, but they are inevitably complicated.

pageA.php is just a form.

pageB.php is validating and processing code. On top of this page I created $_SESSION['message'] = ""; and if something is wrong, at the end of page this could be $_SESSION['message'] = "Password is too short"; – for example.
This message I want to display on pageA.php – inside a div #message.

Sessions are started on top of both pages (session_start();
On pageA I also have a spinner gif, which is hidden by default (display:none;), but it should be visible during (and just during) form processing.

pageA.php

<form id="formReg" action="pageB.php" method="post">
. . .  
</form>
<img id="spinn" src="spinn.gif" />
<div id="message"></div>
<button id="sub">Register</button>

JS at the bottom of pageA:

$("#sub").click( function() {
    $("#spinn").show(); // it seems this works, but very shortly
    $.post( $("#formReg").attr("action"), $("#formReg").serialize()); // this works (data are stored in the table).
    $("#formReg :input").val(''); //this works
    $("#spinn").hide();
    $("#message").html($_Session["message"]); // doesn't work, of course.
});

At the bottom of pageB.php I have:
header("Location: pageA.php");, to keep visitors on pageA.

  • 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-17T08:57:42+00:00Added an answer on June 17, 2026 at 8:57 am

    First: All actions which should happen after the form submit must be placed in a callback function passed to $.post(). AJAX functions return immediately, therefore the spinner will be hidden shortly after it is shown. Do it like this:

    $.post(
        $("#formReg").attr("action"), $("#formReg").serialize(), 
        function(data, textStatus, jqXHR) {
            $("#spinn").hide();
            // Message display code - see below
        }
    );
    

    That way, the code in the callback function gets executed as soon as the post request returns, not after the $.post call returns (which merely starts the request, but doesn’t wait for it’s completion).

    Regarding displaying the message: Your pageB.php doesn’t have to redirect, as it is only invoked via AJAX. So the client’s browser will never leave pageA.php after opening it. The AJAX call is a separate HTTP request, which doesn’t affect the page currently open. To return the value, you have to simply output it on pageB – the data parameter of the callback function defined above will contain it. If you want to keep it simple, just do:

    // in pageB.php
    echo $_SESSION['message'];
    
    // In your JS callback
    function(data, textStatus, jqXHR) {
        // "data" contains the output of pageB.php
        $("#message").html(data);
    }
    

    If you need to pass more than one item to your AJAX callback, take a look at JSON.

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

Sidebar

Related Questions

I'm basically just trying to submit a form through AJAX and want to show
I want to submit a form like this: <input type=button onclick=$('#form1').submit(); value=Create /> but
I am very new to ajax concept,I want to submit a form without refresh
I'm trying to submit a form with AJAX using jquery: <form name=myform action=> <table
I am trying to create a form that utilizes PHP and Jquery AJAX form
on form submit i am making an AJAX request to PHP code, in response
I'm trying to add ajax form submission to a PHP web app I'm working
I've downloaded an ajax/jquery conatct form with a normal button. But I want to
I am using php/ajax to submit a form without page refresh. Here are my
I am trying to add a reCAPTCHA to my PHP contact form with AJAX.

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.