Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
9.7k views
in Technique[技术] by (71.8m points)

php - How to update a mysql database without reloading page

thanks for looking. I have a very long list of items, users click on an image (a plus sign) to add the item to their personal list. At the moment when they click the + it loads a "add-item.php?itemid=*" which processes the below code then redirects them to their own list, I did have it redirecting back to the global list but then it was unclear to the user if the item was added to their list. how would I go about making it all update the database without going anywhere, im thinking javascript but have never written any. Any help would be brilliant!! :)

<?php 
ini_set('display_errors', 'On');
error_reporting(E_ALL);

 $bucketlist=MYSQL_QUERY( "SELECT * FROM membersbuckets where userid = $userid AND bucketid = $bucketid")
 or die(mysql_error());  

          $bucketlist=mysql_fetch_array( $bucketlist ) ;

            if($bucketlist < 1) {

            mysql_query("INSERT INTO membersbuckets (memberbucketid, userid, bucketid, complete)
            VALUES ('', '$userid', '$bucketid', '0')");
            echo "Adding item to your bucketlist...";
            echo "<meta http-equiv='Refresh' content='2; URL=mybucketlist.php'/>";
            }
            else {
            echo "This item is already on your list, redirecting you to your list";
            echo "<meta http-equiv='Refresh' content='2; URL=mybucketlist.php'/>";
            }
?> 

Thank you in advance! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need AJAX, as everyone has said.

Since you have never written any javascript, here is a guide for you.

Instead of your

<a href="add-item.php?itemid='.$itemId.'" > Add Item </a>

Write

<a onclick="addItemToUsersList('.$itemId.')" > Add </a>

For AJAX, use jquery as Angelo has suggested. Download it and add the following

<script type="text/javascript" src="http://path/to/jquery-latest.min.js"></script>
<script type="text/javasript">
function addItemToUsersList(itemId)
{
  $.ajax({
    'url': 'path/to/add-item.php', 
    'type': 'GET',
    'dataType': 'json', 
    'data': {itemid: itemId}, 
    'success': function(data) 
    {
      if(data.status)
      {
        if(data.added)
        {
          $("span#success"+itemId).attr("innerHTML","Item added to your personal list");
        }
        else
        {
          $("span#success"+itemId).attr("innerHTML","This item is already on your list");
        }
      }
    },
    'beforeSend': function() 
    {
      $("span#success"+itemId).attr("innerHTML","Adding item to your bucketlist...");
    },
    'error': function(data) 
    {
      // this is what happens if the request fails.
      $("span#success"+itemId).attr("innerHTML","An error occureed");
    }
  });
}
</script>

And then finally, in your path/to/add-item.php file write the code to add the items. The parameter itemId will be available here as $_GET['itemId']. Just return proper status values using json_encode.

if($bucketlist < 1) 
{
  mysql_query("INSERT INTO membersbuckets (memberbucketid, userid, bucketid, complete) VALUES ('', '$userid', '$_GET['itemId]', '0')");
  return json_encode(array("status" => true, "added" => true));
}
else
{
  return json_encode(array("status" => true, "added" => false));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...