-1

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
    }                   
?>  
10
  • And php code where is? Commented Jan 10, 2014 at 9:33
  • are you getting error? Commented Jan 10, 2014 at 9:33
  • Please show your php code. Commented Jan 10, 2014 at 9:33
  • Also, tell is if the alert is displaying. Commented Jan 10, 2014 at 9:33
  • 2
    p_id is undefined, you have commented the p_id value or change temp_sell(p_id) Commented Jan 10, 2014 at 9:35

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

yes i correct it but still not working i can't get anything from temp_sell.php
@ForhadSikder: because, as the 2 other answers explain, you're not fetching the resultset as an associative array (while($data=mysql_fetch_array($result)) should be while($data=mysql_fetch_assoc($result)))
did you get the alert when you call that function. Make sure your jquery libaray path is correct. Checck your browser console, it maythrough some errors if any
0

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);
        }       
       });
   }

5 Comments

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
thank you, I did not know it, but user is still wrong, because MYSQL_ASSOC) should be as the second argument of mysql_fetch_array, to return a associative array
By default, it fetches both, but you can pass second parametr based on your need.
@KrishR: either way, sure the fetching works using the default behaviour of mysql_fetch_array, but it won't work for long... once the ancient extension is finally removed, the code won't work anyway. In fact, it will already issue E_DEPRECATED notices on up-to-date systems
I updated my answer, thank you @KrishR, after 3 years of php programming, I didn't know that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.