简体   繁体   English

jQuery / AJAX数据未发布

[英]jQuery/AJAX data isn't posting

I am trying to create a very basic auction page on a site I am working on. 我正在尝试在我正在工作的网站上创建一个非常基本的拍卖页面。 I'm sort of working it out as I go along but I am now a bit stuck. 我一直在努力解决问题,但现在有些困难。

Data is stored in a MySQL table, this data has the image link, the ID, and the current bid. 数据存储在MySQL表中,该数据具有图像链接,ID和当前出价。

I then retrieve the data in PHP/HTML, example here: $result = mysqli_query($con,"SELECT * From auction WHERE category = 'Bathroom' ORDER BY ID DESC"); 然后,我在PHP / HTML中检索数据,示例如下:$ result = mysqli_query($ con,“ SELECT * From Auction WHERE category ='Bathroom'ORDER BY ID DESC”);

while($row = mysqli_fetch_array($result))
  {
    echo "<form name='auction' id='auction'><div class='auction-thumb'>
                <div class='auction-name'>" . $row['Item'] . "</div>";
            echo "<img class='auction' src='" . $row['ImagePath'] . "' />";
            echo "<div class='auction-bid'>Current Bid: £" . $row['CurrentBid'] . "</div>";
            echo "<div class='auction-bid'>Your Name: <input type='text' class='bidder' name='bidname'/></div>";
            echo "<div class='auction-bid'>Your Bid: <input type='text' class='auction-text' name='bid'/></div>";
            echo "<div class='auction-bid'><button name='submit' id='submit' value='" . $row['ID'] . "' type='submit'>Place Bid!</button></div>";
    echo "</div></form>";
  }
echo "</table>";

This code pulls through the items absolutely fine. 这段代码绝对可以遍历所有项目。 Along with a textbox for a name and a bid (I am not doing anything with the name at the moment). 以及用于输入名称和出价的文本框(目前我不使用该名称做任何事情)。

My jQuery then looks like this: 然后,我的jQuery如下所示:

$(document).ready(function(){
        $('#auction').submit(function(){
            var id = $('#submit').val();
            var bidname = $('input[name=bidname]').val();
            var bid = $('input[name=bid]').val();


            $.ajax({
            type: "POST",
            url: "auction-handler.php",
            dataType: "json",
            data: {bidname: bidname, bid: bid, id: id},
            success: function(){
            }
        });
        return true;

        }); 
    });

Again this is very basic and I am not concerned about validation just yet. 同样,这是非常基本的,我现在还不关心验证。

And finally here is a snippet of my PHP code: 最后是我的PHP代码的片段:

$bidname = $_POST['bidname'];
$bid = $_POST['bid'];
$id = $_POST['id'];

$query = "UPDATE auction SET CurrentBid = '$bid' WHERE ID = '$id'";

mysqli_query($con, $query) or die(mysqli_error());


mysqli_close($con);

My problem is that when I click submit, nothing really happens. 我的问题是,当我单击提交时,什么都没有发生。 All the variable names and values get put into the browser address bar, and the page just seems to refresh. 所有变量名和值都放入浏览器地址栏中,页面似乎刷新了。

The data does not get posted and when I debug with Firebug, I just get a red cross and it doesn't give me any errors. 数据不会发布,当我使用Firebug进行调试时,我只会出现一个红叉,并且不会给我任何错误。

I know from just looking at my code that best practices aren't followed, but I just want to get something working and then tidy it up later. 通过查看我的代码,我知道没有遵循最佳实践,但是我只是想使某些事情起作用,然后在以后进行整理。

If anyone could point me in the right direction that would be a big help. 如果有人能指出正确的方向,那将是很大的帮助。

Thank you, and if you need anymore information please just let me know. 谢谢,如果您需要更多信息,请告诉我。

You need to re-write this a bit: ID's have to be unique and when you loop through your items you assign the same IDs over and over to elements in different forms. 您需要重新编写一下:ID必须是唯一的,并且当您遍历项目时,一遍又一遍地将相同的ID分配给不同形式的元素。

So when you try to get the values in your submit handler, jQuery does not know which value to get (it probably gets the value of the first element with that ID). 因此,当您尝试获取提交处理程序中的值时,jQuery不知道要获取哪个值(它可能获取具有该ID的第一个元素的值)。

You should start with changing the IDs to for example classes and then serialize (for example...) the submitted form - $(this) in your submit handler - to get the correct data. 您应该先将ID更改为例如类,然后在提交处理程序中序列化(例如...)提交的表单$(this) ,以获取正确的数据。

First of all: You need to rewrite your form element every element should have an unique id to differentiate the respective element. 首先:您需要重写您的form元素,每个元素应具有唯一的ID以区分相应的元素。

<?php while($row = mysqli_fetch_array($result)){ ?>
     <form name='auction' id='auction<?php echo $row['ID'] ?>'>
        <input type='hidden' name='id' value='<?php echo $row['ID'] ?>'>
        <div class='auction-thumb'>
            <div class='auction-name'><?php echo $row['Item'] ?></div>
            <img class='auction' src='<?php echo $row['ImagePath'] ?>' />            
            <div class='auction-bid'>Current Bid: £<?php echo row['CurrentBid'] ?></div>
            <div class='auction-bid'>Your Name: <input type='text' class='bidder' name='bidname'/></div>
            <div class='auction-bid'>Your Bid: <input type='text' class='auction-text' name='bid'/></div>
            <div class='auction-bid'>
                <input type='submit' name='submit' value='Place Bid!'>
            </div>
        </div>
    </form>

and replace your jquery code to 并将您的jquery代码替换为

$(document).ready(function(){
    $('form[name="auction"]').submit(function(){
        var id = $(this).find('input[name="id"]').val();
        var bidname = $(this).find('input[name="bidname"]').val();
        var bid = $(this).('input[name="bid"]').val();

        $.ajax({
           type: "POST",
           url: "auction-handler.php",
           dataType: "json",
           data: {bidname: bidname, bid: bid, id: id},
           success: function(){
           }
        });
        return false;
    }); 
});

Add following keys in ajax to trace the errors. 在ajax中添加以下键以跟踪错误。

  $.ajax({
    url: "auction-handler.php",
    type: "POST",
    dataType: "json",
    data: {bidname: bidname, bid: bid, id: id},
    crossDomain:true,
    success: function(result){ console.log(result);   }
    error: function(httpReq,status,exception){
        alert("error - " +status+" "+exception);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM