scottybwoy Posted January 4, 2007 Share Posted January 4, 2007 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]<?phpif (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. Quote Link to comment Share on other sites More sharing options...
taith Posted January 4, 2007 Share Posted January 4, 2007 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] Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted January 5, 2007 Author Share Posted January 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
matto Posted January 5, 2007 Share Posted January 5, 2007 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] Quote Link to comment Share on other sites More sharing options...
taith Posted January 5, 2007 Share Posted January 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
matto Posted January 5, 2007 Share Posted January 5, 2007 using the javascript above would prevent the php code from running as it's processed on the client side - basically stop the form from being submitted. :) Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted January 25, 2007 Author Share Posted January 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted January 30, 2007 Author Share Posted January 30, 2007 Any Ideas, probe Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted January 30, 2007 Author Share Posted January 30, 2007 Can this even be done, i can't find much on the web? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.