Jump to content

Confirmation Alert Box


scottybwoy

Recommended Posts

How would I go about making an alert box to the user and await their answer.  What I am trying to do is if the user enters a new customer in the search bar, it comes up with a message,  "Are you sure you want to add a new customer, (what they typed)" , "Yes/No".  I have it so that it brings up the alert echoing some javascript like so :
[code]
<?php
if (what they typed exists){
    do some stuff
} else {
  echo '<script language="javascript">confirm("Do you want to insert new customer ' . $company . '?")</script>';

  if (empty($_REQUEST['custId']))
  {
  $custId = $this->makeCustId($company);
  $data_array = array("company" => $company, "custId" => $custId);
  }

  if (isset($_POST['commit']) && $_POST['commit'] == "Commit")
  {
  $data_array = $_POST;
  $this->insertCust($data_array);
  }

$template = CUST_PAGE;
  }
?>
[/code]

But how would I get it to recognise the answer?  At the mo if I click Yes/No it will just enter the customer anyway.  Thanks in advance.
Link to comment
Share on other sites

php is preprocessed... meaning it will add anything/everything you tell it, before it goes to the browser. meaning a javascript confirm box is useless... if you want a prompt, you'd want something like this...

[code]
swicth($_GET[stage]){
case "1":
  #first code here
break;
case "2":
  #prompt
break;
case "3":
  #continue here
break;
}
[/code]
Link to comment
Share on other sites

Sorry I don't follow you.  Do you mean having differnt pages displayed for the confirmation?  I'm trying to make it as user friendly as poss, so was hoping to use some AJaX trick but was unsure how.  Am I asking in the wrong place?  Although I would like to understand your method a bit better if you could explain it further and how it would work. Thanks.
Link to comment
Share on other sites

This can be done like so:

add this to your page:

[code]
<script language="JavaScript">
function Confirmation() {
  var message = confirm("so you want to add customer");
  if(message) {
    return true;
  } else {
    return false;
  }
}
</script>
[/code]

and add this to your form tag:

[code]
<form...onsubmit="return Confirmation();">
[/code]
Link to comment
Share on other sites

yes... confirm boxes are done in javascript, but php is server side... by the time the user see's the confirm box, the php has already finished its work. if you split the code up onto seperate pages, then php can have a confirm box. now if you used ajax, you can do that, because its javascript loading php, however the confirm box would be at the front of your script,
[code]<script language="JavaScript">
function Confirmation() {
var message = confirm("so you want to add customer");
if(message){
  #ajaxcommand()
}
}
</script>[/code]
not in the middle. meaning you would want to break up your php anyways.
Link to comment
Share on other sites

  • 3 weeks later...
Sorry been preoccupied elsewhere with this one,  I tried the method above nut it still wont work.  Let me draw this out :
I have a header file with two search forms (1 input + 1 button each)  It is connected to an external JS file and a php file for some AJAX magic, so whaen user presses a letter it will bring up what ever is in the database (like google suggest) If a user selects the data and presses enter i want it to open up and post the details in the relevant form (It's doing that already).  But if a user types something else that not in the database I want it to bring up a conf box before just adding new data like it does at the mo.  Can it be done?  Is that easy to understand?  Thanks again
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.