Jump to content

[SOLVED] $_POST: fieldnames are correct however arent matchign any values


PC Nerd

Recommended Posts

Hi,

 

Ive got a script where it loops through all the $_POST values - using an if($field == "fieldname") {valuidate for that field;}

 

I know that the field names are correct because they are beign echoed out correctly.  However when i try and patch then to a string, it wont work.

 

Its probably best described by the code and output.:

 

CODE:

 

.....
foreach($_POST as $field => $value) {
	#echo "Stage 1.1 <br>";
	echo $field."<br>";

	#if($_POST['Save'] == '---') {$ERROR['Save'] = 'empty';}

	if($field == "Save" && $value == "---") {
		$ERROR[$field] = "empty";
	}

	if($field == "User_Name" || $field == "Password" || $field == "Conf_Password") {
		if($_POST['Save'] == "1") {
			### Validate the Username, password and email
			if(empty($value)) {
				$ERROR = $ERROR[$field] = 'empty';
			}
			else if(!preg_match("[A-za'-z]", $value)) {
				$ERROR[$field] = 'invalid';
			}
			else if(strlen($value) > 20 || strlen($value) < 5) 
				$ERROR[$field] = 'len';
			}
			else if($field == "Password" && $value != $_POST['Conf_Password']) {
				$ERROR[$field] = 'passmtch';
			}
		}
	}


	if($field == "F_Name" || $field == "L_Name" || $field == "User_Name" || $field == "Emg_Name" || $field == "Emg_Relation") {
		### Validate the names
		echo "is names<br>";
		if(!preg_match("[A-za-z' -]", $value)) {
			$ERROR[$field] = 'invalid';
		}
		else if(strlen($value) > 20) {
			$ERROR[$field] = 'len';
		}

	}
	if($field == "Email") {
		if (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $input_email)) {
			$ERROR[$field] = 'invalid';
		}
	}
	if($field == "Carrier" && $value = "---") {
		$ERROR[$field] = 'empty';
	}
.....

 

Output:

 

F_Name

L_Name

Email

User_Name

Password

Conf_Password

Cell_no

Carrier

DOB

Address

Emg_Name

Emg_Phone

Emg_Relation

Save

Stage 2

 

Stage 3

 

Array

(

    [F_Name] => Michael

    [L_Name] =>

    =>

    [user_Name] =>

    [Password] =>

    [Conf_Password] =>

    [Cell_no] =>

    [Carrier] => ---

    [DOB] =>

    [Address] =>

    [Emg_Name] =>

    [Emg_Phone] =>

    [Emg_Relation] =>

    [save] => ---

)

 

 

As you can see - the values arent matchign for the $field - and the "F_Name" etc.

 

I think its somethign to do with the datatype of the field value - however I cant tell.

 

Any suggestions on how this can be solved woudl be great.

 

Thansk in advance,.

Link to comment
Share on other sites

I dont know why do u need to get the field name(u have set in the code statically) and the value set in the field(dynamically by the one who enters the value) to be matched.

Actually what is the problem and what do you need, I have looked at the code it seems to be syntactically correct but logical correctness can be maintained by the implementer only.

Link to comment
Share on other sites

Yeah.

 

I know that I probably coudl have designed the validation better - and I wil lnext time.  However what isnt working is $field never matches any of the values its compared to - even though ive statically defined its value.

 

ill attach teh full file.

 

 

Thanks

 

[attachment deleted by admin]

Link to comment
Share on other sites

Im not really familiar with regular expressions - ive sort of read through forums and then used different expressions from around the place to get mine.  as you saying it should be:

 

