删除并添加到数据库中

时间:2012-01-01 21:39:13

标签: javascript mysql ajax

所以我需要一个函数,所以当用户点击X按钮时,它会删除按钮,并将其添加到名为like的数据库表中,因此当单击X按钮时,它会将其从页面中删除但不会更新有任何东西的mysql ......

继承我的脚本我需要它的页面.. 你会看到我的

<a href="#" class="close" onclick="removeElement('tbl','<? echo $site->facebook;?         >');">x</a>

我需要它来做这样的mysql更新

mysql_query("INSERT INTO `liked` (user_id, site_id) VALUES('{$x[1]}','{$site->id}')");

我怎样才能实现这一目标?在这个页面上..

<?php
include('header.php');
if(!isset($data->login)){echo "<script>document.location.href='index.php'</script>";        exit;}
?>
<body id="tab1"> 
<div>
<ul id="tabnav"> 
<li class="tab1"><a href="facebook.php">Earn Coins</a></li> 
<li class="tab2"><a href="addfb.php">Add Facebook Page</a></li> 
<li class="tab3"><a href="fbpages.php">Your Pages</a></li> 
</ul>
</div>
<h1>Facebook Fan Pages</h1>
 <?
  $site2 = mysql_query("SELECT * FROM `facebook` WHERE (`active` = '0' AND `points` >     '4') AND `id` NOT IN (SELECT `site_id` FROM `liked` WHERE `user_id`='{$data->id}') ORDER BY `cpc` DESC LIMIT 0, 15");
 $ext = mysql_num_rows($site2);
 if($ext > 0){
 ?><div id="fb-root"></div>
 <script>
 window.fbAsyncInit = function() {
 FB.init({status: true, cookie: true, xfbml: true});
 var user= "<? echo $data->id;?>";
 document.getElementById("Hint").style.display='block';
  FB.Event.subscribe('edge.create', function(response) {
 $.ajax({
    type: "POST",
    url: "fbreceive.php",
    data: "data="+response + "---" + user,        
    cache: false
  });
$("#Hint").html('<font size="3"><b>Liked with success!</b></font>');
removeElement('tbl', response);
   });
 };
 (function() {
    var e = document.createElement('script');
 e.type = 'text/javascript';
 e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
 e.async = true;
  document.getElementById('fb-root').appendChild(e);
  }());

 function removeElement(parentDiv, childDiv){
 if (document.getElementById(childDiv)) {     
      var child = document.getElementById(childDiv);
      var parent = document.getElementById(parentDiv);
      parent.removeChild(child);
 }
  }
  function refreshpage()
  {
  window.location.reload();
  }
   </script>
  <center><div id="Hint" style="display:none;"></div></center>
  <div id="tbl">
  <?
 for($j=1; $site = mysql_fetch_object($site2); $j++)
  {
 ?> 
 <div class="tbl tbl-facebook" id="<? echo $site->facebook;?>">
<a href="#" class="close" onclick="removeElement('tbl','<? echo $site->facebook;?     >');">x</a>
<div><fb:like href="<? echo $site->facebook;?>" send="false" layout="button_count"           show_faces="false" font=""></fb:like></div>
<div class="title"><a href="<? echo $site->facebook;?>" target="_blank"   style="color:blue;"><? echo $site->title;?></a></div>
<div class="points">Coins: <b><? echo $site->cpc;?></b></div>
   </div>
    <?}?>
   </div>
    <div class='infobox'>Like the pages and refresh page to see the addition of points!
  <form action='' method='' onsubmit='refreshpage();'>
 <input name='refresh' type='submit' value='Refresh'>
  </form></div>
  <?}else{?>
  <div class="msg_error">Sorry, there are no more coins to be earned at the moment.      Please try again later.</div>
   <div class="msg_success"><a href="buy.php"><b>Feel like you need more coins? You can    purchase them now!</b></a></div><?}?>
 <div class="clearer">&nbsp;</div>

    <? include('footer.php');?>

现在有一个包含文件,这里是

  <?
  include('config.php');
  if(isset($_POST['data'])){
   $x = explode('---', $_POST['data']);
  $site = mysql_fetch_object(mysql_query("SELECT * FROM `facebook` WHERE     `facebook`='{$x[0]}'"));
  if($site->id != ""){
  mysql_query("UPDATE `users` SET `coins`=`coins`+'{$site->cpc}' WHERE `id`='{$x[1]}'");
  mysql_query("UPDATE `facebook` SET `likes`=`likes`+'1', `points`=`points`-'{$site-    >cpc}' WHERE `facebook`='{$x[0]}'");
  mysql_query("INSERT INTO `liked` (user_id, site_id) VALUES('{$x[1]}','{$site->id}')");
  }}
  ?>

1 个答案:

答案 0 :(得分:0)

您可以使用ajax在幕后发出请求。

请参阅: http://www.w3schools.com/ajax/default.asp

使用javascript DOM编辑按钮/按钮。

请参阅: http://www.w3schools.com/jsref/dom_obj_element.asp


<div id="content"></div>
<script>
    function AjaxReq(){
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp1=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp1.onreadystatechange=function()
      {
      if (xmlhttp1.readyState==4 && xmlhttp1.status==200)
        {
        document.getElementById("content").innerHTML=xmlhttp1.responseText;
        }
      };
      xmlhttp1.open("GET","yourpage.php",true);
      xmlhttp1.send();  
    };
</script>