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

Posted on 2013-12-01
Medium Priority
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 111

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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 1000 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="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/black-tie/jquery-ui.min.css" 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 1000 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 1000 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 1000 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="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

770 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