Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic form variables (_POST) 1

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA

Hi,

I need to loop through all the records in an XML file and check if a specific record (user_id) exist in the form submitted. The form field names match the user_id from the XML file.

I assume it's a stupid error but when I execute the script, I get an "Illegal offset" error.

Code:
$users = simplexml_load_file("users.xml");

foreach ($users->user as $user) {         
      // Define User ID from XML file	
      $user_id = $users->id;
	
 
	if (isset($_POST[$user_id)) {
	     echo "User exists";
	}
}
 
i am surprised that you get an illegal offset message rather than some other more serious fatal error. you need to close array keys with a square bracket.
Code:
if (isset($_POST[$user_id[red]][/red])){
 
Apart from the mentioned typos(?), you need to cast it to string or in an indirect way using interpolation.
[tt] if (isset($_POST[(string)$user_id])){[/tt]
or
[tt] if (isset($_POST["$user_id"])){[/tt]
 
Also I suppose this is another typo?
[tt] $user_id = $use[highlight]r[/highlight]->id;[/tt]
 
Thanks to everyone for the help.

tsuji: This is exactly what I was looking for. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top