Solved

PHP Get Values from Associative Array

Posted on 2013-12-17
2
442 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

679 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