Jump to content

[SOLVED] Strip_tags Help Needed


tqla

Recommended Posts

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";
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
        }
    };

 

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.