weixin_33711647 2016-04-11 18:00 采纳率: 0%
浏览 31

PHP-Ajax数据迁移问题

Not able to figure out why my ajax code is not sending the data to the php file. have tried many things but it doesn't seem to work. can anybody help me out here please. My html code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ABC Fashion</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script type = "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">    </script>
<script>
$(document).ready(function(){
$("button").click(function(){
 alert((this).id);
 var id=(this).id;
 var params ={id : (this).id};
$.post("code.php", params);
  alert(1);
});
});
</script>
</head>
<body>
<div id="main_container">
<div class="center_content"  align="center">
  <div class="center_title_bar"  align="center">Latest Products</div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">Levi's Men's Cotton T-Shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T1.jpg" name="tshirt" alt="" border="0" id="T1"/></a></div>
       <div class="product_img"><button id="d1" onclick="">Dislike</button></div>
       <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">UB Stylish T-shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T2.jpg" name="tshirt" alt="" border="0" id="T2"/></a></div>
       <div class="product_img"><div class="product_img"><button id="d2" onclick="">Dislike</button></div></div>
      <div class="prod_price"><span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">Jacob n John T-Shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T3.jpg" name="tshirt" alt="" border="0" id="T3"/></a></div>
       <div class="product_img"><div class="product_img"><button id="d3" onclick="">Dislike</button></div></div>
      <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">Levi's Men's Cotton T-Shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T4.jpg" alt="" border="0" /></a></div>
       <div class="product_img"><div class="product_img"><button id="d4" onclick="">Dislike</button></div></div>
      <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">UB Stylish T-shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T5.jpg" alt="" border="0" /></a></div>
       <div class="product_img"><div class="product_img"><button id="d5" onclick="">Dislike</button></div></div>
      <div class="prod_price"><span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">Jacob n John T-Shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T6.jpg" alt="" border="0" /></a></div>
       <div class="product_img"><div class="product_img"><button id="d6" onclick="">Dislike</button></div></div>
      <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">Levi's Men's Cotton T-Shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T7.jpg" alt="" border="0" /></a></div>
       <div class="product_img"><div class="product_img"><button id="d7" onclick="">Dislike</button></div></div>
      <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">UB Stylish T-shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T8.jpg" alt="" border="0" /></a></div>
       <div class="product_img"><div class="product_img"><button id="d8" onclick="">Dislike</button></div></div>
      <div class="prod_price"><span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">Jacob n John T-Shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T9.jpg" alt="" border="0" /></a></div>
       <div class="product_img"><div class="product_img"><button id="d9" onclick="">Dislike</button></div></div>
      <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">Levi's Men's Cotton T-Shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T10.jpg" alt="" border="0" /></a></div>
       <div class="product_img"><div class="product_img"><button id="d10" onclick="">Dislike</button></div></div>
      <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">UB Stylish T-shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T11.jpg" alt="" border="0" /></a></div>
       <div class="product_img"><div class="product_img"><button id="d11" onclick="">Dislike</button></div></div>
      <div class="prod_price"><span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div>
  <div class="prod_box">
    <div class="top_prod_box"></div>
    <div class="center_prod_box">
      <div class="product_title"><a href="details.html">Jacob n John T-Shirt</a></div>
      <div class="product_img"><a href="details.html"><img src="images/T12.jpg" alt="" border="0" /></a></div>
       <div class="product_img"><div class="product_img"><button id="d12" onclick="">Dislike</button></div></div>
      <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div>
    </div>
    <div class="bottom_prod_box"></div>
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div>
  </div> 
 </div>
 </body>
 </html>

and my php code is as follows:

 <?php


$servername = "localhost:3306";
$username = "admin";
$password = "password";
$dbname = "testdb";


$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
die("Connection failed: " . mysqli_error($conn));
} 

 $num= (int)$_POST["params"];
 $str = $num;
 $tempid = substr_replace($str,"T",0);
 echo $str;
$sql = "INSERT INTO `test` (`disliked`) VALUES ('$num')";
if (mysqli_query($conn, $sql) === TRUE) {
echo "New record created successfully";
} 
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>
  • 写回答

1条回答 默认 最新

  • weixin_33728268 2016-04-11 18:10
    关注

    You have in your code var params ={id : (this).id}; which is wrong. Should be var params ={id : $(this)[0].id}; (And change all instances of (this).id to $(this)[0].id)

    And you're passing the id parameter, so on the server side, change $_POST["params"]; to $_POST["id"];

    $num= $_POST["params"]; // REMOVE(int) casting, you're doing it on the next line
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?