Jump to content

Creating A Feedback Email Script


walapu

Recommended Posts

I have been working at this for hours and I still havnt fixed it lol. Check it out

<?phpfunction spamcheck($field)  {  //filter_var() sanitizes the e-mail  //address using FILTER_SANITIZE_EMAIL  $field=filter_var($field, FILTER_SANITIZE_EMAIL);  //filter_var() validates the e-mail  //address using FILTER_VALIDATE_EMAIL  if(filter_var($field, FILTER_VALIDATE_EMAIL))    {    return TRUE;    }  else    {    return FALSE;    }   }if (!isset($_REQUEST['Name']))   {   echo "<span color=\"red\">-Please enter your name</span><br/>";   }$mailcheck = spamcheck($_REQUEST['Email']);if ($mailcheck==FALSE)   {   echo "<span color=\"red\">-Please enter a valid email</span><br/>";   }     if (!isset($_REQUEST['Feedback']))   {   echo "<span color=\"red\">-Please fill out the feedback form</span><br/>";   }   if (isset($_REQUEST['Name'], $_REQUEST['Feedback']) $mailcheck==FALSE)//Checks if all forms are filled in   {   $name = $_REQUEST['Name'] ;   $email = $_REQUEST['Email'] ;   $subject = Feedback;   $message = $_REQUEST['Feedback'] ;   mail("feedback@blabla.com", "$subject","$name has sent feedback: \n $message", "From: $email" );   echo "Thank you for your feedback, we will review it shortly";   }      ?>

Its the if (isset($_REQUEST['Name'], $_REQUEST['Feedback']) $mailcheck==FALSE) were I am having problems. Also the fact that when I press submit it clears the form if incorrect

Link to comment
Share on other sites

isset only takes one parameter, you're trying to give it two. You also need to separate conditions with AND or OR, you don't just list the conditions. It would probably be better if you used a true/false variable to keep track of whether or not errors happened and check that one variable at the end so that you don't have to check all of the conditions again.

<?php$errors = false;if (!isset($_REQUEST['Name'])){  $errors = true;  ...}$mailcheck = spamcheck($_REQUEST['Email']);if ($mailcheck==FALSE){  $errors = true;  ...}if (!isset($_REQUEST['Feedback'])){  $errors = true;  ...}if (!$errors){  $name = $_REQUEST['Name'];  ...}?>

Link to comment
Share on other sites

When I first load the page it gives me the errors then when I submit the page with nothing it only says no valid email :). Whats wrong? It then allows me to submit with just the email form filled out.

<?phpfunction spamcheck($field)  {  //filter_var() sanitizes the e-mail  //address using FILTER_SANITIZE_EMAIL  $field=filter_var($field, FILTER_SANITIZE_EMAIL);  //filter_var() validates the e-mail  //address using FILTER_VALIDATE_EMAIL  if(filter_var($field, FILTER_VALIDATE_EMAIL))    {    return TRUE;    }  else    {    return FALSE;    }   }$errors = false;if (!isset($_REQUEST['Name']))   {   $errors = true;   echo "<span color=\"red\">-Please enter your name</span><br/>";   }$mailcheck = spamcheck($_REQUEST['Email']);if ($mailcheck==FALSE)   {   $errors = true;   echo "<span color=\"red\">-Please enter a valid email</span><br/>";   }     if (!isset($_REQUEST['Feedback']))   {   $errors = true;   echo "<span color=\"red\">-Please fill out the feedback form</span><br/>";   }   if (!$errors) //checks for no errors   {   $name = $_REQUEST['Name'] ;   $email = $_REQUEST['Email'] ;   $subject = Feedback;   $message = $_REQUEST['Feedback'] ;     if ($_SERVER['HTTP_X_FORWARD_FOR']) {   $ip = $_SERVER['HTTP_X_FORWARD_FOR'];   } else {   $ip = $_SERVER['REMOTE_ADDR'];   }   mail("feedback@blabla.com", "$subject","Name: $name \n Email: $email \n Feedback: $message \n Ip: $ip ", "From: $email" );   echo "Thank you for your feedback, we will review it shortly";   }      ?>

Link to comment
Share on other sites

Right, that's what the code is telling it to do. The isset function (not surprisingly) checks if a variable is set. $_REQUEST['name'] is not going to be set if you just load the page without submitting the form, so the code is telling it to print an error message in that case. It lets through a blank value because you're only checking if the variable is set at all, not what the value is. Submitting the form will set the variable, even though it might set it to a blank value.You should check if the form has been submitted at all before doing any validation or anything else. If the form hasn't been submitted, there's no point to validate the data that isn't there. Check here for examples about how to determine if a form was submitted, and how to check for empty values:http://w3schools.invisionzone.com/index.php?showtopic=12509

Link to comment
Share on other sites

I feel like this should be working, but idk why it is not. I have taken at look at if (isset($_POST['submit_button']) and $_REQUEST['Feedback'] = '') but I feel this way is more reliable. I also added a ban feature :)

