0

How can I collect the data on the row from the table that I select and use it in the result?

Here is the javascript I am using to show the data entry screen, once the function has been called by selecting the row. Now I just need to design a form in PHP that will include (1) some of the data from the row selected and (2) some new data that will be collected.

Here is the Javascript to select the row and call the data entry form

$(document).ready(function () {
    $("tr").live('click',function(){
        $.post('data_entry_form.php', function(data) {
            $('#section2').html(data);
        });
    });
});

Here is the PHP Script

<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif ORDER BY aif_id DESC");
$result->setFetchMode(PDO::FETCH_ASSOC);

echo "<table id=\"all_aifs\">";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
echo "</tr>";

foreach($result as $index => $row) {
echo "<tr>";
echo "<td>$row[fee_source_id]</td>";
echo "<td>$row[company_name_per_sedar]</td>";
echo "<td>$row[document_filing_date]</td>";
echo "<td>Placeholder</td>";
echo "</tr>";
}

echo "</table>";
echo "<br>";
$dbh = NULL;
?>
Ben
  • 1,013
  • 4
  • 16
  • 34
  • I am a little confused, do you need to know how to get data from a table row? – Dom Jan 01 '13 at 20:46
  • Hi @Dom yes I need to be able to click on a table row which will load a form (I have been able to load a generic form) but I want the form to be associated with the row that I clicked and when I submit the form, I would like the data to be inserted into that row of the db. – Ben Jan 01 '13 at 20:49
  • Can you provide a jsfiddle of what you currently have? – Dom Jan 01 '13 at 20:51
  • Why don't you pass the record ID to the ajax call, and simply fetch the correct data on the back-end? If you're already making the ajax call, there's no reason not to just give it a quick lookup. – Dutchie432 Jan 01 '13 at 21:16
  • @Dutchie432 that sounds like exactly what I need to do, but easier said than done... Any chance you could show me how that is done? – Ben Jan 01 '13 at 21:19
  • @Dutchie432 I have checked your answer, and most of it makes sense to me, except the part where you say "Then your ajax page would just read $_REQUEST['id'] to get the id of the form being edited." - what ajax page are you talking about? I have an html file, a javascript file, and a couple PHP files (one for the table on the left and one for the form on the right). – Ben Jan 04 '13 at 16:12
  • If your form is inside of an HTML `data_entry_form.html` The HTML file your form is is should be renamed to a PHP file `data_entry_form.php`. Then put some PHP code into it to fetch the appropriate data as described in my comment. – Dutchie432 Jan 04 '13 at 16:56

2 Answers2

0

Within the event handler, this and $(this) refer to your selected row:

$("tr").live('click',function(){
    // this.cells[1] is the cell that contains Pubco Name
    // You can also use $(this).children("td")[1]
    ...
});
Christophe
  • 27,383
  • 28
  • 97
  • 140
  • it seems to me that it would be a lot easier to use the DOM rather that read from the db? – Ben Jan 03 '13 at 09:31
  • @BenJones I'm not sure but I think you meant to post this comment under my answer. Let me ask you a question. What happens if the data is edited by user2 while user1 is viewing the page? If user1 clicks an edited row and the update is done using the DOM, user1 will not see the edits made by user2. Using ajax also ensures the form has the most recent values. When you say "Easier" - it may be easier to program, but won't result in the best solution. `Easier to code` rarely does. – Dutchie432 Jan 03 '13 at 10:55
  • 1
    Do you expect me to retype my answer for the same question in another location? I'm not sure what you're getting at. Also the `live()` function doesn't indicate that the "control is live". Try googling it for crying out loud. – Dutchie432 Jan 03 '13 at 11:14
0

The "Correct" answer to this problem is NOT to read from the DOM. Never a good idea. I suggest you pass the record id to the ajax call and have the ajax call return an already-populated form.

//PHP
//place the data attribute in the tr
echo "<tr data-recordId='".$row['id']."'>";


//JS
$(document).ready(function () {
    $("tr").live('click',function(){

        //Get the ID from the row clicked
        var id = $(this).data('recordId'); 

        //short-hand
        $('#section2').load('data_entry_form.php?id='+id);

    });
});

Then your ajax page would just read $_REQUEST['id'] to get the id of the form being edited.

//Ajax side PHP
$id = (int)$_REQUEST['id'];
//Fetch data and use it to pre-populate form item

You would pre-populate your inputs like this

<input type="text" value="<?php  echo $row['value']; ?>" />

or

echo '<input type="text" value="'.$row['value'].'" />';

NOTE: If your values contain quotes, you will want to replace them with the code &quot;

echo '<input type="text" value="'.str_replace('"', '&quot;', $row['value']).'" />';
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
  • Could you explain why reading from the DOM is not a good idea in this case? – Christophe Jan 01 '13 at 21:38
  • Well, can you explain to me why you would fetch an empty form via ajax and then populate after the fact? It's standard practice. What if the form contains fields/data that aren't shown in the table? – Dutchie432 Jan 02 '13 at 13:54
  • I am just pointing out the difference between "standard practice" vs. "never a good idea". To answer your question: I often do it myself when fields need to be populated with context data from multiple sources (tables, cookies, other ajax calls). Of course, scraping the row won't help if the data is not in the table - but that's not the case here. – Christophe Jan 02 '13 at 17:22
  • It's "never a good idea" to go against standard practice. That's why it's called "Standard" – Dutchie432 Jan 02 '13 at 19:44
  • I "accidentally" downvoted, and when I noticed stackoverflow didn't allow me to take it back (unless there's an edit to the answer). – Christophe Jan 02 '13 at 23:52
  • @Dutchie432 can you please explain the AJAX side? – Ben Jan 04 '13 at 13:35
  • @Dutchie432 I have read that the "live" method is deprecated and should not be used anymore... – Ben Jan 04 '13 at 16:13
  • That's actually correct. It will still work, and I used it in because it in your code. You'll want to look at the jQuery `$.on()` function. Same basic idea. On the ajax side, the PHP page will need to read the id from the `.load(...)` statement like `$id=$_REQUEST['id'];`. Then use that ID to fetch the information you need for that row to be pre-populated into the form. Once you have it, simply place the values into the form you already have. – Dutchie432 Jan 04 '13 at 16:53
  • hmm I'm still having issues... I've successfully implemented the PHP part where it assigns a data attribute to each row, but still can't seem to populate the form... I will paste my updated code above. – Ben Jan 04 '13 at 17:30