Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2415
  • Last Modified:

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

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
0
thenelson
Asked:
thenelson
4 Solutions
 
AdamSenior DeveloperCommented:
Here's an existing answer:

[Link Deleted]
0
 
Ray PaseurCommented:
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
0
 
thenelsonAuthor Commented:
"a dialogue popup box over the form."
a javascript alert box
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Chris StanyonCommented:
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:

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

Open in new window

0
 
Chris StanyonCommented:
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) {
   alert(response);
});

Open in new window

0
 
Chris StanyonCommented:
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) {
      e.preventDefault();
      $.ajax({
         url: $(this).attr('action'),
         type: 'post',
         data: $(this).serialize()
      }) 
      .done(function(response) {
         $('<div>').html(response).dialog({modal: true});
      });
   });
});
</script>

Open in new window

0
 
Slick812Commented:
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>
<style>
body{background:#e3f7ff;}

#formAjax{
position:relative;
width:25em;
background:#fbdba0;
margin:12px auto;
border:3px solid #5ad;
padding:9px;
}

#popup{
position:absolute;
top:4px;
left:2px;
display:none;
width:24.5em;
height:3.5em;
background:#ffbaaa;
border:5px solid #d00;
padding:6px;
}

.xBut{
position:relative;
top:-5px;
right:-5px;
width:1em;
height:1em;
font-weight:bold;
float:right;
line-height:98%;
cursor:pointer;
font-family:arial,sans-serif;
background:red;
color:white;
text-align:center;
border:1px solid white;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body><h3 style="text-align:center;">Test AJAX Form Submit</h3>
<hr>
<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>
</form>

<script>
$( 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) {
  e.preventDefault();
  var postRe = $.post( "formjqy.php", $(this).serialize());
  postRe.done(function( data ) {
  $('#ajaxRe').html(data);
  $('#popup').show();
  });
});
</script>
</body></html>

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 -
<?php
$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;
  else
  $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
0
 
thenelsonAuthor Commented:
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.

Thanks!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now