4

I'm trying to save a html table rows into php array and then save the array in database.

<form action="" method="post">
        <table class="widefat" id="theTable">
                        <thead>
                                <tr>
                                   <th>Level Identifier</th>
                                    <th>Non-logged in message</th>
                                    <th>Logged in message</th>
                                </tr>
                        </thead>
                        <tbody>

                                  <tr>
                                    <td><input type="text" value="" style="height: 19px;background-color: white;font-size:10px;"/></td>
                                    <td><textarea style="font-size:10px;" name="promo_msg_free" cols="43" rows="1">This is your custom message template</textarea></td>
                                    <td><textarea style="font-size:10px;" name="promo_msg_free" cols="43" rows="1">This is your custom message template</textarea></td>
                                  </tr>

                                   <tr>
                                    <td><input type="text" value="" style="height: 19px;background-color: white;font-size:10px;"/></td>
                                    <td><textarea style="font-size:10px;" name="promo_msg_free" cols="43" rows="1"></textarea></td>
                                    <td><textarea style="font-size:10px;" name="promo_msg_free" cols="43" rows="1"></textarea></td>
                                  </tr>

                        </tbody>    
                    </table> 
                </form>

How can i retrieve each row data and save it to array element and finally I would be saving the array to db? Thanks

Irfan
  • 4,882
  • 12
  • 52
  • 62
  • 1
    What have you tried so far? Any attempts made to parse the HTML? Any attempts to write to the database? What kind of database? Any libraries you prefer/can use? – Ryan Mar 19 '13 at 18:18
  • Your question is not clear. Do you want to save the values of each table row, or just the values in the inputs and text areas ? – SteveP Mar 19 '13 at 18:19
  • why would u use tables to layout form elements in the first place? Use div/css – GGio Mar 19 '13 at 18:33
  • The rows would be dynamic. This is what I'm trying to accomplish: http://screencast.com/t/0T3I9jglD4K – Irfan Mar 19 '13 at 18:39
  • Voted to close this question - it really did not give enough information about the OP's specific needs, or what he had tried already, and was too open-ended. – thomasrutter Mar 20 '13 at 03:20

2 Answers2

7

IF YOU WANT TO SAVE HTML OF EACH ROW DO:

Use JQuery.

var rowsArray = {};
var i = 0;
$('#theTable tr').each(function({
    rowsArray[i] = $(this).html(); // if you want to save the htmls of each row
    i++;
});

then use ajax to post this data

$.ajax({
   type: 'post',
   url: URL_TO_UR_SCRIPT,
   data: { myarray : rowsArray },
   success: function(result) {
     //ur success handler OPTIONAL
   }
});

In PHP side you do:

$array = isset($_POST['myarray']) ? $_POST['myarray'] : false;
if ($array) { 
  $array = serialize($array);
  //UPDATE YOUR DATABASE WITH THIS SERIALIZED ARRAY
}

you cant save php array into database therefore you need to serialize it and when you retrieve it from DB use unserialize()

IF you meant that you wanted to save the input and text area values then you need to set the names for each of the element and then access them in your script using $_POST.

 $array = array;
 foreach($_POST as $key => $value) {
    //sanitize your input here
    $array[$key] = $value;
 }
 $serialized = serialize($array);
 //save serialized array in your DB

NOTE/HINT: FYI do not use html table to lay out the form elements. Tables should be used for data representation. You could easily do samething using divs and css

GGio
  • 7,563
  • 11
  • 44
  • 81
  • FYI, the rows are dynamic. Please have a look of this screencast and you will get what I'm trying to accomplish.http://screencast.com/t/0T3I9jglD4K – Irfan Mar 19 '13 at 18:38
  • does not matter they are dynamic or not you could make it dynamic using div/css. Tables should be used to display statistical data and not to lay things out nicely. read this: http://stackoverflow.com/questions/83073/why-not-use-tables-for-layout-in-html – GGio Mar 19 '13 at 18:55
0

This is basic PHP usage. You give your inputs names, an when you submit your form, your script in the submitted page will do the job.

Your values will reside in the

$_POST 

array. So you access them by

$_POST['input_name']

you will have to go through each value by calling its name and then put it in the database accordingly.

mavili
  • 3,385
  • 4
  • 30
  • 46
  • The problem is, the rows are dynimic. I'm not sure how many rows would be there. So I can not name them. – Irfan Mar 19 '13 at 18:33
  • then you could do jquery as i mentioned in my answer just alter it little bit. $('input').each({}); $('textarea').each({}); etc... – GGio Mar 19 '13 at 18:36