Jump to content

Need help with post method


doforumda

Recommended Posts

Hi I am having this error while running my php script using ajax. Everything seems to be fine but I do not know why it is giving this error.

<br />
<b>Notice</b>:  Undefined index:  amount in <b>C:\wamp\www\...\add_bid.php</b> on line <b>3</b><br />
{"error":"yes","fieldErrors":null}

 

Here is my code

 

here is .php code which contains the form

<?php
session_start();
$auctionid = $_POST['auction_id'];
include "design/header.php";
include "DB/db.php";
?>
<div class="contents" id="bid_contents">
    <?php
        $q = "SELECT * FROM bids WHERE productid='$auctionid' ORDER BY bidid DESC LIMIT 0,1";
        $query = mysql_query($q) or mysql_error();
        //echo $q;
        $row = mysql_fetch_assoc($query) or mysql_error();
        $current_bid = $row['current_bid'];
        
        $thumbnail = mysql_query("SELECT * FROM products WHERE productid='$auctionid'");
        $row_thumb = mysql_fetch_assoc($thumbnail);
        $thumb = $row_thumb['thumbnail'];
        $title = $row_thumb['product_title'];
        
    ?>
    <div id="thumb">
        <img src='<?php echo $thumb ?>' border='none' /><br />
        <?php 
        echo $title."<br />";
        echo "<b>Current Bid:</b> Eur ".$current_bid;
        ?>
        <form name="bid_form" id="bid_form">
            <input type="text" name="amount" size="5" />
            <input type="button" name="place_bid_btn" id="bid_btn" value="Place Bid" />
            <div id="message"></div>
        </form>
    </div><!-- thumb -->
</div>
<?php include "design/footer.php" ?>

 

here is .js file for ajax

 

$(function() {
   $('#bid_btn').click(function() {
     var url = 'add_bid.php';
     var query = $('#bid_form').serialize();
     alert(query);
     $.ajax({
        type: 'POST',
        url: url,
        query: query,
        dataType: 'json',
        success: function(data) {
            if(data.error == 'no') {
                $('#message').css('display','none');
            } else {
                $('#message').html(data.fieldErrors).css('display','block');
            }
        }
     });
   });
});

 

and here is where i get data from previous .php file and the problem seems to be in this page. I guess. Name of this page is add_bid.php

<?php
session_start();
$bid = $_POST['amount'];
$error = 'yes';
$msg = $bid;

$JSON_array = array('error' => $error, 'fieldErrors' => $msg);
$JSON_response = json_encode($JSON_array);

header('Content-type: application/json');
echo $JSON_response;
?>

 

I am adding jquery library and javascript file in my header.php file so it is fine.

 

Please help

 

Link to comment
https://forums.phpfreaks.com/topic/237202-need-help-with-post-method/
Share on other sites

The error you're seeing is only a notice, telling you that $_POST['auction_id'] does not exist. You should always ensure you check variables/array keys exist, in situations where there's a chance they might not, with isset or empty - depending on what value you're expecting.

The problem is at this line

$bid = $_POST['amount'];

 

I did this now

if(isset($_POST['amount'])) {
    $msg = "amount is set";
} else {
    $msg = "Amount is not set";
}

 

now it shows Amount is not set but I am sending data to this page but it says it is not set. Help

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.