How to Populate the SAME Html Form using php, js, jquery, and ajax Without Refreshing the Screen

I have a simple form for a club that I have created containing 4 fields at  The form opens blank.  When the submit button is clicked, it should go to the actionTest.php and populate (fill) the fields of the same form without refreshing the screen and changing the structure in anyway.  It should also stay on the same test.php form but call the routine on the background.  

The problem is that it is going to the actionTest.php and the structure is changing.

How can I get it to populate (fill) the fields of the same form?
Vanavah EdwardsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It has to be done via ajax method.
You have used plain php form . So how you expect that it would work as you wish.

will help you to get reference
Ray PaseurCommented:
It's helpful to post your code samples in the CODE snippet so we can all see them inline, instead of having to open separate attachments.

Here is "test.php"

$email 		= "";  // set $email
$surname 	= "";  // set $surname 
$firstname 	= "";  // set $firstname 
$middlename = "";  // set $middlename


<form action="actionTest.php" method="post">
  <label>Email</label> <input name="email" value="<?php echo $email;?>">
  <label>Surname</label> <input name="surname" value="<?php echo $surname;?>">
  <label>First Name</label> <input name="firstname" value="<?php echo $firstname;?>">
  <label>Middle Name</label> <input name="Middle" value="<?php echo $middlename;?>">
   <button type="submit">Submit</button>


Open in new window

Here is "actionTest.php"

$email 		= "";  // set $email
$surname 	= "";  // set $surname 
$firstname 	= "";  // set $firstname 
$middlename = "";  // set $middlename

