ukscotth Posted September 29, 2011 Share Posted September 29, 2011 Hi, I'm trying to work out how I can trigger a javascript popup type box in an if statement, to trigger it normally from a button it would be : <input type="button" value="Display" onclick="javascript: formFunction();" class="submit" /> But I want to use it when people submit a form without filling in some of the fields so something like this : if (($_POST['first_name'] == '')) { run javascript popup } Any ideas ? Many thanks in advance. Scott Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/ Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 It aint pretty but it does what you want. if (($_POST['first_name'] == '')) { echo '<script type="text/javascript"> alert(\'this is an alert box\');</script>'; } Wouldnt it be better to verify the data using javascript before it gets posted to the server and then floods the user with incomplete field popups? Just a thought. Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273956 Share on other sites More sharing options...
ukscotth Posted September 29, 2011 Author Share Posted September 29, 2011 Thanks, Unfortunatly the person im doing the site for wants a facebook style popup for some strange reason Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273960 Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 You can replace the alert box that I wrote with any other javascript function. Facebook style popups will require HTML etc inside them so it should be left till the end of script execution.. AKA.. store all the errors (if any) when the page is loaded make your pretty little popup.. From a user stand point, its less intrusive. Example: if (isset($_POST['submitted'])) { $errors = array(); // do all your error checking } Bottom of the page: <?php if (isset($errors) && !empty($errors)) { ?> <script type="text/javscript"> javascriptErrorHandler('<?php echo join(',',$errors); ?>'); </script> <?php } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273963 Share on other sites More sharing options...
ukscotth Posted September 29, 2011 Author Share Posted September 29, 2011 Thanks il give it a try. Sorry for being a newbie but what would I put in the : // do all your error checking All it needs to check is 2 fields Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273967 Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 Check that the field is set, that it isnt empty and any other error checking that the field may require. (ie, has to be a number, can only be alpha, is an email etc etc) Where is the data going once it has been submitted? Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273969 Share on other sites More sharing options...
ukscotth Posted September 29, 2011 Author Share Posted September 29, 2011 Ok so i put something like this ? if (isset($_POST['submitted'])) { $errors = array(); if (($_POST['from'] == "") || ($_POST['to'] == "")){ } The data will be sent to another page as example.php?from=example&to=example Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273970 Share on other sites More sharing options...
ukscotth Posted September 29, 2011 Author Share Posted September 29, 2011 ok hes just told me that a normal alert box is fine but when i do this it messes up the styling on the page if(isset($_POST['submit'])){ if (($_POST['from'] == "") || ($_POST['to'] == "")){ }else{ header( 'Location:quote_part1.php?from='.$_POST['from'].'&to='.$_POST['to'] ) ; } } any ideas why ? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273974 Share on other sites More sharing options...
ukscotth Posted September 29, 2011 Author Share Posted September 29, 2011 sorry i meant to put if(isset($_POST['submit'])){ if (($_POST['from'] == "") || ($_POST['to'] == "")){ ?> <script type="text/javascript"> window.alert("You message goes here!") </script> <?php }else{ header( 'Location:quote_part1.php?from='.$_POST['from'].'&to='.$_POST['to'] ) ; } } Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273976 Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 Where are you putting this header call? After there has been code output to the browser? Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273977 Share on other sites More sharing options...
ukscotth Posted September 29, 2011 Author Share Posted September 29, 2011 nope at the very top Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273983 Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 If the style has broken (in my experience), it usually means text/an error has been displayed at the top of the page. Can you show me the code from line 1 until the end of this bit of script. Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273987 Share on other sites More sharing options...
ukscotth Posted September 29, 2011 Author Share Posted September 29, 2011 Problem solved. I used this code to check the fields before they were submitted <script language="javascript" type="text/javascript"> function checker() { var myForm = this.document.myForm; if(myForm.from.value == '') { alert("Please Select the pick up area"); myForm.from.focus(); return false; } if(myForm.to.value == '') { alert("Please Select the droping area"); myForm.to.focus(); return false; } if(myForm.from.value == myForm.to.value) { alert("Pick up area and droping area should not be same"); myForm.to.focus(); return false; } } </script> Thanks alot for your help, its most appreciated Quote Link to comment https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/#findComment-1273989 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.