Forhad Sikder
Forhad Sikder

Reputation: 1

Ajax call with javascript not working

I want to try to get some value from page cart.php from and ajax call, but its not working. Ajax code is below:

function temp_sell(id) {
    //var p_id=document.getElementById('temp').value;
    alert(p_id);
    $.ajax({
        type: "POST",
        url: "temp_sell.php",
        data: "value=" + p_id,
        success: function (message) {
            alert(message);
            $("#your_cart").html(message);
        }
    });
}

temp_sell.php is below where I want to show some products details

<?php 
include("connection.php");
echo $p_id = $_POST['value'];
$qty=1;
$query="SELECT * FROM product WHERE id='$p_id'";
    $result=mysql_query($query);
    while($data=mysql_fetch_array($result))
    {
    ?>
            <form>
            <table>
                <tr>
                    <img src="<?php echo "img/".$data['image'];?>"/>
                    <strong><?php echo $data['pro_name'];?></strong>
                    <strong><?php echo $data['price'];?></strong>
                    <input type="text" value="<?php echo $qty;?>"/>
                </tr>
            </table>
            </form>
    <?php
    }                   
?>  

Upvotes: -1

Views: 104

Answers (2)

Alireza Fallah
Alireza Fallah

Reputation: 4607

Do this :

function temp_sell(id){
var p_id=document.getElementById('temp').value; // <--- uncomment this line
alert(p_id);
$.ajax({
         type: "POST", 
         url: "temp_sell.php",
         data: {value:p_id}, // <--- change this
         success: function(message){ 
         alert(message);
         $("#your_cart").html(message);
        }       
       });
   }

Upvotes: 0

Krish R
Krish R

Reputation: 22711

p_id is undefined, you have commented the p_id value or change temp_sell(p_id).

function temp_sell(p_id)

instead of

function temp_sell(id)

Reference comments : Ajax call with javascript not working

Upvotes: 1

Related Questions