APD1993 Posted March 25, 2012 Share Posted March 25, 2012 ...added to an error log? Currently, my coding is as follows: Form to gather data: <html><head><title>Car Accident Report Form</title></head> <form action="errorlog.php" method="post"> First Name: <br><input type="text" name="name"><br> Surname:<br> <input type="text" name="surname"><br> Age: <br><input type="text" name="age"><br> Weeks Since Accident: <br><input type="text" name="weeks"><br> <input type="submit" value="Submit"> </form> Coding for error log: <html><head><title>Validating Car Accident Report Form Details</title></head> <?php $name=$_POST["name"]; $surname=$_POST["surname"]; $age=$_POST["age"]; $weeks=$_POST["weeks"]; $subtime = strftime('%c'); $pass = "The details uploaded are fine<br><br>"; if ((((trim($name)="" && trim($surname)="" && trim($age)="" && trim($weeks)="")))) { echo "It seems that you have not entered a value for the Name, Surname, Age or Weeks fields. This has been added to the error log.<br><br>"; error_log("<b><u>Error</b></u><br>The user has not entered any values", 3, "C:/xampp/htdocs/errorlog.txt"); } if (($age<18)&& ($weeks<=1)) { echo "It seems that you have entered an invalid age and number of weeks since the accident. This has been added to the error log.<br><br>"; error_log("<b><u>Error</b></u><br>The user has entered an age that is below 18 and the number of weeks since the accident is equal to or under 1 week!<br>Age entered:" . $age . "<br>Number Of Weeks Since Accident entered:" . $weeks . "<br>Time that form was submitted:" . $subtime . "<br><br>", 3, "C:/xampp/htdocs/errorlog.txt"); } else if ($age<18) { echo "It seems that you have entered an invalid age. This has been added to the error log.<br><br>"; error_log("<b><u>Age Error</b></u><br>The user has entered an age that is below 18!<br>Age entered:" . $age . "<br>Time that form was submitted:" . $subtime . "<br><br>", 3, "C:/xampp/htdocs/errorlog.txt"); } else if ($weeks<=1) { echo "It seems that you have entered an invalid age number of weeks since the accident. This has been added to the error log.<br><br>"; error_log("<b><u>Week Error</b></u><br>The user has entered a number of weeks since the accident that is equal to or under 1 week!<br>Number Of Weeks Since Accident entered:" . $weeks . "<br> Time that form was submitted:" . $subtime . "<br><br>", 3, "C:/xampp/htdocs/errorlog.txt"); } else { echo $pass; } echo "Car Accident Report Form details have been sent<br>"; echo "<a href='readlog.php'>Read Error Log</a>" ?> </html> How can I get it so that if a user presses the space bar in a field, then the trim function sees that there's no white space and then this gets added to the error log? Any help is appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/259704-how-can-i-use-the-trim-function-so-that-any-fields-with-blank-spaces-will-be/ Share on other sites More sharing options...
scootstah Posted March 25, 2012 Share Posted March 25, 2012 Run a trim for all $_POST values and then see if they are empty. $_POST = array_map('trim', $_POST); $name=$_POST["name"]; $surname=$_POST["surname"]; $age=$_POST["age"]; $weeks=$_POST["weeks"]; if (!empty($name) && !empty($surname) && !empty($age) && !empty($weeks)) { // something is empty } EDIT: Also, is that really something you need to log? Quote Link to comment https://forums.phpfreaks.com/topic/259704-how-can-i-use-the-trim-function-so-that-any-fields-with-blank-spaces-will-be/#findComment-1331022 Share on other sites More sharing options...
APD1993 Posted March 25, 2012 Author Share Posted March 25, 2012 Run a trim for all $_POST values and then see if they are empty. $_POST = array_map('trim', $_POST); $name=$_POST["name"]; $surname=$_POST["surname"]; $age=$_POST["age"]; $weeks=$_POST["weeks"]; if (!empty($name) && !empty($surname) && !empty($age) && !empty($weeks)) { // something is empty } EDIT: Also, is that really something you need to log? It's something that I'd want to use, yes BTW, I tried the coding and I have it like this: <html><head><title>Validating Car Accident Report Form Details</title></head> <?php $_POST = array_map('trim', $_POST); $name=$_POST["name"]; $surname=$_POST["surname"]; $age=$_POST["age"]; $weeks=$_POST["weeks"]; $subtime = strftime('%c'); $pass = "The details uploaded are fine<br><br>"; if (!empty($name) && !empty($surname) && !empty($age) && !empty($weeks)) { echo "It seems that you have not entered a value for the Name, Surname, Age or Weeks fields. This has been added to the error log.<br><br>"; error_log("<b><u>Error</b></u><br>The user has not entered any values", 3, "C:/xampp/htdocs/errorlog.txt"); } else if (($age<18)&& ($weeks<=1)) { echo "It seems that you have entered an invalid age and number of weeks since the accident. This has been added to the error log.<br><br>"; error_log("<b><u>Error</b></u><br>The user has entered an age that is below 18 and the number of weeks since the accident is equal to or under 1 week!<br>Age entered:" . $age . "<br>Number Of Weeks Since Accident entered:" . $weeks . "<br>Time that form was submitted:" . $subtime . "<br><br>", 3, "C:/xampp/htdocs/errorlog.txt"); } else if ($age<18) { echo "It seems that you have entered an invalid age. This has been added to the error log.<br><br>"; error_log("<b><u>Age Error</b></u><br>The user has entered an age that is below 18!<br>Age entered:" . $age . "<br>Time that form was submitted:" . $subtime . "<br><br>", 3, "C:/xampp/htdocs/errorlog.txt"); } else if ($weeks<=1) { echo "It seems that you have entered an invalid age number of weeks since the accident. This has been added to the error log.<br><br>"; error_log("<b><u>Week Error</b></u><br>The user has entered a number of weeks since the accident that is equal to or under 1 week!<br>Number Of Weeks Since Accident entered:" . $weeks . "<br> Time that form was submitted:" . $subtime . "<br><br>", 3, "C:/xampp/htdocs/errorlog.txt"); } else { echo $pass; } echo "Car Accident Report Form details have been sent<br>"; echo "<a href='readlog.php'>Read Error Log</a>" ?> </html> And I left all of the fields blank, but I'm presented with this error message: It seems that you have entered an invalid age and number of weeks since the accident. This has been added to the error log. When I want it to display the first error message. Any idea why this is happening? EDIT: Ah, I got rid of the "!" marks since they were being used for "is not" Quote Link to comment https://forums.phpfreaks.com/topic/259704-how-can-i-use-the-trim-function-so-that-any-fields-with-blank-spaces-will-be/#findComment-1331025 Share on other sites More sharing options...
scootstah Posted March 25, 2012 Share Posted March 25, 2012 Whoops, I must have been spacing when I wrote that. Here, try this instead: if (empty($name) || empty($surname) || empty($age) || empty($weeks)) Quote Link to comment https://forums.phpfreaks.com/topic/259704-how-can-i-use-the-trim-function-so-that-any-fields-with-blank-spaces-will-be/#findComment-1331037 Share on other sites More sharing options...
cpd Posted March 25, 2012 Share Posted March 25, 2012 I wouldn't directly manipulate the $_POST variable. Assign it to a new variable in-case you have some need for the $_POST data later on. When your building large applications with a lot of post data you may need the original. Quote Link to comment https://forums.phpfreaks.com/topic/259704-how-can-i-use-the-trim-function-so-that-any-fields-with-blank-spaces-will-be/#findComment-1331053 Share on other sites More sharing options...
scootstah Posted March 25, 2012 Share Posted March 25, 2012 While I would normally agree, I think manipulating $_POST in this case is acceptable. The reason is that 1) you probably won't need to preserve white-space at the beginning/end of the data anywhere else and 2) it ensures that if you re-display the form with prepopulated fields that they will be in fact empty, and not full of spaces (which might confuse someone). Quote Link to comment https://forums.phpfreaks.com/topic/259704-how-can-i-use-the-trim-function-so-that-any-fields-with-blank-spaces-will-be/#findComment-1331056 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.