pwes24 Posted December 7, 2007 Share Posted December 7, 2007 Hello everyone, How do you prevent the php script from executing the first time the a page is loaded into a browser? I was thinking putting code between the <head> tags but it has not worked. Please help. Thank you. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 7, 2007 Share Posted December 7, 2007 Why would you want to do that? What are you doing? Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted December 7, 2007 Share Posted December 7, 2007 And where's the error message? Quote Link to comment Share on other sites More sharing options...
pwes24 Posted December 7, 2007 Author Share Posted December 7, 2007 Let's say i want convert km to miles. so the first thing is to check user input. if its not there an echo error like "enter some number" shows up. The problem is that this error shows up the first time the page loads. How can i prevent it from doing so? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 7, 2007 Share Posted December 7, 2007 Let's say i want convert km to miles. so the first thing is to check user input. if its not there an echo error like "enter some number" shows up. The problem is that this error shows up the first time the page loads. How can i prevent it from doing so? You need to do a CHECK first to see if they requested the data. I assume you have a submit button, we will call that "submit". So you would just check if they pressed the button <?php if (isset($_POST['submit'])){ //now put all the code here } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 7, 2007 Share Posted December 7, 2007 You need to add some logic. Assuming you are using a form to gather the data, at the top of the page you need to first check if the form has been submitted. if (!$_POST['submit']) { do your form stuff here } else { do your php here } Quote Link to comment Share on other sites More sharing options...
pwes24 Posted December 7, 2007 Author Share Posted December 7, 2007 What am I doing wrong here? <body> <?php if (isset($_POST['submit'])){ $food=$_GET[food]; if (!empty($food)) { echo "The foods selected are:<br />"; foreach($food as $foodstuff) { echo $foodstuff.", "; } else { echo('check a box please'); } } } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <fieldset> <label>Italian <input name="food[]" type="checkbox" value="Italian" checked /></label> <label>Mexican <input name="food[]" type="checkbox" value="Mexican" /></label> <label>Chenese <input name="food[]" type="checkbox" value="Chenese" /></label> </fieldset> <input type="submit" value="submit" name="submit"> </body> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 7, 2007 Share Posted December 7, 2007 Is $food empty? You need to $_GET['food']; You're missing the quotes. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 7, 2007 Share Posted December 7, 2007 Also, you can't do this with HTML <label>Italian <input name="food[]" type="checkbox" value="Italian" checked /></label> food[] won't work like that. 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.