tqla Posted July 1, 2007 Share Posted July 1, 2007 Hi. I have the following script. It validates a form. It works fine. I think I have taken most of the precautions with it but I would still like to add the "strip_tags" function. Can someone please tell me where to put it? I've tried a few places but I can't seem to figure it out. I think I need to do this: $firstName = strip_tags($firstName); $lastName = strip_tags($lastName); etc... But where then do I put it? Thanks. <?php /* set up array containing all the fields */ $labels = array ( "clubCard" => "Club Card Number", "firstName" => "First Name", "lastName" => "Last Name", "address" => "Address", "additionalAddress" => "Additional Address", "city" => "City", "state" => "State", "zip" => "Zip Code", "email" => "Email Address", "confirmEmail" => "Confirm Email Address", "okToContact" => "OK To Contact"); foreach ($_POST as $field => $value) { /* check each field except middle name for blank fields */ if ( $value == "" ) { if ($field != "clubCard" and $field != "additionalAddress" and $field != "okToContact" ) { $blank_array[] = $field; } } /* check text for invalid formats. */ elseif ($field == "address" or $field == "clubCard" or $field == "additionalAddress" ) { if (!ereg("^[A-Za-z0-9' -]{1,50}$",$_POST[$field]) ) { $bad_format[] = $field; } } /* check text for invalid formats. */ elseif ($field == "firstName" or $field == "lastName" or $field == "city" or $field == "state" ) { if (!ereg("^[A-Za-z' -]{1,50}$",$_POST[$field]) ) { $bad_format[] = $field; } } /* check email for invalid format. */ elseif ($field == "zip") { if(!ereg("^[0-9]{5,5}(\-[0-9]{4,4})?$",$value)) { $bad_format[] = $field; } } } /* if any fields are not okay, display error message and form */ if(@sizeof($blank_array) > 0 or @sizeof($bad_format) > 0) { if(@sizeof($blank_array) > 0) { /* display message for missing information */ echo "<b><font color=\"#ff0000\">You didn't fill in one or more required fields. You must enter:</b><br> </font>"; /* display list of missing information */ foreach($blank_array as $value) { echo "<font color=\"#ff0000\"> {$labels[$value]}<br></font>"; } } if(@sizeof($bad_format) > 0) { /* display message for bad information */ echo "<BR><b><font color=\"#ff0000\">One or more fields have information that appears to be incorrect. Correct the format for:</b><br></font>"; /* display list of bad information */ foreach($bad_format as $value) { echo "<font color=\"#ff0000\"> {$labels[$value]}<br></font>"; } } /* redisplay form */ include("survey.php"); exit(); } /* if data is good */ echo "All data is good"; ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2007 Share Posted July 1, 2007 You would put it in this section [added it for you]: <?php foreach ($_POST as $field => $value) { $value = strip_tags($value); /* check each field except middle name for blank fields */ if ( $value == "" ) { if ($field != "clubCard" and $field != "additionalAddress" and $field != "okToContact" ) { $blank_array[] = $field; } } ?> Are you sure this line if ( $value == "" ) Isn't supposed to be if ( $value != "" ) It wouldn't make sense to execute the inner if statement it the value was blank...maybe I just don't know how the code works though. Just an observation. Quote Link to comment Share on other sites More sharing options...
tqla Posted July 1, 2007 Author Share Posted July 1, 2007 Hey pocobuenno1388!! Thanks (again)! I didn't even think of applying the strip_tags to the $value. I was stuck on adding it to each variable. What I asking of this line: if ( $value == "" ) Is if the field equals empty then it is considered a $blank_array. Then later in the script this message is echoed if(@sizeof($blank_array) > 0 or @sizeof($bad_format) > 0) { if(@sizeof($blank_array) > 0) { /* display message for missing information */ echo "<b><font color=\"#ff0000\">You didn't fill in one or more required fields. You must enter:</b><br> </font>"; /* display list of missing information */ foreach($blank_array as $value) { echo "<font color=\"#ff0000\"> {$labels[$value]}<br></font>"; } }; Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2007 Share Posted July 1, 2007 Ah, I see. Interesting way of going at it, but it works =] 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.