Jump to content

How can I use the "trim" function so that any fields with blank spaces will be..


APD1993

Recommended Posts

...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! :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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" :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.