Link to home
Start Free TrialLog in
Avatar of Genesis5150
Genesis5150

asked on

How to get the data from an addRow() function using php

The question I have is this I have a form that allows the user to add a row for inputting more bedrooms. Let's say the user adds an extra 2 rows how do I extra the data from the form to insert into a database. below is an example of the form.

Thanks

<?php
// FOR TESTING
$bedsize = array (
  'Single','Double','Queen','King','Helipad'
);
?>
<!doctype html>
<html>
<head>
<title>Test</title>
<link href="css/bootstrap.css" rel="stylesheet" />
</head>
<body>
<table class="table" id="addbeds">
  <tbody>
    <tr>
      <td>
        <p align="center">Bedroom</p>
      </td>
      <td>
        <p align="center">Comment</p>
      </td>
      <td></td>
    </tr>
    <tr>
      <td>
        <input type='text' name='bedroom[]' title='Please give this bedroom a name. ie Bedroom 1' maxlength='65,535' placeholder="Name this bedroom" class='input-small' value='<?php echo (isset($data['bedroom'])) ? $data['bedroom'] : '' ?>' data-toggle="tooltip" data-placement="right" />
      </td>
      <td>
        <input type='text' name='comment[]' title='Comment on this bedroom' maxlength='65,535' placeholder="What to find on this room." class='input-medium' value='<?php echo (isset($data['comment'])) ? $data['comment'] : '' ?>' data-toggle="tooltip" data-placement="right" />
      </td>
      <td>
        <select name="bedsize[]" title="bedsize" class="input-medium">
<?php foreach($bedsize as $key => $value): ?>
          <option value="<?php echo $key ?>"<?php echo (isset($data['bedsize']) && $data['bedsize'] == $key) ? ' selected="selected"' : null ;?>><?php echo $value ?></option>
<?php endforeach ?>
        </select>
      </td>
    </tr>
  </tbody>
</table>
<table style="WIDTH: 100%; BORDER-COLLAPSE: collapse">
  <tr>
    <td>
      <p align="center">
        <input type="button" value="Add Row" onclick="addRow()" />
        <input type="button" value="Subtract Row" onclick="delRow()" />
      </p>
    </td>
  </tr>
</table>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
function addRow()
{
  var nr = $('#addbeds tr:last-child').clone();
  $('#addbeds tbody').append(nr);
}

function delRow() {
console.log($('#addbeds tbody tr').length);
  if ($('#addbeds tr').length > 2) {
	$('#addbeds tr:last-child').remove();
  }
}
</script>
</body>
</html>

                                        

Open in new window

Avatar of Paul Wilson
Paul Wilson

hiii,

you can allow users to add as many as  row by creating dynamic form , and you can use PHP scripts to fetch the data  from dynamically added fields. Follow this article to implement this functionality: http://techstream.org/Web-Development/PHP/Dynamic-Form-Processing-with-PHP
As the user adds/deletes each row they execute a function. This is termed script. You can add as they are adding because it is using the php bedsize to add rows.

You need to know what the tables in your database need. How many columns, what kinds of values. You can ( Insert ) into the table using the php if you have the additional data needed. An SQL statement is pretty easy to construct itself after you know what the table needs. You can even google terms. But you will need the table name and the values for sure first.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Slight amendment to the AddRow. The current version copies the contents of the input's when you duplicate the row
function addRow()
{
  var nr = $('#addbeds tr:last-child').clone();
  // CLEAR INPUTs IN CLONE
  $('input', nr).val('');
  $('#addbeds tbody').append(nr);
}

Open in new window