无法访问ajax发送的php页面上的数据?

时间:2016-04-10 12:35:27

标签: php jquery mysql ajax

我有一个html表单,下面显示的代码我已经使用ajax将点击按钮的id发送到php页面,但我没有得到任何值的PHP页面,它发送null。 我是初学者,通过教程学习,有人能指出我正在做的错误吗?

   <!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;
 $.post("code.php", id);
});
});
</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>

我的php代码如下:

<?php
$servername = "localhost";
$username = "admin";
$password = "pwd";
$dbname = "testdb";


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

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

$num= (int)$_POST[id];
$str = $num;

$tempid = substr_replace($str,"T",0);
 echo $tempid;
$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 个答案:

答案 0 :(得分:1)

替换$ .post(“code.php”,id);与

var request = $.ajax({
                                url: "code.php",
                                type: "POST",
                                data: {id:id},
                                dataType: "html"
                                });



                     request.done(function(msg){
                       alert(msg);
                     });  

                      request.fail(function(jqXHR, textStatus) {
                                     alert( "Request failed: " + textStatus );
                      });