?
Solved

$_POST values 'disappear on 2nd read ?

Posted on 2013-05-29
5
Medium Priority
?
393 Views
Last Modified: 2013-05-30
Hi,

I having problems understanding what is going on in my function copied below. This function is part of a wordpress functions.php file.

What appears to be happening is that if I read the $_POST values once in my function they contain the values I expect ('valuefield1' and 'valuefield2') but if I read them a second time, straight afterwards in the same function these values disappear - but the count of the $_POST['item_meta'] contains 2 both times !

function mycustomvalidation($errors, $field, $value)
{
	addlog('Check 1 $_POST Values');
	$count = count($_POST['item_meta']);
	while (list($key,$val)=each($_POST['item_meta']))
	{
	   addlog($key . '=>' . $val);
	}
	addlog('$_POST Contains ' . $count . ' Values');

	addlog('Check 2 $_POST Values');
	$count = count($_POST['item_meta']);
	while (list($key,$val)=each($_POST['item_meta']))
	{
	   addlog($key . '=>' . $val);
	}
                 addlog('$_POST Contains ' . $count . ' Values');

    return $errors;
}

Open in new window


The log file (written to by the 'addlog' function seen in the code above) shows

Check 1 $_POST Values
84=>valuefield1
91=>valuefield2
$_POST Contains 2 Values
Check 2 $_POST Values
$_POST Contains 2 Values

Open in new window


(So between lines 5 and 6 in the log I'm expecting to see lines 84=>valuefield1 and 91=>valuefield2 again)

Really strange - be grateful for assistance in resolving this.

FYI
Running UniServer web package (latest version) on Windows 7 PC.
0
Comment
Question by:rcscs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39206224
Maybe 'addlog' doesn't allow duplicate values.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39206231
To double check, you can put var_dump($_POST['item_meta']); right after addlog('$_POST Contains ' . $count . ' Values'); both times.  I think you will see the same thing twice when you do that.
addlog('$_POST Contains ' . $count . ' Values');
var_dump($_POST['item_meta']);

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39206241
Just on the outside chance that I am wrong about this, please ask a moderator to add this to the WordPress Zone.

The PHP each() function returns the current key and value pair from an array and advances the array cursor.  So my recommendation is to use reset($_POST) after line 10 and before line 11.
0
 

Author Comment

by:rcscs
ID: 39206880
Thanks for the replies.

Ray - you got it.

I initially added the while each loop to check on the contents of the S_POST variable - but in doing that I wasn't aware of the behaviour which you describe with the array cursor.

Adding

reset($_POST(['item_meta'])

Did the trick.

Many thanks (been looking at this for hours...)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39207223
Thanks for the points.  Another way of processing arrays is to use foreach(), but with external data, I prefer to know the names of the keys beforehand and only process the keys that are known to my script.  It's a security thing that helps to avoid attack data.  Not sure if you can do that with WordPress.

Best regards, ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question