Solved

How do I create an Array from  a form post and do a for each on the array?

Posted on 2008-06-25
5
207 Views
Last Modified: 2008-06-26
Hello Experts,


How do I create an Array from  a form post and do a for each on the array?


Here is my form and my if isset statement not really sure where to go from there.





Thanks
<?PHP  

if(isset($feed)){
 
 

// Wanting to create an array of the posted data from the form.
 
 

// Then take each value from the array and do a query for each value.
 

foreach($formarray as formexecute){
 
 

}
 

	

}
 
 

   

 echo(" <br><br>    

  <form name=\"form1\" enctype=\"multipart/form-data\" method=\"post\" action=\"index.php\">

  <input type=hidden name=feed value=pricerunner>

    <input type=hidden name=feed value=nextag>

    <input type=hidden name=feed value=pricerunner>

    <input type=hidden name=feed value=shopzilla>

    <input type=hidden name=feed value=pricegrabber>

    
 

  

  

<input type=\"submit\" value=\"Upload Product Feeds\" class=\"btn\"

   onmouseover=\"this.className='btn btnhov'\" onmouseout=\"this.className='btn'\">

  </p>

</form>");
 
 
 

?>

Open in new window

0
Comment
Question by:Devain
  • 2
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 275 total points
ID: 21870893
$_POST is an array, so you just need to iterate over the items:


 
foreach($_POST as $k => $v){
 
  echo $k . " = " . $v;
}

If you are doing a "get" request then use:
foreach($_GET as $k => $v){
 
  echo $k . " = " . $v;
}
0
 

Author Comment

by:Devain
ID: 21871672
ok I guess I dont understand as you look at my form they are all the same. I want to create an array of the posted values then to if statements per each array value and execute different code.  When you click on the form it only displays the last value in the form.


The GET does nothing since I am not using get in my form I am using post but the post uses shows the values when you click on the button.  Here is the code.

   <input type=hidden name=feed value=nextag>
   <input type=hidden name=feed value=pricerunner>
   <input type=hidden name=feed value=shopzilla>
   <input type=hidden name=feed value=pricegrabber>
   
there is not array it only pulls the last value pricegrabber??

sample page    http://servermanagerinc.com/test/working.php

<?PHP  

if(isset($feed)){

 

 

// Wanting to create an array of the posted data from the form.

 

 

// Then take each value from the array and do a query for each value.

 

foreach($_POST as $k => $v){

 

  echo $k . " = " . $v;

}
 

foreach($_GET as $k => $v){

 

  echo $k . " = " . $v;

}

	

}

 

  echo("<style>

 input.btn{

   color:#050;

   font-family:'trebuchet ms',helvetica,sans-serif;

   font-size:100%;

   font-weight:bold;

   background-color:#fed;

   border:1px solid;

   border-top-color:#696;

   border-left-color:#696;

   border-right-color:#363;

   border-bottom-color:#363;

   filter:progid:DXImageTransform.Microsoft.Gradient

      (GradientType=0,StartColorStr='#ffffffff',EndColorStr='#ffeeddaa');}

input.btnhov{

   border-top-color:#c63;

   border-left-color:#c63;

   border-right-color:#930;

   border-bottom-color:#930;}
 
 

</style>   ");  

   

 echo(" <br><br>    

  <form name=\"form1\" enctype=\"multipart/form-data\" method=\"post\" action=\"working.php\">

  <input type=hidden name=feed value=pricerunner>

    <input type=hidden name=feed value=nextag>

    <input type=hidden name=feed value=pricerunner>

    <input type=hidden name=feed value=shopzilla>

    <input type=hidden name=feed value=pricegrabber>

    

 

  

  

<input type=\"submit\" value=\"Upload Product Feeds\" class=\"btn\"

   onmouseover=\"this.className='btn btnhov'\" onmouseout=\"this.className='btn'\">

  </p>

</form>");

 

 

 

?>

Open in new window

0
 

Author Comment

by:Devain
ID: 21871718
The last post may not be understood I hope this one is better.

I guess I don't understand as you look at my form input values  they are all the same. I want to create an array of the posted values then create a if statements per each item in the array and execute different code.  When you click on the form it only displays the last value in the posted form using the above foreach statement.


The GET foreach statement does nothing since I am not using get in my form.


   <input type=hidden name=feed value=nextag>
   <input type=hidden name=feed value=pricerunner>
   <input type=hidden name=feed value=shopzilla>
   <input type=hidden name=feed value=pricegrabber>
   
There is no array it only pulls the last value of the input pricegrabber the others are not displayed or iterated through.

0
 
LVL 9

Assisted Solution

by:tkalchev
tkalchev earned 225 total points
ID: 21873234
  <input type=hidden name=feed[] value=nextag>
   <input type=hidden name=feed[] value=pricerunner>
   <input type=hidden name=feed[] value=shopzilla>
   <input type=hidden name=feed[] value=pricegrabber>


in this case you have an array $_POST["feed"] . You can get the values this way :

foreach ( $_POST["feed"] as $v ) {

...

}
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 275 total points
ID: 21874686
>>...There is no array it only pulls the last value of the input pricegrabber the others are not displayed or iterated through.
Ok, to get a feed array, you basically you need to treat the hidden fields as if they were checkboxes. You need to append [] at the end of the name, exactly like tkalchev showed you, and then the foreach will work correctly.
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

Title # Comments Views Activity
Display different menus depending on if user is logged in or not 11 31
php call to a non-object 3 32
updating the date data 12 21
echo time from sql to input type="time" 9 17
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 count occurrences of each item in an array.

912 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

26 Experts available now in Live!

Get 1:1 Help Now