foevah Posted November 25, 2007 Share Posted November 25, 2007 I have this real simple form that I want to add validation checking to. So if the user enters in some swear words in the feedback and then they submit the form I wont it to say underneath the form HEY YOU ARE NOT ALLOWED TO SWEAR! I would also like to check if the user has entered a correct email using the @ sign. I am not sure how to do this. Please can someone help? <form method="post" action="processfeedback.php"> Your name: <br /> <input type=text name="name" size=40><br /> Your email address: <br /> <input type=text name="email" size=40><br /> Your feedback:<br /> <textarea name="feedback" rows=5 cols=30> </textarea><br /> <input type="submit" value="Send feedback"> </form> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted November 25, 2007 Share Posted November 25, 2007 add this script above your mail() function. <?php $badwords=array("badword","swearword"); // add each prohibited word in this array // repeat the IF condition below for each field you want to be filtered if (eregi('('.implode('|',$badwords).')', $feedback){ echo "HEY YOU ARE NOT ALLOWED TO SWEAR!"; exit; } // email address validation to check for specific symbols if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email)) { echo "PLEASE ENTER A VALID EMAIL ADDRESS!"; exit; } ?> Quote Link to comment Share on other sites More sharing options...
foevah Posted November 25, 2007 Author Share Posted November 25, 2007 I added your code above the mail() function and the browser gives me this message: Parse error: parse error, unexpected '{' in /home/webmedia/public_html/jecgardner/phpexamples/reg_exp/processfeedback.php on line 18 This is the processfeedback.php with your code added: <?php //create short variable names $name=$HTTP_POST_VARS['name']; $email=$HTTP_POST_VARS['email']; $feedback=$HTTP_POST_VARS['feedback']; $toaddress = 'feedback@example.com'; $subject = 'Feedback from web site'; $mailcontent = 'Customer name: '.$name."\n" .'Customer email: '.$email."\n" ."Customer comments: \n".$feedback."\n"; $fromaddress = 'From: webserver@example.com'; $badwords = array("fuck","swearword"); // add each prohibited word in this array // repeat the IF condition below for each field you want to be filtered if (eregi('('.implode('|',$badwords).')', $feedback){ echo "HEY YOU ARE NOT ALLOWED TO SWEAR!"; exit; } // email address validation to check for specific symbols if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email)) { echo "PLEASE ENTER A VALID EMAIL ADDRESS!"; exit; } mail($toaddress, $subject, $mailcontent, $fromaddress); ?> <html> <head> <title>Feedback Submitted</title> </head> <body> <h1>Feedback submitted</h1> <p>Your feedback has been sent.</p> </body> </html> In this tutorial I am reading it says Note that generally you should check users have filled out all the required form fields using, for example isset(). We have omitted this from the script and other examples for sake of brevity. I have tried finding a tutorial on adding the isset() function to this feedback form but I can't find anything that will help me. Can someone explain how I can add isset() to this form please? I would also like to know why phpQuestioner's swear word filter and email validation doesnt work? Quote Link to comment Share on other sites More sharing options...
xyn Posted November 25, 2007 Share Posted November 25, 2007 Try this. $badwords = array( 'shit','fuck' ); if(in_array($feedback,$array)){ print "You cant swear"; exit; } Also i would replace: if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email)) with: if(!preg_match('|^[a-z0-9_.%-]+@[a-z0-9_-]+\.[a-z0-9]{2,4}$|i', $email)) 1. preg_match is must faster than ereg. 2. ^ $ removes all symbols (foreign characters) 3. |i makes it case-insensitive. 4. i fixed a few things you were missing. Regards. Quote Link to comment Share on other sites More sharing options...
foevah Posted November 25, 2007 Author Share Posted November 25, 2007 I replaced phpQuestioner's code with yours xyn and now the browser says: Warning: in_array(): Wrong datatype for second argument in /home/webmedia/public_html/jecgardner/phpexamples/reg_exp/processfeedback.php on line 18 PLEASE ENTER A VALID EMAIL ADDRESS! Can someone explain the isset() function that this tutorial im reading decides to miss out? Note that generally you should check users have filled out all the required form fields using, for example isset(). We have omitted this from the script and other examples for sake of brevity. Also is it possible for it to say you can't swear on the same page as the form instead of going to a new page then having to click back? So if the user enters an invalid email and swears in the feedback text area can it say invalid email and you can't swear underneath each field instead of it going to a new page?? I have tried using this swear word code but the browser gives me another error message: $swarewords= array(bad,mouth,mother,screw,off); if ($swarewords=!) { echo "no sware words were found."; } else { echo "You naughy person you!"; } Parse error: parse error, unexpected ')' in /home/webmedia/public_html/jecgardner/phpexamples/reg_exp/processfeedback.php on line 16 Quote Link to comment Share on other sites More sharing options...
revraz Posted November 25, 2007 Share Posted November 25, 2007 http://us.php.net/isset Basically checks to see if something is set or not. Can someone explain the isset() function that this tutorial im reading decides to miss out? Note that generally you should check users have filled out all the required form fields using, for example isset(). We have omitted this from the script and other examples for sake of brevity. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted November 25, 2007 Share Posted November 25, 2007 You could use javascript for that if you wished. Something like: The form: <form action="feedback.php" method="post" onsubmit="return checkForm();"> <div id="d_feedback" style="display: none;">Please do not swear.</div><br /> Subject:<input type="text" name="subject" /><br /> Content:<textarea name="content" id="content" cols="10" rows="5"></textarea> The script: <script type="text/javascript> function checkForm(form) { content = document.getElementById('d_content'); if(form.content.indexOf('fuck') >- 1) { content.style.display = 'block'; form.content.focus; return false; } else { content.style.display = 'none'; } return true; } that should work i think :S Quote Link to comment Share on other sites More sharing options...
foevah Posted November 25, 2007 Author Share Posted November 25, 2007 wolphie - yours doesnt work either. ok i have been given 3 different methods here and i cant get any to work :-\ Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 25, 2007 Share Posted November 25, 2007 hmm can't you just use $word = str_replace('badword','XXX',$word); to replace the word with XXXX just change the variable name. or put the badwords into an array and loop through each one. Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 25, 2007 Share Posted November 25, 2007 try this <?php function check_email_simple(&$email) { if (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$',$email)) { return("ERROR"); } else { return("SAFE"); } } function check_badwords(&$feedback) { #make all feedback lower case $feedback = strtolower($feedback); $badwords = array (1=> 'badword1','badword2','badword3'); $total_array= count($badwords); for ($x = 1; $x <= $total_array; $x++) { if (strstr($feedback,$badwords($x))) { return("ERROR"); } } return("SAFE"); } $feedback=mysql_real_escape_string($_POST['feedback']; $name =mysql_real_escape_string($_POST['name']; $email = mysql_real_escape_string($_POST['email']; if (check_email_simple($email) == "ERROR") { echo "your email is wrong"; exit(); } if (check_badwords($feedback) == "ERROR") { echo "you can't use that word in our site"; exit(); } echo "its safe mate"; ?> Sorry for double post Quote Link to comment Share on other sites More sharing options...
foevah Posted November 25, 2007 Author Share Posted November 25, 2007 Distant_storm i get this browser error: Parse error: parse error, unexpected ';' in /home/webmedia/public_html/jecgardner/phpexamples/reg_exp/processfeedback.php on line 49 Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 25, 2007 Share Posted November 25, 2007 <?php function check_email_simple(&$email) { if (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$',$email)) { return("ERROR"); } else { return("SAFE"); } } function check_badwords(&$feedback) { #make all feedback lower case $feedback = strtolower($feedback); $badwords = array (1=> 'badword1','badword2','badword3'); $total_array= count($badwords); for ($x = 1; $x <= $total_array; $x++) { if (strstr($feedback,$badwords($x))) { return("ERROR"); } } return("SAFE"); } $feedback=addslashes($_POST['feedback']); $name =addslashes($_POST['name']); $email =addslashes($_POST['email']); if (check_email_simple($email) == "ERROR") { echo "your email is wrong"; exit(); } if (check_badwords($feedback) == "ERROR") { echo "you can't use that word in our site"; exit(); } echo "its safe mate"; ?> Lol this doesn't work either some unidentified header error I get. Its too late for me to debug it really have fun Quote Link to comment 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.