views:

2682

answers:

6

I'm trying to reload a table which was also generated by PHP.

The table has an ID: #bookmarks

After the user pressed a button, the table should reload the content + the data they have just added. I'm a bit confused because I don't know how to send all of the data from a PHP result.

Thanks, James

A: 

When the user presses the button you should call the code that was used to load the table. The data might be the data from the beginning + the data the users added. Or the data might be reretrieved from the server, depending on how your application works.

A bit more info might clear things up, so we could give more specific answers.

Natrium
+1  A: 

In point form:

  • Submit the user's data back to a PHP page via an AJAX request. (Look at the jQuery Form plugin)
  • The PHP page should accept and validate the data, insert it into the database and then send a response back to the page in some format (I recommend JSON, using the php function json_encode.
    • The response should either be a "rejected" or an "accepted" with the user's data returned to them, cleaned up as required by your own system.
  • Then in the AJAX success callback method, use jQuery to append the data into the table, or give them a message telling them why it was rejected.
nickf
I don't use JSON to generate my table, I find it an extra step which can be skipped easily.
James Brooks
so you generate it on the server side in PHP? I don't see there being much difference there, except: a) it's more data to send, and b) you're putting presentation logic (html tags) in your data layer (php).
nickf
+2  A: 

This is assuming your PHP returns a ready-to-inject HTML code for the table:

$("#update_button").click(function(){
    $("#mytable").load("/tools/getTable.php")
})

in your page you need a DIV placeholder like this:

<div id="mytable"></div>

and your getTable.php needs to echo back html like this:

<table>
  <tr>
    <td>col1<td>
    <td>col2<td>
  <tr>
</table>
duckyflip
I can't really do that because there are other functions which require the table to be there.
James Brooks
James but the table will be there, even on the first request, you can do <?PHP include("getTable.php") ?> inside the file and when you open the page your table will be loaded, and using the AJAX it will be fetched dynamically
duckyflip
Of course! Thanks duckyflip!
James Brooks
A: 

Well the table is generated using this:

<table id="bookmarks">
  <thead>
   <tr class="table-top">
    <th>Thumbnail</th>
    <th>Title/Description</th>
    <th>Tags</th>
    <th>Action</th>
   </tr>
  </thead>
  <?php
  $hID = userToID($_SESSION['username']);
  $hQuery = mysql_query("SELECT * FROM linkz WHERE userid='$hID'") or die(mysql_error());
  while($hRow = mysql_fetch_array($hQuery)) {
   echo "<tr class='link'><td><img src='http://www.thumbshots.de/cgi-bin/show.cgi?url=".$hRow["location"]."' /></td><td><a href='share/".$hRow['shareid']."'>".$hRow["title"]."</a> - <i>". $hRow["description"]."</i><br /><b>Share Link:</b> http://www.linkbase.us/share/".$hRow['shareid']."&lt;/td&gt;&lt;td&gt;".$hRow['tags']."&lt;/td&gt;&lt;td&gt;&lt;a href='#' id='".$hRow['shareid']."' class='delbutton'>Delete</a></td></tr>";
  }
 ?>
 </table>
James Brooks
okay, I think you'll need a different way of working if you want to ajaxify your table with jQuery.
Natrium
Thanks Natrium!
James Brooks
A: 

For specific help using jQuery, check out the jEditable plugin which is designed to provide the ability to edit data in place. There's also instructions on how to collect the data and save it.

Tony Miller
Editing is done, it's just the reload of the table.
James Brooks
I've used jEditable thank you.
James Brooks
A: 

You'll need to look into the basics of AJAX first.

You'll need to learn how to walk before you can start running.

Search for some tutorials on Ajax, PHP & Ajax

Here and here are 2 to start with

Natrium
Everything is entered fine into the table, even deletion works!What about reloading the whole page? Will location.reload() work even after an AJAX post?
James Brooks
location.reload() might work. I don't really know that.So if I'm correct, you are posting the data to the server using ajax. But when retrieving, you don't use ajax. Right?If you retrieve the data with Ajax, the whole reloading-part might be a load easier, and cleaner than just performing a document.reload()
Natrium