if(!preg_match([A-za\'-z], $value)) ???

 

 

if so - it still doesnt explain my original problem, statically defined values arent matching.

 

Thanks

Link to comment
Share on other sites

Im not really familiar with regular expressions - ive sort of read through forums and then used different expressions from around the place to get mine.  as you saying it should be:

 

if(!preg_match([A-za\'-z], $value)) ???

 

 

if so - it still doesnt explain my original problem, statically defined values arent matching.

 

Thanks

Not what I'm saying.

 

if(!preg_match([A-Za-z], $value))

Link to comment
Share on other sites

if you want to say match letters a-z upper or lowercase and the character '

say this:

 

$pat = "~[a-z']~i";

$result = preg_match($pat, $value);

//result can be true or false..or..see manual

 

where ~ is the delimiter (see manual, you need these to specify regex patterns, you were missing delimiters before)

 

you can't just stick ' in the middle of the a-z range, it must come after

a-z' not a'-z

Link to comment
Share on other sites

ok - ive changed that however my original problen is still there.

 

The foreach defiend $field - wont match with any of the String values im comparing it to  even though they appear the same with print_r($_POST) etc etc

 

any suggestions on that

 

*thanks for the preg_match() stuff

Link to comment
Share on other sites

No - shoudl I

 

 

i had a value in my $_POST array i think that was field= 1 value = NULL.  That is probably the submit button??

 

Ive created an increment through the foreach - that when it reaches 14 ( the number of fileds in the form) - it break;'s the loop.

 

as i said before- should i have the submit button in the loop?

 

Thanks

Link to comment
Share on other sites

ok-

 

its a basic signup script - il upload a copy of the output for you in a second

as for the required fields - all are required, except username and password - they are only required if the "save" radio button is checked.

 

my script iss attached above - however that isnt the one with teh updated preg_match expressions updates.

 

the script output is available for viewing - http://www.battleages2.com/new_user.php

 

Thanks for all your help.

Link to comment
Share on other sites

Try this please/.......... only a test i used eregi sorry

<?php

foreach($_POST as $field => $value) {
	echo $field."<br>";
        if($field == "Save" && $value == "---") {
    $ERROR[$field] = "empty";
	}
        if($field == "User_Name" || $field == "Password" || $field == "Conf_Password") {
	if($_POST['Save'] == "1") {
	if(empty($value)) {
	$ERROR = $ERROR[$field] = 'empty';
	}
	elseif(!eregi("[A-z\'\-]", $value)) {
	$ERROR[$field] = 'invalid';
	}
	elseif(strlen($value) > 20 || strlen($value) < 5) 
	$ERROR[$field] = 'len';
	}
	elseif($field == "Password" && $value != $_POST['Conf_Password']) {
	$ERROR[$field] = 'passmtch';
			}
		}
	}
       if($field == "F_Name" || $field == "L_Name" || $field == "User_Name" || $field == "Emg_Name" || $field == "Emg_Relation") {
   echo "is names<br>";
   if(!eregi("[A-z\'\-]", $value)) {
   $ERROR[$field] = 'invalid';
		}
   elseif(strlen($value) > 20) {
   $ERROR[$field] = 'len';
		}
       }
   if($field == "Email") {
   if(!eregi("^[A-Z0-9\._\-]+\@[A-Z0-9.-]+\.[A-Z]{2,4}$", $input_email)) {
   $ERROR[$field] = 'invalid';
		}
	}
  if($field == "Carrier" && $value = "---") {
  $ERROR[$field] = 'empty';
   }
?>

Link to comment
Share on other sites

<?
foreach($_POST as $field => $value) {
if(empty($value) && $field == "Conf_Password"){// put here other stuff similar to this
	continue;
}
if(isset($value)){
	if($field == "Email"){
		if (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $input_email)) {
			$ERROR[$field] = 'invalid';
		}
	}
	else{
		if(!ctype_alnum($value)){// alphanumeric only
			$error[] = $field ;
	}
}
else{
	$empty[] = $value;
}
}
?>

i rewrite the codes maybe you can have a good start out of that but note not tested..and that is not complete you may just follow the flow of that code

Link to comment
Share on other sites

ok-

 

firslty ive tried redarrows code - exactly the same......

 

my expected output - is to have the $ERRORS array contain more than the "Save"  Value

 

I know that the $field isnt matching because it never reaches the  echoo "got to names"; line straight after.

 

if the ereg or preg_match isnt workign then it should either return an error for that function - or return false when i want it to return true (justu go !preg_match() to solve that etc etc).

 

my problem is no the regular expressions used to validate - its matching the $field variable to the values of "F_Name" etc etc.

 

Thanks

Link to comment
Share on other sites

posting??

 

here is an over view of how the script runs:

 

if(isser($_GET['validate')) ## checks to see if form is submitted and set to validate it....

  then validate by:

    looping through$_POST ( how i sent the data from teh form);

    i get teh $field and $value out of that $_POST array  - and then check to see if $field is F_Name, L_Name, EMAIL etc etc.(THIS IS WHERE THE ERROR IS)  thisis basicalyl to shortcut agains validating the same thing.... ie F_Nane, EMG_Contact etc etc all have the same validation proccesses (same regex etc).

 

  once it matches the field - it validates if $value is empty- or too long, too short - invalid data(regex) etc.  then is sets the $ERROR array to include a value for that $field if there is an error ** it writes over the previous error for that field **  $ERROR is in the format of:  $ERROR[fieldname] = error_type  eg $ERROR['F_Name'] = 'invalid'; ( auto messages are creted for different error types ;ater in the script).

 

 

 

 

the script goes on to either submit to MYSQL if there were no errors, or redisplay the form if there were - and display error messages.

 

 

 

 

*** the error im receiveing is not a PHP error as such - its a code flow.  what shoudl happpen is $field shoudl match one of the if(statements from teh foreach loop - however it is not.)

 

 

 

i could possilby create a flow chart if that woudl be easier to understadn ( i knwo that this is slightly confusing even for me on accaision so let me knwo if you want me to rewrite it or something etc).

 

 

Thanks for your help so far.... - im looking into teng84's code atm - ill let you knwo how that goes.

 

EDIT:  that code simply does the same thing does it not - it just does it in a different order to how im doing it (i know i shoudl do it this way but ill rewriting it later) - how coudl this solve the problem  - coudl you explain what its meant to do different?  Thanks END EDIT

 

Thanks for all your help so far.

Link to comment
Share on other sites

Update:

 

ive added this at the beginning of the loop:

if($field == "L_Name") {echo "ok - value working :: ".$field."<br>";}

 

and i change the "L_Name" to whatever value i want - and it all works...

 

thsi puts the problem down the the if()'s....

 

if one is workig in dividually - shouldnt multiple conditions in it - seperated by ||  work?

 

ie:

 

if:  if(condition1) {}

      if(condition2) {}

      if(condition3) {}

      if(condition4) {}

 

then shouldnt:

 

      if(condition1 || condition2 || condition3 || condition4) {} ?

 

Thanks

Link to comment
Share on other sites

I found your logic error that is preventing the "name" checking logic to execute (you need to use a program editor that lets you match up { })

 

 

<?php
	else if(strlen($value) > 20 || strlen($value) < 5) 
		$ERROR[$field] = 'len';
	}
	else if($field == "Password" && $value != $_POST['Conf_Password']) {
		$ERROR[$field] = 'passmtch';
	}
}
}
?>

 

In the above code, line #58 in the posted code - else if(strlen($value) > 20 || strlen($value) < 5) has no opening {, this causes the } that is two lines down (line #60) to match the { on line #50. This also makes the } on line #64 match the { on the end of line #49. The } on line 65 matches the { on your foreach() loop. So, the foreach loop finishes for all the $_POST values before the code that checks the names is ever executed.

 

This also means you are missing a } later in the code. I would guess at line #176 where there is one commented out.

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.