gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:13:20 25 4
gpt4 key购买 nike

我有一个 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);
?>

最佳答案

问题出在 $.post 调用中,您传递的 id 值没有标识符。

改变这个:

$.post("code.php", id);

为此:

$.post("code.php", {id: id});

关于php - 无法访问 ajax 发送的 php 页面上的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36529795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com