Solved

$_POST values 'disappear on 2nd read ?

Posted on 2013-05-29
5
380 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 83

Expert Comment

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

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 110

Accepted Solution

by:
Ray Paseur earned 500 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 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 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