yakoup46 Posted April 16, 2010 Share Posted April 16, 2010 So I am trying to use an alert box. I am using... echo "<script>alert('Sorry Wrong!')</script>"; However, it brings up the alert and then sits on a blank page, which is obviously my PHP file. I want the alert box to open in the same page and not go the actual PHP file. Quote Link to comment https://forums.phpfreaks.com/topic/198750-alert-box-need-help/ Share on other sites More sharing options...
ChemicalBliss Posted April 16, 2010 Share Posted April 16, 2010 err what? Whats pages are you on about we know nothing. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/198750-alert-box-need-help/#findComment-1043070 Share on other sites More sharing options...
yakoup46 Posted April 16, 2010 Author Share Posted April 16, 2010 I basically want an alert box using PHP, but on the same page as the submit button. Ya know what I mean? Quote Link to comment https://forums.phpfreaks.com/topic/198750-alert-box-need-help/#findComment-1043073 Share on other sites More sharing options...
JonnoTheDev Posted April 16, 2010 Share Posted April 16, 2010 Why are you using PHP to display Javascript. PHP is server side so it will process the code prior to it getting to the user. Javascript should be client side so use an event handler i.e <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>test</title> <script language="javascript"> function doSomething() { alert('did you see what I did'); } </script> </head> <body> <a href="#" onClick="doSomething()">Click Me</a> </body> </html> If you are using a form set the action to run your javascript validation function. Quote Link to comment https://forums.phpfreaks.com/topic/198750-alert-box-need-help/#findComment-1043075 Share on other sites More sharing options...
yakoup46 Posted April 16, 2010 Author Share Posted April 16, 2010 Yeah I know how to do that but I want the display box on the same page. So say I am at something.com, I hit sumbit, then what I want to happen is for a alert box to come up on the same page and stay on that page after I close it. I am using PHP to validate the forms so I am kinda forced to use Javascript in my PHP. Quote Link to comment https://forums.phpfreaks.com/topic/198750-alert-box-need-help/#findComment-1043082 Share on other sites More sharing options...
ChemicalBliss Posted April 16, 2010 Share Posted April 16, 2010 What pages man? You want help, but we dont know why you want it because you already have a php page as you say with the form on there why not just add the alert on it? At least myself - am very confused with your question. Example.php <?php // some code ?> <form action="test.php" method="post"> <input type="submit" value="HIT ME!" onClick="Javascript:Alert('OMG I GOT HIT');"> </form> Thats all i can think your asking for right now? -cb- Quote Link to comment https://forums.phpfreaks.com/topic/198750-alert-box-need-help/#findComment-1043085 Share on other sites More sharing options...
JonnoTheDev Posted April 16, 2010 Share Posted April 16, 2010 If you are submitting the form to the same page then it will remain on that page unless you are submitting to a different file or using a header to redirect. You should not use javascript to display errors when you are using a server side script to validate, you will just see a blank screen with an alert box, the user will click ok and the page will be outputted. A simple bit of text near the input box where the user made the error is much better. If you use Javascript to display errors then you should use Javascript to validate the form! Although you should still have server side validation as users can simply switch javascript off. Quote Link to comment https://forums.phpfreaks.com/topic/198750-alert-box-need-help/#findComment-1043088 Share on other sites More sharing options...
yakoup46 Posted April 16, 2010 Author Share Posted April 16, 2010 Okay so how do I display this text near the input box? Quote Link to comment https://forums.phpfreaks.com/topic/198750-alert-box-need-help/#findComment-1043090 Share on other sites More sharing options...
JonnoTheDev Posted April 16, 2010 Share Posted April 16, 2010 Save the following as test.php and run on your server: <?php // process form $errors = array(); if(isset($_POST['submit']) && $_POST['submit'] == 'go') { if(!strlen(trim($_POST['name']))) { $errors['name'] = true; } if(!count($errors)) { // no errors } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form methoid="post" action="test.php"> <?php if($errors['name']) print "<p>Please enter your name</p>"; ?> Name: <input type="text" name="name" size="50" maxlength="50" /> <input type="submit" name="submit" value="go" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/198750-alert-box-need-help/#findComment-1043095 Share on other sites More sharing options...
ChemicalBliss Posted April 16, 2010 Share Posted April 16, 2010 On the php form. Put if conditions where you want the notes to be displayed, then something like: Form.php <?php // Check if form has been submitted: if(isset($_POST['submit'])){ .. Validate etc if(isset($_GET['name'] && strlen($_GET['name']) > 3)){ .. All ok.. }else{ // Something in form is wrong with name.. $wrong_name = "Your Name is too short"; } } // Your Form ?> <input type="text" name="name" value="<?php echo($_GET['name']); ?>" /> <?php // Echo what was wrong (above, it will keep the same value the user entered on the form sop they dont have to enter it again. if(isset($wrong_name)){ echo($wrong_name); } ?> More advanced santization systems can be made but this is a simple method that works per element. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/198750-alert-box-need-help/#findComment-1043098 Share on other sites More sharing options...
yakoup46 Posted April 16, 2010 Author Share Posted April 16, 2010 Thanx for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/198750-alert-box-need-help/#findComment-1043112 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.