0

I have a dynamic table that can be edited inline or can have rows dynamically added. I want to be able to hit a save button that runs an UPDATE query to update the database. But I can't figure out how. I am really stuck on this and would appreciate any help.

Here is a codepen: http://codepen.io/anon/pen/yawyQQ

You can find most code in the codepen...I will provide some of my HTML/PHP code and Ajaxsubmit code.

HTML/PHP code:

<html>

    <head>
        <title>Stage Rebate Master HTML Table</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
         <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
         <link rel="stylesheet" type="text/css" href="html_master.css">
         <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <script type="text/javascript" src="html_master.js"></script>
    </head>

    <label id="table_name">Stage_Rebate_Master</label><br>

<body>    

<div id="dialog-form" title="Add Vendor">
  <p class="validateTips">All form fields are required.</p>

  <form>
    <fieldset>
      <label for="mr_name">Vendor</label>
      <input type="text" name="mr_name" id="mr_name" class="text ui-widget-content ui-corner-all">
      <label for="buyer_id">Buyer ID</label>
      <input type="text" name="buyer_id" id="buyer_id" class="text ui-widget-content ui-corner-all">
      <label for="poc_n">POC Name</label>
      <input type="text" name="poc_n" id="poc_n" class="text ui-widget-content ui-corner-all">
      <label for="poc_p">POC Email</label>
      <input type="text" name="poc_e" id="poc_e" class="text ui-widget-content ui-corner-all">
      <label for="poc_p">POC Phone</label>
      <input type="text" name="poc_p" id="poc_p" class="text ui-widget-content ui-corner-all">

      <!-- Allow form submission with keyboard without duplicating the dialog button -->
      <input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>
</div>



<div id="users-contain" class="ui-widget">
<table id="html_master" class="ui-widget ui-widget-content">
<thead>
    <tr class="ui-widget-header">
    <td>ID</td>
    <td>Vendor</td>
    <td>Buyer ID</td>
    <td>POC Name</td>
    <td>POC Email</td>
    <td>POC Phone</td>
    <td>Edit/Delete</td>
    </tr>
</thead>
<tbody>

<?php
    foreach ($dbh->query($sql) as $rows){
    ?>
    <tr>
        <td class="mr_id" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td>
        <td class="mr_name" contenteditable="false"><?php echo $rows['MR_Name']?></td>
        <td class="buyer_id" contenteditable="false"><?php echo $rows['Buyer_ID']?></td>
        <td class="poc_n" contenteditable="false"><?php echo $rows['MR_POC_N']?></td>     
        <td class="poc_e" contenteditable="false"><?php echo $rows['MR_POC_E']?></td>
        <td class="poc_p" contenteditable="false"><?php echo $rows['MR_POC_P']?></td>
        <td><input type="button" class="edit" name="edit" value="Edit">
        <input type="button" class="deactivate" name="deactivate" value="Deactivate"></td>
    </tr>
 <?php
  }
 ?>
</tbody>

    <input type="button" class="create-user" value="Add Row">
    <input type="submit" value="Save Table" class="save">

</table>
</div>

    <input type="button" class="create-user" value="Add Row">
    <input type="submit" value="Save Table" class="save">

</body>


</html>

Ajaxsubmit code:

<?php

$host="xxxx"; 
$dbName="xxxxxxx"; 
$dbUser="xxxxx"; 
$dbPass="xxxxxxxxxxxx";

$dbh = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );


$data = $_POST['data'];
foreach($data as $row){
    $sql = "UPDATE Stage_Rebate_Master SET MR_Name='$row[mr_name]',
    Buyer_ID='$row[buyer_id]',
    MR_POC_N='$row[poc_n]',
    MR_POC_E='$row[poc_e]',
    MR_POC_N='$row[poc_p]'
    WHERE MR_ID='$row[mr_id]'";

    $dbh->query($sql);
}

?>
3
  • Have you documented about ajax? Commented Oct 24, 2016 at 19:15
  • I have read up on it a little bit, but have never used it enough to know how to use it with this...just need something quick Commented Oct 24, 2016 at 19:17
  • check my answer and if anything not clear ask me please Commented Oct 24, 2016 at 19:24

2 Answers 2

1

You can use jquery or javascript to post and update data

jquery code :

$.post("save2db.php",{
  id:"id",
  vendor:"vendor_name"
});

id : $_POST['id']

vendor : $_POST['vendor']

save2db.php will be:

    if(isset($_POST['vendor'])){
       $vendor=$_POST['vendor'];
       $id=$_POST['id'];
     //your update sql query
    }
Sign up to request clarification or add additional context in comments.

2 Comments

so what would the ajaxsubmit page look like, or in your case, the save2db page look like?
do you get any error ??,check this site w3schools.com/jquery/jquery_ajax_get_post.asp
0

If you just want it to happen when you click on an 'update' button, you could try using an

if(isset($_POST["Update"]))
{
 //PDO statement to update the database
}

This will only action if the Button with the name 'Update' is pressed within the form.

2 Comments

so would this go in the ajax submit page??
It would go on the main page where the 'update' button would be. It would be server side, so it would require the page to be reloaded. It's kind of like a validation script saying you submitted the form with value update, so the following actions.

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.