【发布时间】:2018-08-28 06:30:46
【问题描述】:
我分别使用 Ajax 将两个 javascript 变量发布到一个 php 文件以及一个 html 表单。我想将两个 javascript 变量与已发布的表单值一起使用,但我不知道该怎么做。
<script>
$(document).ready(function() {
var aucid = "<?php echo $auctionID; ?>";
var userid = "<?php echo $userID; ?>";
$.ajax({
url: "JqueryPHP/HighestBid.php",
method: "POST",
data: {'auctionid': aucid, 'userid' : userid },
success: function (result) {
$('#price').html(result);
}
});
$('form').bind('submit', function (event) {
event.preventDefault();// using this page stop being refreshing
$.ajax({
type: 'POST',
url: 'JqueryPHP/HighestBid.php',
data: $('form').serialize(),
success: function () {
alert('form was submitted');
}
});
});
});
我将这两个 javascript 变量分别发布到表单中。
<form>
<input type="number" min="<?php echo $startingprice ?>" step="any" style="width: 10em;" size="35" name="newbid" id="newbid" tabindex="1" class="form-control" placeholder="New Bid €" value="" required>
<input type="submit" name="submit" id="submit" tabindex="2" class="form-control btn btn-login" style="width: 14em" value="submit">
</form>
<h4 class="price">Highest bid : <span id="price"></span></h4>
当我将 userID 的值回显到 span 类中时,您可以看到它的值为 2。
//JqueryPHP/HighestBid.php'
$auctionid;
$userID;
$auctionid = $_POST['auctionid'];
$userID = $_POST['userid'];
echo $userID;
if (isset($_POST['newbid']))
{
$newbid=$_POST['newbid'];
$conn = new mysqli('localhost', 'root', '', 'auctionsite');
$sql = 'INSERT INTO auction (useridhighestbid)VALUES("'.$userID.'")';
if(@$conn->query($sql)){ //execute the query and check it worked
return TRUE;
}
}
但是,当我在提交表单时尝试使用 userID 并尝试将其插入数据库进行测试时,该值为 0。
我将如何使用 javascript 变量发布表单值,以便我可以使用更新语句来更新我的数据库?
【问题讨论】:
-
您可以在
Form Serialize中附加aucid和userid值。 -
您为什么需要这样做?在我看来,
userid已经存储在会话中,aucid由访问的 url 确定。使用用户提供的值只会让您的拍卖被客户操纵。 -
我也解决了这个问题@jeroen,但有些人似乎不在乎......
-
@will mannix 看到更新的答案。
标签: javascript php jquery ajax post