I have a html form with code shown below in which i have used ajax to send the id of the clicked button to the php page, but i am not getting any value to the php page, it is sending null. I am a beginner in this and have studied via tutorials, can anybody point out the mistake i am doing?
我有一个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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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=[ ] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[ ] 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>
My php code is as follows:
我的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);
?>
3 个解决方案
#1
1
The issue is in the $.post
call, you're passing the value of id
with no identifier.
问题在于美元。post调用,传递没有标识符的id值。
Change this:
改变:
$.post("code.php", id);
to this:
:
$.post("code.php", {id: id});
#2
1
Remember we need to send Key=>value pair.
记住,我们需要发送Key=>值对。
You are directly passing value in your Ajax post method so key is missing and you are trying to access using key.
您直接在Ajax post方法中传递值,因此丢失了key,并试图使用key进行访问。
Var params = {id:id};//first Id is key : second id is value.
Var params = {id:id};//第一个id是key:第二个id是value。
$.post("code.php", params);
$ . post(”代码。php”,params);
#3
1
replace the $.post("code.php", id); with
替换$ . post(“代码。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 );
});
#1
1
The issue is in the $.post
call, you're passing the value of id
with no identifier.
问题在于美元。post调用,传递没有标识符的id值。
Change this:
改变:
$.post("code.php", id);
to this:
:
$.post("code.php", {id: id});
#2
1
Remember we need to send Key=>value pair.
记住,我们需要发送Key=>值对。
You are directly passing value in your Ajax post method so key is missing and you are trying to access using key.
您直接在Ajax post方法中传递值,因此丢失了key,并试图使用key进行访问。
Var params = {id:id};//first Id is key : second id is value.
Var params = {id:id};//第一个id是key:第二个id是value。
$.post("code.php", params);
$ . post(”代码。php”,params);
#3
1
replace the $.post("code.php", id); with
替换$ . post(“代码。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 );
});