Comy
Comy

Reputation: 91

AJAX PHP UPDATE dynamically

I'm trying to make a form that UPDATEs my table with PHP and Ajax dynamically not just using PHP. The form works everything is fine exept I have no idea how to implement the Ajax stuff. If you could give me a hand or usefull informations related to my problem that would be great.

PHP file: item.php?item=Staff

<input type="submit" name="submit"  onClick="UpdateRecord('Staff');" value="Unquip" />

Javasript Code:

<script type="text/javascript">
function UpdateRecord(item)
  {
      jQuery.ajax({
       type: "POST",
       url: "item.php",
       data: 'item='+item,
       cache: false,
       success: function(response)
       {
         alert("Record successfully updated");
       }
     });
 }
    </script>

I'm still missing something important for sure.

Thank you

Upvotes: 0

Views: 895

Answers (4)

I see you're using jQuery already and presumably are getting the alert to notify you that your call has been made?

If that's the case, there are a few different options for you to update your table:

1: Update the entire table

You can do this either with PHP (and have it generate the HTML for you, which I wouldn't recommend actually, something with separating your logic from your view); but it's a viable option if you're already using templates.

2: Return an object that contains all the information your table would need

And then use Javascript/jQuery to handle this information and re-populate or update your table with the latest information. You can do this more easily by already adding usable ID's to your rows and cells so you can specifically target certain parts of your table to update them.

3 Rebuild the entire table using Javascript

This is easier than #2, but surely takes a lot more time on the client because of the redrawing. Especially when the table contains a whole lot of rows which all need to be rebuild/recalculated.

I would suggest you look into doing #2, where you use id="item-3214325643" to define a row (<tr>) and have Javascript run through the results of your PHP (which should simply contain the updated results after handling the input) to keep your table updated.

I hope this helped you out! If you need any specific advice, don't hesitate to contact me :-) or reopen another, more specific question.

Upvotes: 1

MrKekson
MrKekson

Reputation: 740

If i want to send it to a client then I'm usually doing it with JQuery:

$.ajax({
    type:'POST',
    url: 'JQuery.php',
    data: 'Action=foo&val=' + variable ),
    async: true,
    success: function(msg){ do something }
})

Put it in a javascript function, and create a callback loop, or a timer

t=setTimeout("yourfunction( )",60000);

so it will update time to time. You can also set it up in a way that it's sending request to the server, with a huge timeout, and if something happens during the timeout, the server will respond, if im right gchat/fbchat works this way.

Also this bit of script shuld run on the cliend side, because servers can't start a request/post from the client browser, so i'm not shure if that is even possible with pure php.

If you want to request a fev pages, then summarize it on the server side, then CURL.

Upvotes: 1

innovative kundan
innovative kundan

Reputation: 631

This is jquery ajax easy to implement ajax.


$.ajax({
  type: "POST",
  url: "item.php",
  data: { name: "John", location: "Boston" }
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

and in item.php take value as $_POST name or location and all the data you need to and 
update as you want. like
if(isset($_POST['name']))
{
// write code that you want,
}

you can send your data as GET or POST as you want hope it will help you.

Upvotes: 0

bmurauer
bmurauer

Reputation: 1279

if you can, i highly recommend looking into a javascript framework like jQuery, which makes the Ajax part much easier. You then don't have to handle the XMLHttpRequests yourself, but just call the jQuery function.

Some good examples for the function can be found in the jQuery docs

also GET and POST are described well, which are more specific versions of the ajax function.

Upvotes: 1

Related Questions