Jump to content

[SOLVED] preg_replace or array where have i gone wrong?


HoTDaWg

Recommended Posts

hi,

 

for the sake of learning i have oversimplified this script. It is merely for my ammusent however it does not seem to be functioning properly. Take a look:

 

<?php
error_reporting(E_ALL);
function form()
{
    echo '<form name="yes" action="' . $_SERVER['PHP_SELF'] . '" method="POST">
<input type="text" name="name" value="name"><br /><input type="text" value="age" name="age"><br /><br />
<input type="submit" name="submit" value="Submit!">';
}

$submit = $_POST['submit'];
if (!isset($submit))
{
    form();
}
else
{
    $errorhandler = array();
    $name = $_POST['name'];
    $age = $_POST['age'];
    $the_form = array('Name' => "$name", 'Age' => "$age");
    foreach($the_form as $key => $field)
    {
        if (preg_match("/[^a-zA-Z0-9]/", $field))
        {
            $errorhandler = array('Field' => "$key", 'Type' => "Invalid Character", 'Message' =>"An invalid character was found. Only numbers and letters are allowed.");
        	echo "Error, in $field";
	}
        if (preg_match("/^[A-Z]/", $field))
        {
            $errorhandler = array('Field' => "$key",'Type' => "Invalid Character",'Message'=>"An uppercase or capital letter was found. Please keep characters in lower case.");
            echo "Error, in $field";
        }
        if (strlen($field) > 20)
        {
		$errorhandler = array('Field' => "$key", 'Type' => "Too Long", 'Message' =>"Your response is too long in length. The maximum number of characters allowed is 20.");
		echo "Error, in $field";
	}
    }
    if(count($errorhandler) > 0) 
    {
	print_r($errorhandler);
	form();
}
else
{
	echo "no errors.";
}

}
?>

 

Currently, when the form is submitted, regardless of whether or not it meets requirements, it jumps directly to No Errors.

 

any ideas?

 

this is my first time with multidimensional arrays, so i personally am leaning towards something wrong with the count command...but what?!

 

thanks,

 

HoTDaWg

Link to comment
Share on other sites

Since an error is printed if it finds a match, whether anything is in the error array or not, I would say your preg_match isn't working. You might want to echo out the value of $field before the preg_match, too, just to make sure it has the characters you are looking for.

Link to comment
Share on other sites

Actually, the array() function returns an array and using "[]" creates an array, so using them both will make a two dimensional array.

The following would work without using the array function.

$errorhandler[] = blar;
$errorhandler[] = blar;

 

EDIT: Except that that IS what he is trying to do, so you are right, it should be with a "[]" or else it will be overrighting the previous value; though I don't think that is the cause of his problems because 1 > 0.

Link to comment
Share on other sites

hey sorry for the lack of update guys. thanks for the help so i currently change the script around. it worked fine but now i am trying to get it to output the errors properly. take a look at the script.

 

<?php

/**
* @author x-ecutioner
* @copyright 2007
*/
error_reporting(E_ALL);
function form()
{
    echo '<form name="yes" action="' . $_SERVER['PHP_SELF'] . '" method="POST">
<input type="text" name="name" value="name"><br /><input type="text" value="age" name="age"><br /><br />
<input type="submit" name="submit" value="Submit!">';
}

$submit = $_POST['submit'];
if (!isset($submit))
{
    form();
}
else
{
    $errorhandler = array();
    $name = $_POST['name'];
    $age = $_POST['age'];
    $the_form = array('Name' => "$name", 'Age' => "$age");
    foreach($the_form as $key => $field)
    {
        if (preg_match("/[^a-zA-Z0-9]/", $field))
        {
            $errorhandler[]= array('Field' => "$key", 'Type' => "has an invalid character(s).", 'Message' =>"Only numbers and letters are allowed.<br>");
        	echo "Error, in $key<br />";
	}
        if (preg_match("/^[A-Z]/", $field))
        {
            $errorhandler[]= array('Field' => "$key",'Type' => "contains (a) capital letter(s).",'Message'=>"Please keep characters in lower case.<br>");
            echo "Error, in $key<br />";
        }
        if (strlen($field) > 20)
        {
		$errorhandler[]= array('Field' => "$key", 'Type' => "is too long.", 'Message' =>"The maximum number of characters allowed is 20.");
		echo "Error, in $key<br />";
	}
    }
    if(count($errorhandler) > 0) 
    {
	if(count($errorhandler[0]) > 0 )
	{
		echo "<li>The ".$errorhandler[0][0]."field".$errorhandler[0][1].$errorhandler[0][2]."</li>";	
	}
	elseif(count($errorhandler[1]) > 0 )
	{
		echo "<li>The ".$errorhandler[1][0]."field".$errorhandler[1][1].$errorhandler[1][2]."</li>";	
	}
	else
	{
		echo "<li>The ".$errorhandler[2][0]."field".$errorhandler[2][1].$errorhandler[0][2]."</li>";	
	}
	form();		
}
else
{
	echo "no errors.";
}

}
?>

 

the problem is, i get the following errors:

 


Notice: Undefined offset: 0 in ********* on line 48

Notice: Undefined offset: 1 in ********* on line 48

Notice: Undefined offset: 2 in ********* on line 48

 

this is getting really confusing. do you think it could have anything to do with the [] which are being put now? or is it the output thats messed

 

thanks for any help possible,

 

HoTDaWg

Link to comment
Share on other sites

alright it works perfectly! thanks so much for your help and also too lemmin!

 

imma mark it solved for now although chances are i am going to try adding more features to this script in which case i will reactive under a different title.:P

 

thanks for all the help,

 

HoTDaWg

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.