0

thank you for your answer meanwhile i got this code for inserting the multiple data, and it really works but my problem is it adds the fisrt column then after finished then it executes the second column i want them to excecute insert f1 then f2 then f1 then f2 again: just imagine i have 4 input with same name f1 and f2

Inserting multiple entries into table from the same form please refer on this post

1
  • There's multiple things wrong here, but most importantly, none of your form fields have a name attribute, and that attribute will be needed to pass information to your PHP script to be used in MySQL. Commented Dec 24, 2013 at 6:35

4 Answers 4

1

Your are missing names in all of your form fields

/*PHP code where the form submit, and repeat the same for other fields like descriptionField*/



 <?php
   for($i = 0; $i <= count ( $_POST ['dateField'] ); $i ++) {
    // Do what ever you want with data
    var_dump ( $_POST ['dateField'] [$i] );
    }

    ?>

HTML Code, names added

    <tr class="item-row">
        <td class="item-name"><div class="delete-wpr">
                <textarea>Date</textarea>
                <a class="delete" href="javascript:;" title="Remove row">X</a>
            </div></td>

        <td class="description"><textarea name="descritpionField[]">Description</textarea></td>
        <td><textarea name="dateField[]" style="text-align: center;"
                class="asd">0</textarea></td>
        <td><textarea name="dateField[]" style="text-align: center;"
                class="qty">0</textarea></td>
        <td><textarea name="dateField[]" style="text-align: center;"
                class="cost">0</textarea></td>
        <td style="text-align: center;"><span class="price">0</span></td>
    </tr>
Sign up to request clarification or add additional context in comments.

Comments

0

If i understand correctly

If this fields in form, then set 'name' attr to fields like name="user[]" When u'll submit the form, all values of "user[]" fields will in array. Just do print_r() and you will see, what i try to explain

Also u can do it in javascript. . . Just grab all values in array. . .

User "Noor" already post example for you

Comments

0

Please try this code,

  <?php

  echo "<pre>";
  if(isset($_POST['submit'])){

      print_r($_POST['data']);//RESULT WILL GET AN ARRAY

  }

  ?>

  <form action="" method="post">
  <table>
  <?php
  //$items = // DATA FROM DB
  //$items_count = count($items);
  $items_count = 5;
  for($i=0;$i<$items_count;$i++) {
      ?>
      <tr class="item-row-<?php echo $i;?>">
          <td class="item-name"><div class="delete-wpr"><textarea name="data[<?php echo $i;?>]['date']">Date</textarea><a class="delete" href="javascript:;" title="Remove row">X</a></div></td>
          <td class="description"><textarea name="data[<?php echo $i;?>]['description']">Description</textarea></td>
          <td><textarea name="data[<?php echo $i;?>]['age']" style="text-align:center;" class="asd">0</textarea></td>
          <td><textarea name="data[<?php echo $i;?>]['dob']" style="text-align:center;" class="qty">0</textarea></td>
          <td><textarea name="data[<?php echo $i;?>]['status']" style="text-align:center;" class="cost">0</textarea></td>
          <td style="text-align:center;" ><span class="price">0</span></td>
      </tr>

  <?php } ?>
  <table>
  <input type="submit" class="button" name="submit" value="submit" />
  </form>

Comments

0

Answer fo the post (Trouble with $_POST [duplicate])

I don't know if I understand your concern. I think you are trying to create a quiz. And so the user must validate several attempts. your problem is that you can not accumulate the different answers following a table. so here is a solution.

<?php

$good_answers = array(
    "easy1" => array("4","3","5","2","6","9","7","8","1" ),
    "easy2" => array("6","8","2","5","7","1","4","9","3" ),
    "easy3" => array("1","9","7","8","3","4","5","6","2" ),
    "easy4" => array("8","2","6","1","9","5","3","4","7" ),
    "easy5" => array("3","7","4","6","8","2","9","1","5" ),
    "easy6" => array("9","5","1","7","4","3","6","2","8" ),
    "easy7" => array("5","1","9","3","2","6","8","7","4" ),
    "easy8" => array("2","4","8","9","5","7","1","3","6" ),
    "easy9" => array("7","6","3","4","1","8","2","5","9" )
);

if(isset($_POST['row'])){
    $easy = false;
    $client_responses = $_POST['row']; // EX: [" "," "," " ,"2","6"," " ,"7"," " ,"1"]
    $old = json_decode($_POST['old']);
    $old[] = $client_responses;
    // Or make    array_push($old,$client_responses);     if you prefere
    foreach ($good_answers as $easy => $responses) {
        if($client_responses === $responses){
            $easy = $responses;
            break;
        }
    }

    // generating table for HTML of client responses
    echo '<table>';
    // saving old responses
    echo '<input type="hidden" value="'. json_encode($old) .'" name="old">';
        foreach ($old as $number => $row) {
            echo '<tr id="row'. $number .'">';
            
            for ($i=0; $i < count($row); $i++) {
                echo '<td class="cellTop">';
                    echo '<input type="text" maxlength="1" name="row" value="'. $row[$i].'"/>';
                echo '</td>';
            }

            echo '</tr>';
        }
    echo '</table>';
}

1 Comment

Hi! You can flag posts as duplicate if you think that it already have an answer in SO.

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.