steph90 Posted October 13, 2010 Share Posted October 13, 2010 I have created a form where the user enters their first name, last name, work hours, and rate. I want to add a check so that once submit is pressed, the page will return an error if one of the fields was left blank. How can I do it?? I have been trying for hours... Here is my code: <html> <title> Pay Calculator </title> <form method='post' action='pay2.php'> First Name: <input type='text' name='first' value="<?php echo ($_POST['first']); ?>" <br/> Last Name: <input type='text' name='last' value="<?php echo ($_POST['last']); ?>" <br/> Hours of Work: <input type='text' name='hours' value="<?php echo ($_POST['hours']); ?>" <br/> Hourly Pay Rate: <input type='text' name='rate' value="<?php echo ($_POST['rate']); ?>" <br/> <input type='submit' value='submit'> </form> </html> <?php $pay = $_POST['pay']; $hours = $_POST['hours']; $extra = $_POST['extra']; $rate = $_POST['rate']; if ($hours > 40){ $extra=$hours-40; $hours=40; $pay=($extra*1.5*$rate)+($hours*$rate); } else { $pay=$hours*$rate; } if (empty($_POST['$first'])) {print "**Please enter your first name\n";} echo "\n Your pay is: <br /> $ $pay <br />" ; ?> Link to comment https://forums.phpfreaks.com/topic/215826-error-checking-a-form/ Share on other sites More sharing options...
steph90 Posted October 14, 2010 Author Share Posted October 14, 2010 Sorry, here is the code in the correct form...Please Help!! <html> <title> Pay Calculator </title> <form method='post' action='pay2.php'> First Name: <input type='text' name='first' value="<?php echo ($_POST['first']); ?>" <br/> Last Name: <input type='text' name='last' value="<?php echo ($_POST['last']); ?>" <br/> Hours of Work: <input type='text' name='hours' value="<?php echo ($_POST['hours']); ?>" <br/> Hourly Pay Rate: <input type='text' name='rate' value="<?php echo ($_POST['rate']); ?>" <br/> <input type='submit' value='submit'> </form> </html> <?php $pay = $_POST['pay']; $hours = $_POST['hours']; $extra = $_POST['extra']; $rate = $_POST['rate']; if ($hours > 40){ $extra=$hours-40; $hours=40; $pay=($extra*1.5*$rate)+($hours*$rate); } else { $pay=$hours*$rate; } if (empty($_POST['$first'])) {print "**Please enter your first name\n";} echo "\n Your pay is: <br /> $ $pay <br />" ; ?> Link to comment https://forums.phpfreaks.com/topic/215826-error-checking-a-form/#findComment-1121998 Share on other sites More sharing options...
pengu Posted October 14, 2010 Share Posted October 14, 2010 if (empty($pay) || empty($hours) || empty($extra) || empty($rate)) { echo "You have forgotten to fill something out, click the back button and try again."; exit(); } Something like this. Link to comment https://forums.phpfreaks.com/topic/215826-error-checking-a-form/#findComment-1122021 Share on other sites More sharing options...
pengu Posted October 14, 2010 Share Posted October 14, 2010 Also.. <form method='post' action='pay2.php'> First Name: <input type='text' name='first' value="<?php echo ($_POST['first']); ?>" <br/> Last Name: <input type='text' name='last' value="<?php echo ($_POST['last']); ?>" <br/> Hours of Work: <input type='text' name='hours' value="<?php echo ($_POST['hours']); ?>" <br/> Hourly Pay Rate: <input type='text' name='rate' value="<?php echo ($_POST['rate']); ?>" <br/> <input type='submit' value='submit'> </form> This is wrong, the VALUES should be like 'first', 'last', 'hours' etc etc.. Link to comment https://forums.phpfreaks.com/topic/215826-error-checking-a-form/#findComment-1122022 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.