Solved

How can I use an alert box that allows user to either continue or kill the form submission?

Posted on 2014-09-10
6
766 Views
Last Modified: 2014-09-10
Here's my PHP:

$amber = "select id from students where first_name='$first_name' and last_name='$last_name'";
$amber_query=mysqli_query($cxn, $amber)
or die("Amber didn't make it.");
$amber_count=mysqli_num_rows($amber_query);
	if($amber_count>0)
	{
	//you've got someone with the same first and last name, check to see if the family_id select box was utilitzed
		if(isset($_POST['family_id'])&&trim($_POST['family_id'])<>"")
		//they chose a last name, so that means they're getting ready to duplicate somebody
		{
		header("Location:student_duplicate.php");
		exit();
		}

        RIGHT HERE - I want to put a Javascript alert box that tells the user they're getting ready to potentially add a duplicate entry. It may not be, so I want to give the user to either continue with the submission, or kill it. How?


	}

Open in new window

0
Comment
Question by:brucegust
6 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 275 total points
ID: 40314968
JavaScript has confirm()
http://www.w3schools.com/js/js_popup.asp
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 125 total points
ID: 40314973
add this in your form :

<form onsubmit="return check()"

with the following javascript :
<script>
function check() {
   return confirm("do you want to continue?");
}
</script>

Open in new window

0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 100 total points
ID: 40314978
For that to work you would need to go back to the browser, you cannot execute javascript in a PHP page and then continue with the PHP
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 275 total points
ID: 40314991
But here's the issue... PHP is a server-side script.  It runs for the purpose of preparing HTML, CSS and JavaScript.  These three things are client-side scripts.  When you visit a web page you make a request to the server.  The server prepares a response consisting of HTML, CSS and JavaScript.  It sends that response, then it disconnects from the client machine.  This is called a "stateless" protocol because the server no longer knows anything about the client.  Only after the server has run and produced the response document does the client machine start its work.

As a result of this, you may have to think about using AJAX if you want to change the natural order of HTTP client/server processes.  The JavaScript Alert/Confirm is possible, but it's a lot of work since you're going to have to do this sort of thing:

1. Make an AJAX request to test the "amber" query
2. Get the AJAX response and use alert/confirm as needed
3. Take the (human) client response and verify whether to update anyway
4. If the confirm is "no" skip the update
5. If the confirm is "yes" make an AJAX request to force the update.

I think I might omit the JavaScript from the design and just use regular HTTP page loads.  You can keep the intermediate data in the session between scripts.
0
 

Author Comment

by:brucegust
ID: 40315060
Ray, I think I'm going to go with your wisdom, although the other suggestions are things that I'll stash away for other situations.

The problem is that the alert, while it would normally be cut and dry, has to have the ability to communicate with the server like you pointed out. Hence, I'm going to punt this approach and go with something else.

Thanks!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40315104
I think you'll get better results faster that way!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now