1

I am trying to prepare a loop that will store the data from form fields below as a variable, that I can then use to create a new dynamic form based on this data.

FLOW EXAMPLE:
1. jquery submits hidden form in popup iframe(done)
2. PHP loops through POST from hidden form in iframe and outputs as new form in current popup iframe.
3. user selects the fields they wish to submit to database. Submits form.
4. database entry is added
5. selected fields are removed from current iframe. None selected are left.

I am stuck on the loop part of the code, I need an efficient solution that allows me to loop through POST and output a new form that the user can submit.

I know its alot to ask, but any help would be great, been banging my head against the wall trying to figure out how to do this. :(

Thanks, Steve

PS. Sometimes there will be 10 fields, sometimes 2....it all depends on what is on the page when the iframe is created. But there will always be the same number of each field. Like in my field example below.

EXAMPLE OF FIELDS (all hidden)

<input type="hidden" id="data[]" name="data[]" value="SOMETHING HERE">
<input type="hidden" id="data[]" name="data[]" value="SOMETHING BLAH BLAH">
<input type="hidden" id="data[]" name="data[]" value="BLAH BLAH SOMETHING">

<input type="hidden" id="info[]" name="info[]" value="MY INFO HERE">
<input type="hidden" id="info[]" name="info[]" value="RANDOM INFO HERE">
<input type="hidden" id="info[]" name="info[]" value="MORE INFO">

<input type="hidden" id="img[]" name="img[]" value="http://example.com/img2.gif">
<input type="hidden" id="img[]" name="img[]" value="http://example.com/someimage.jpg">
<input type="hidden" id="img[]" name="img[]" value="http://example.com/myimage.png">

<input type="hidden" id="title[]" name="title[]" value="Item title goes here">
<input type="hidden" id="title[]" name="title[]" value="Item title goes here ">
<input type="hidden" id="title[]" name="title[]" value="Item title goes here">

MY PHP loop I tried:

 foreach ($_POST['data'] as $data=>$val){
    echo "$data: $val <br />";
   }
 foreach ($_POST['info'] as $info=>$val){
    echo "$info: $val <br />";
   }
 foreach ($_POST['img'] as $img=>$val){
    echo "$img: $val <br />";
   }
 foreach ($_POST['title'] as $title=>$val){
    echo "$title: $val <br />";
   }

EXAMPLE PHP Output

0: €29.16
1: €34.46
2: €34.46
3: €63.62
4: €53.02
5: €10.60
6: €42.42
0: ../img1.gif
1: ../img2.jpg
2: ../img3.jpg
3: ../img4.png
4: ../img5.gif
5: ../img6.jpg
6: ../img7.jpg
    etc...

Example of how the output needs to be structured in the new form:

Item 1      Item 2        item 3         item 4  

Title0       title1        title2        title3
info0        info1          info2         info3
img0         img1            img2         img3   
data0        data1           data2        data3
checkbox    checkbox      checkbox      checkbox
                                                 SUBMIT BTN
4
  • essentially, your loops are glorified var_dump's. I can't honestly say I see any attemt at structuring the output, let alone structure it in a way that adheres to the desired output, nor do you specify what about generating that output isn't working for you (descibe specifc problem) Commented Jul 29, 2013 at 11:38
  • OO would solve your problem wonderfully. If you wish to go the procedural way, you'll need a bit of "hack". You'll have to simulate entities... See yones safari's answer for an easy solution. I'd rather use some more sophisticated approach though... Commented Jul 29, 2013 at 11:43
  • @EliasVanOotegem - var_dump doesn't come supplied with checkboxes and submit buttons. Commented Jul 29, 2013 at 11:45
  • @PédeLeão: No, but the code the OP posted doesn't, either: echo "$data: $val <br />"; is practically the same as echo "$data => $val, <br />";, add an echo 'array (', count($_POST['data']), ') {<br/>'; before and after the loops and you must admit: it's pretty similar, isn't it? Commented Jul 29, 2013 at 11:47

3 Answers 3

4

Use this code:

$items = array();
foreach($_POST['data'] as $key => $val)
{
   $items[] = array(
      'data' =>$_POST['data'] [$key],
      'info' =>$_POST['info'] [$key],
      'img' =>$_POST['img'] [$key],
      'title' =>$_POST['title'] [$key] );
}
Sign up to request clarification or add additional context in comments.

Comments

1

Name your input fields like this

<input type="hidden" id="data[]" name="data1" value="SOMETHING HERE">
<input type="hidden" id="data[]" name="data2" value="SOMETHING BLAH BLAH">
<input type="hidden" id="data[]" name="data2" value="BLAH BLAH SOMETHING">

Go through them like this:

<?php
$num = 1;
$data = array();
while (true) {
    if (array_key_exists('data'.$num,$_POST)) {
        $data[] = $_POST['data'.$num];
    }
    else {
        break;
    }
}

print_r($data); //outputs all data values

I hope I got your question correctly ;)

Comments

1

You should first do some business logic to get the data layout you need, and then use your newly formatted data to do what it is you are doing, i.e.

$items = array();

foreach ($_POST['data'] as $data=>$val){
    $items[$data]['data'] = $val;
}
foreach ($_POST['info'] as $info=>$val){
    $items[$info]['info'] = $val;
}
foreach ($_POST['img'] as $img=>$val){
    $items[$img]['img'] = $val;
}
foreach ($_POST['title'] as $title=>$val){
    $items[$title]['title'] = $val;
}

var_export($items);

and as long as you are only posting these particular values, you can actually condence this down to:

$items = array();
foreach($_POST as $part => $ar) {
  if(is_array($ar) {
    foreach($ar as $idx => $val) {
      $items[$idx][$part] = $val;
    }
  } 
}
var_export($items);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.