Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jackie Jen
Jackie Jen
2,723 Points

How to create a confirmation box in php

Hi

For my coding I have 3 checkbox1,2&3. by default this 3 checkbox is checked. If user accidentally unchecked one of the checkbox and click the submit button.

I would like to have an confirmation message box to ask user whether would like to proceed or return. if click return will stay back on original page. below i have create a button but did not show up a message box. please advice.

<input type="button" value="Rate Request" onclick="return comfirm('Are you sure want to delete');" />

Best Regards

3 Answers

I think.. to do a confirmation box in php isn't possible without javascript but I could be wrong.

But you could create php code which will write javascript code to the page when a validation fails.

When you submit your form, i'm guessing it goes to a php processing file or back to the same page with a conditional to run code if the SERVER_REQUEST == POST.

It's here that you run your validations.

(I'm really sorry if you know all this already btw!)

This scenario works easily if you validate on the same page as the form - i.e. <form action="same page">.

So you're expecting $_POST['confirm'] to be true/1/ticked. All you have to do is say if $_POST['confirm'] is not true/1/ticked, print javascript popup code to the page.

<?php
$alert_script;
$error;

//Put this POST block right at the top of the page.
if ($_POST['confirm'] !== 1) {
  $alert_script =  "<script>alert(\'You have to tick the box\')</script>";
  $error = true;
} else {
  $error = false;
}
if (!$error) {
  //complete the action code/call another file.
}
?>
//Then wherever you're running document ready or general page scripts, you'll want to echo $alert_script

//scripts
<?php
  if (isset($alert_script) {
     echo $alert_script;
  }
?>

At the same time, you want to stop any additional code executing until the user selects the confirm box. You might add a variable $error = true if a validation fails and only allow the success functions to run if (!$error) { Success Function (i.e. writing to database or sending email) }

Validation messages via javascript might be considered obtrusive - if the user has js turned off, how are they going to know what the error is? They would also be able to submit the form without that particular validation.

Instead of creating an alert, you could create an error message and a box for error messages. Write a conditional to say if an error message exists, echo this message box and echo the error message inside.

I hope this helps!

Navid Mirzaie Milani
Navid Mirzaie Milani
6,274 Points
<input type="button" name="confirm" id="confirm"  onClick="confirm('do you really want this')" value="click">
Jackie Jen
Jackie Jen
2,723 Points

Hi Thomas,

I have been study javascript this few days. You are correct, in order to make a msg box to pop out it need javascript to do it. by the way javascript perform on client side to do some validation. Thanks for giving such useful info

No worries, I'm glad it helped! You're absolutely right, javascript can do client side validations :) However, I have to stress again that it is good practice to validate via php (server side) to reduce security risks. Validations in javascript can be bypassed if the user has javascript turned off.