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. Link to comment https://forums.phpfreaks.com/topic/80653-error-message/ 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? Link to comment https://forums.phpfreaks.com/topic/80653-error-message/#findComment-409004 Share on other sites More sharing options...
therealwesfoster Posted December 7, 2007 Share Posted December 7, 2007 And where's the error message? Link to comment https://forums.phpfreaks.com/topic/80653-error-message/#findComment-409006 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? Link to comment https://forums.phpfreaks.com/topic/80653-error-message/#findComment-409032 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 } ?> Link to comment https://forums.phpfreaks.com/topic/80653-error-message/#findComment-409043 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 } Link to comment https://forums.phpfreaks.com/topic/80653-error-message/#findComment-409045 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> Link to comment https://forums.phpfreaks.com/topic/80653-error-message/#findComment-409057 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. Link to comment https://forums.phpfreaks.com/topic/80653-error-message/#findComment-409061 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. Link to comment https://forums.phpfreaks.com/topic/80653-error-message/#findComment-409065 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.