[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

$_POST values 'disappear on 2nd read ?

Posted on 2013-05-29
5
Medium Priority
?
403 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 dynamically set the form action using jQuery.
Suggested Courses

650 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