Solved

PHP Get Values from Associative Array

Posted on 2013-12-17
2
438 Views
Last Modified: 2013-12-17
I have a form that collects amounts paid here...

http://buildmomentum.org/register/lc/group_payment.php?id_church=ORG101&total_group_payment=100

When the button is clicked, I need to give the person entering the amounts an opportunity to check their entry.  How do I convert the associative array into variables I can show the user, then pass on to the next page for processing?

I could post code I've tried, but I've tried so many different ways of doing a "For Each" loop and "Implode" from $_POST that I don't know what code to post.  At this point, I think it's easier to start from scratch.

Thanks,

Steve
0
Comment
Question by:stkoontz
2 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 39724054
try:
if(isset($_POST['payment']) && $_POST['payment']=='Confirm totals and make payment.')
{
	foreach($_POST['id_registrant'] as $key=>$value)
	{
		$fieldname=htmlentities($key,ENT_QUOTES,'UTF-8');
		$fieldvalue=intval($value);
		echo sprintf('<div>%s: %d<input type="hidden" name="id_registrant[%s]" value="%d"/></div>',$fieldname,$fieldvalue,$fieldname,$fieldvalue);
	}
}

Open in new window


NOTE: If you were to change the names of your input elements from:
<input type="text" name="id_registrant[lastname63700]" id="id_amount1">

to something like:
<input type="text" name="id_registrant[firstname lastname__63700]" id="id_amount1">

then you could split $key at the double underline to get the first and last names:
if(isset($_POST['payment']) && $_POST['payment']=='Confirm totals and make payment.')
{
	foreach($_POST['id_registrant'] as $key=>$value)
	{
		$temp=explode('__',$key);
		$fullname=htmlentities($temp[0],ENT_QUOTES,'UTF-8');
		$fieldname=htmlentities($key,ENT_QUOTES,'UTF-8');
		$fieldvalue=intval($value);
		echo sprintf('<div>%s: %d<input type="hidden" name="id_registrant[%s]" value="%d"/></div>',$fullname,$fieldvalue,$fieldname,$fieldvalue);
	}
}

Open in new window

0
 
LVL 2

Author Closing Comment

by:stkoontz
ID: 39725256
Thanks for the help.  That worked!

Steve
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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 count occurrences of each item in an array.

861 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now