if (isset($_POST["email"])){
    $email 		= $_POST["email"]; // set $email to posted field named email
    $surname 		= $_POST["surname"]; // set $surname to posted field named surname
    $firstname 	= $_POST["firstname"]; // set $firstname to posted field named firstname
    $middlename 	= $_POST["middlename"]; // set $middlename to posted field named middlename
	$surname = "Brown";
	$firstname = "John";
	$middlename = "Ossie-Moore";


<form action="" method="post">
	<input name="email" value="<?php echo $email;?>">
	<input name="surname" value="<?php echo $surname;?>">
	<input name="firstname" value="<?php echo $firstname;?>">
	<input name="Middle" value="<?php echo $middlename;?>">

Open in new window

Ray PaseurCommented:
It looks like you need to get a foundation in how web applications work.  Hopefully these links can help you get that knowledge.

Client-Server protocols are the underpinning of all WWW applications.  This is key to understanding how AJAX requests work.

jQuery is one of the easiest ways to make a request without the appearance of a page load.  This article shows the essential moving parts.  The "success" function can update the form fields via jQuery.  jQuery uses selectors to locate "id" attributes and can get / set the contents of the selected page elements.

If you're new to PHP, please take a little time to learn the way PHP works.  You can skip over the parts you already know, but it's worth getting a foundation in the syntax and basic design patterns.

As time permits. I'll try to set up a code example that shows you how this sort of thing might be done.  I'll post back later after I've had a chance to write and test the code.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

greetings vanavah, , I did some code to do as you said, to use the actionTest.php to ajax Without Refreshing the Screen. But It was not as easy as I thought it would be.

this is the code for an .html web page. There is NO PHP in it, as it is not needed as far as I can see -
<!doctype html>
<html><head><title>JQuery AJAX Form Submit</title>

margin:2px auto;
border:3px solid #5ad;

#form1 label{
font-weight: bold;
color: #811;

<script src=""></script>

<script>  // I use 2.2.4 of Jquery

$(function() {
  $("#form1").submit(function(e) {
  var ajxSend = {
    type: "POST",
    url: "actionTest.php",
    data: $(this).serialize(),
    dataType: "json",
    timeout: 3500
  var aj = $.ajax(ajxSend);
  aj.done(function( received ) {
// IMPORTANT, you need to understand what should be SENT and RECEIVED by ajax
    if(received.error) { // test for error response
    alert("ERROR="+received.error+"- php value check Invlaid, "+received.message);
    return; // end ALL JS code

  $.each( received, function(key, val) {
    $( "input[name='"+key+"']" ).val(val);
  $("#title1").html("AJAX SUCCESS as SUBMIT for email= ";

  });, error1, err) {// the Status IS NOT a Number, but the Type of jquery ERROR
  if (xhr.status==404) alert("ERROR from Ajax as '404 status' the "+ajxSend.url+" page was NOT on Server, \nCan NOT recover from this ERROR, This operation is NOT available!");
  else {
  alert("Ajax ERROR = \""+error1+"\", with server Status: "+xhr.status+", post-URL: "+ajxSend.url+", \npost-Data: "", \nerror because: "+err);
  if (aj.responseText) {
    console.log("AJAX TEXT= "+aj.responseText);
    } else console.log("NO AJAX TEXT RECEIVED");

<body><h3 id="title1" style="text-align:center;">JQuery AJAX Form Submit</h3>

<form id="form1" action="actionTest.php" method="post">
  <label>Email</label> <input name="email" value="">
  <label>Surname</label> <input name="surname" value="">
  <label>First Name</label> <input name="firstname" value="">
  <label>Middle Name</label> <input name="Middle" value="">
   <button type="submit">Submit</button>



Open in new window

next is the code in the actionTest.php page
ini_set('display_errors', 1);
header('Cache-Control: no-cache');
// Change the Type to text/plain
header('Content-Type: text/plain');

if (count($_POST) < 1) {
  echo json_encode( array('error' => 'NO POST', 'message' => 'ERROR: The POST array was EMPTY') );

$errorMsg = '';
$outArray = array();
foreach($_POST as $key => $val) {
if (!$val) {
  $errorMsg .= $key.' needs to be filled, ';
$outArray[$key] = $val;

if ($errorMsg) {
  $outArray =  array('error' => 'EMPTY FIELDS', 'message' => $errorMsg);
  } else { // change the fields
  $outArray['surname'] .= " Brown";
  $outArray['firstname'] .= " John";
  $outArray['Middle'] .= " Ossie-Moore";

echo json_encode($outArray);

Open in new window

There to much there for me to try and explain all of what I did, In the .html page I use the JQuery  $.ajax( ) to send the <form> input values to the actionTest.php  page. . I really could not understand what you were doing in your actionTest.php code, you seemed to want to change the person's name inputs, , but I do not know why you do this? ?

This code works for me,

 so ask questions if you need explanations for how this may do it's work.
Ray PaseurCommented:
Here is an example that should help illuminate some of the important principles.  Since this is a two-part application we will need to look at the client / server scripts separately to see how they interact.  First, let's look at the server-side script.  It's pretty simple.  It expects a GET-method request and returns a JSON response.  The request will contain two fields named "dyn" and "stat."  The response object will return two fields named "dyn" and "stat."  The script will put the current timestamp into they dynamic field ("dyn") and will simply copy whatever was in the request["stat"] into the response->stat element.
<?php // demo/temp_vanavah_server.php

$response = new StdClass;

$response->dyn = time();

$response->stat = $_GET['stat'];

echo json_encode($response);

Open in new window

Next let's look at the client-side of the process.  When the #clickable element is clicked, our function will create two variables, "ddata" and "sdata."  These variables will contain the values of the input controls from "#dynamic" and "#static" respectively.  We will use these values to create a GET request, passing the values as arguments.  When the server-side script responds to the request, we will receive a JSON string in the "response" variable.  We then use JSON.parse() to enable our JavaScript to extract the components from the response.  We assign these string values to the appropriate elements in the HTML.
<!DOCTYPE html>
<!-- -->
<html dir="ltr" lang="en-US">
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<style type="text/css">
#clickable {

<script src=""></script>
        var ddata = $("#dynamic").val();
        var sdata = $("#static").val();
        $.get("temp_vanavah_server.php", {dyn:ddata,stat:sdata}, function(response){
            var resp = JSON.parse(response);

<title>HTML5 Page With jQuery in UTF-8 Encoding</title>

<noscript>Your browsing experience will be much better with JavaScript enabled!</noscript>

<input id="dynamic"   name="dyn"  value="Dynamic Field" /><br>
<input id="static"    name="stat" value="Static Field" /><br>
<input id="clickable"             value="Fill in some form data and click here" />


Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vanavah EdwardsAuthor Commented:
Please excuse my behavior by not responding sooner.  This was because my daughter got married over the weekend.  I will respond shortly.
Ray PaseurCommented:
Ray PaseurCommented:
E-E sent me an email message asking me to close this question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.