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

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.");
	//you've got someone with the same first and last name, check to see if the family_id select box was utilitzed
		//they chose a last name, so that means they're getting ready to duplicate somebody

        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

brucegustPHP DeveloperAsked:
Who is Participating?
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.

Ray PaseurCommented:
JavaScript has confirm()
add this in your form :

<form onsubmit="return check()"

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

Open in new window

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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ray PaseurCommented:
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.

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
brucegustPHP DeveloperAuthor Commented:
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.

Ray PaseurCommented:
I think you'll get better results faster that way!
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.