submit form to server side php script which reports results to alert popup box

Posted on 2013-12-01
Last Modified: 2013-12-03
I am successfully submitting a form to a server side php script which now displays the success or failure to a new page. I would like the php script to report the success or failure to a dialogue popup box over the form.

How can I do this? TIA
Question by:thenelson
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Expert Comment

ID: 39688698
Here's an existing answer:

[Link Deleted]
LVL 110

Expert Comment

by:Ray Paseur
ID: 39688733
Can you please post an example of a web page that does this?  I understand the part about making an AJAX request, but I want to see what you mean by "a dialogue popup box over the form."  Thanks, ~Ray
LVL 39

Author Comment

ID: 39689013
"a dialogue popup box over the form."
a javascript alert box
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
ID: 39689375
The easiest way to do this is with jQuery and jQueryUI. Link to both libraries and the CSS file in the head of you document, and then on the AJAX success, create a DIV, drop in the response your want and call the dialog() function.

Add this to the HEAD of you page:

<script src="//"></script>
<script src="//"></script>
<link rel="stylesheet" href="//" type="text/css" media="screen" charset="utf-8"/>

Open in new window

Your AJAX call would look something like this:

   url: 'yourScript.php'
.done(function(response) {
   $('<div>').html(response).dialog({modal: true});

Open in new window

LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
ID: 39689387
If you just want an alert box then don't call dialog() - just use the alert() function (assuming response is a simple string)

.done(function(response) {

Open in new window

LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
ID: 39689403
Just re-read your question. I'd assumed you were already making an AJAX call but maybe you're not.

Here's a little more detail. Assume your form has an ID of myForm. We can POST the form to your server-side script asynchronously (in the background). Your server-side script would then respond with some information, that you put into a dialog box for the user to see. The whole jQuery code for the head of your page would look like this:

<script type="text/javascript">
$(document).ready(function() {
   $('#myForm').submit(function(e) {
         url: $(this).attr('action'),
         type: 'post',
         data: $(this).serialize()
      .done(function(response) {
         $('<div>').html(response).dialog({modal: true});

Open in new window

LVL 34

Accepted Solution

Slick812 earned 250 total points
ID: 39693860
greetings  thenelson, , You may be new to trying to code a web page with an AJAX send-receive to a PHP server page, Although the JQuery does seem to make the javascript part of ajax a bit more simple, you will still need to know enough about how AJAX is several functional methods, not just one method, as you have to do browser JS code AND server PHP code!. I have done this AJAX form submit thing many times, and have CSS to have an "Over the Form" popup DIV, here is both the browser DHTML CSS JS code and the PHP code. You will not be able to use the same HTML return from the PHP server that you used for the - "submitting a form to a server side php script which now displays the success or failure to a new page", , because ib AJAX you do NOT return an entire HTML page, but return from AJAX  just enough info text (html) to fill a DIV on the page.
First here is the full page code for the HTML -
<!doctype html>
<html><head><title>jQuery Form Test AJAX</title>

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

border:5px solid #d00;

border:1px solid white;
<script src=""></script>
<body><h3 style="text-align:center;">Test AJAX Form Submit</h3>
<form name="formAj" id="formAjax" action="formnon.php" method="post">
Name: <input type="text" name="uname" size="16" value="" /> must be more than 4 characters<br />
Age: <input type="text" name="age" size="5" maxlength="3" value="" /> Years: must be older than 17 years<br />
<input type="submit" value=" Submit Me ">
<div id="popup"><div class="xBut" onclick="$('#popup').hide();">X</div><span id="ajaxRe">NONE YET</span></div>

$( document ).ajaxError(function( event, request, settings, exc ) {
alert("ERROR from Ajax POST Server-Status: "+request.status+", post-URL: "+settings.url+", error because: "+exc);

$('#formAjax').submit(function(e) {
  var postRe = $.post( "formjqy.php", $(this).serialize());
  postRe.done(function( data ) {

Open in new window

Inside the <form> I have a <div> with the id of "popup" with is hidden (display:none), if the AJAX is successful in submit the form inputs and gets a PHP response as JS data, then The popup us shown with  $('#popup').show();  
The popup is positioned over the Form, no matter where you place the Form on page. Click the red X button on popup, to hide the popup.  Please notice that I have all of the functional javascript at the very end of the page, I can move it to other page locations like in the <head> with  $(document).ready(function() {   but For me this the least javascript.

Next is the server PHP code -
$uname = $_POST['uname'];
$age = (int) $_POST['age'];

if ((strlen($uname) > 4) && ($age > 17))
  $ajaxReturn = '<b style="font-size:150%px;">SUCCESS! Your Submition is On Tract!</b><br />'.$uname;
  $ajaxReturn = '<b style="font-size:150%px;">FAILURE! Your Submition was Junk!</b><br />'.$uname;

echo $ajaxReturn;

Open in new window

which is as simple a code test for success as I might use.

If you need more info about Ajax , please ask questions
LVL 39

Author Closing Comment

ID: 39694276
Slick812, Chris,

You are right that I am not familiar with Ajax. I am taking a bit of time to read up on it to better understand its use before jumping into the coding.

I have a four section form (actually 4 forms on a single page which I plan to split to four separate pages later when I get the time). Instead of having a popup stating the form was submitted, I am now thinking of having the submit button change to a "Section X successfully submitted, Go on to section X+1" button. And keep track of the submissions with cookies so the user can come back and complete the form later if he wishes.

So I got a bit of studying and coding to do and will post another question if I need more help.


Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question