<?php   if ($_SERVER['HTTP_X_FORWARD_FOR']) {   $ip = $_SERVER['HTTP_X_FORWARD_FOR'];   } else {   $ip = $_SERVER['REMOTE_ADDR'];   }$mailcheck = spamcheck($_REQUEST['Email']);function spamcheck($field)  {  //filter_var() sanitizes the e-mail  //address using FILTER_SANITIZE_EMAIL  $field=filter_var($field, FILTER_SANITIZE_EMAIL);  //filter_var() validates the e-mail  //address using FILTER_VALIDATE_EMAIL  if(filter_var($field, FILTER_VALIDATE_EMAIL))    {    return TRUE;    }  else    {    return FALSE;    }   }$bancheck = bancheck($ip);function bancheck($field)  {  if($field = '')    {    return TRUE;    }  else    {    return FALSE;    }   }      $errors = false;if (isset($_POST['submit_button']) and !is_string || ($_REQUEST['Name'] = trim($_REQUEST['Name'])))   {   $errors = true;   echo "<font color=\"#ff0000\">-Please enter your name</font><br/>";   }if (isset($_POST['submit_button']) and $mailcheck==FALSE)   {   $errors = true;   echo "<font color=\"#ff0000\">-Please enter a valid email</font><br/>";   }     if (isset($_POST['submit_button']) and !is_string || ($_REQUEST['Feedback'] = trim($_REQUEST['Feedback'])))   {   $errors = true;   echo "<font color=\"#ff0000\">-Please fill out the feedback form</font><br/>";   }if (isset($_POST['submit_button']) and $bancheck==TRUE)   {   $errors = true;   echo "<font color=\"#ff0000\">*Sorry but you have been banned from using this form due to abuse*</font><br/>";   }     if (isset($_POST['submit_button']) and !$errors) //checks for no errors   {   $name = $_REQUEST['Name'] ;   $email = trim($_REQUEST['Email']) ;   $subject = Feedback;   $message = $_REQUEST['Feedback'] ;   mail("feedback@blabla.com", "$subject","Name: $name \n Email: $email \n Feedback: $message \n Ip: $ip", "From: $email" );   echo "Thank you for your feedback, we will review it shortly";   }      ?>

Link to comment
Share on other sites

The 2 conditions using is_string aren't formatted correctly. is_string is a function, so you need to send it a parameter to check, but you aren't doing that. Regardless, all of the form data is a string, all form data, even if they type in numbers, is considered string data. So is_string would always return true anyway. I'm not sure what you're trying to check here:($_REQUEST['Name'] = trim($_REQUEST['Name'])If you're trying to check if the value is blank, you can use the empty function:

if (isset($_POST['submit_button']) and empty(trim($_REQUEST['Name'])))

or compare it with an empty string:

if (isset($_POST['submit_button']) and trim($_REQUEST['Name']) == '')

Link to comment
Share on other sites

Hmm this gives me an error

if (isset($_POST['submit_button']) and empty(trim($_REQUEST['Name'])))

Fatal error: Can't use function return value in write context in /blabla/htdocs/feedback/index.php on line 99But its fine ill just compare it to an empty value.Also question how do you make the form not clear itself on submit? I have looked everywhere but I can not find a solution

Link to comment
Share on other sites

Also question how do you make the form not clear itself on submit? I have looked everywhere but I can not find a solution
An easy solution would be that you add an attribute value inside the input tag of your textfield and set its default argument equal to $_REQUEST['Name'] as shown below -
<input type="text" name="Name" value="<?php if (isset($_REQUEST['Name']))echo $_REQUEST['Name'];elseecho 'You can give a default value to be displayed when page is loading for the first time';?>"/>

Good Luck :)

Link to comment
Share on other sites

EDIT: ahhh I am about to kill myself. I change a few things and add a subject form, then stupidly quit dreamweaver so there was no going back. Also for some god knows unknown reason it wont keep the text in the feedback form when I submit anymore.Here is there error I am getting.Warning: filter_var() expects parameter 2 to be long, string given in /data/19/1/45/144/1860307/user/2020087/htdocs/feedback/index.php on line 90Warning: filter_var() expects parameter 2 to be long, string given in /data/19/1/45/144/1860307/user/2020087/htdocs/feedback/index.php on line 94I have no clue what I have done to cause this.lol and I know I am using browser side scripting

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...