2

hi i want to get the this php variable by jquery and use it for ajax this my php code and i use it in php page

function product($li)
{
    $result = mysqli_query($li, "SELECT * FROM product ORDER BY id ASC") or die(mysqli_error($li));

    while ($Row = mysqli_fetch_array($result)) {
        echo "<div class='product'>";

        echo "<img src=" . $Row['imageurl'] . " width='100' height='75'>";
        echo $Row['farsiname'];
        echo $Row['englishname'];
        echo $Row['description'];
        echo $Row['price'];
            echo getid($li , $Row['id']);
           echo '<button class="add_to_cart">برو تو سبد</button>';
        echo '<input type="hidden" class="id" name="productid" value="' . $Row['id'] . '" />';

        echo '</div>';
    }
}

i want to get $row['id'] by jquery when i click button please help me!

karthikr
  • 97,368
  • 26
  • 197
  • 188
Alireza
  • 2,744
  • 2
  • 17
  • 21
  • PHP runs on the server, jquery runs on the client. if you want jquery to access PHP stuff, you'll need to have PHP embed that stuff into the page at generation time, or use an AJAX call. – Marc B Sep 18 '14 at 20:18
  • `$Row['id']` is probably different, at each step of the while loop. You should probably use `echo json_encode($result->fetch_all(MYSQLI_ASSOC));`, then access in AJAX as `data['0'].id` and so on. Your code echos HTML. I think you want JSON. – StackSlave Sep 18 '14 at 20:29

5 Answers5

2
$('.add_to_cart').on('click',function(){
  alert($(this).closest('.product').find('.id').val())
})
supernova
  • 3,814
  • 3
  • 22
  • 37
1

The main approach for this case is to render Javascript code with PHP.

Below there is a pseudocode showing the approach:

<script language="Javascript">
 var id = <?php echo $Row['id']; ?>;
</script>
zavg
  • 10,351
  • 4
  • 44
  • 67
0

You want to get it's value from the hidden input field, right?

If so, then you simply need to do this in jQuery.

$(".id").val();

This is finding the value of the hidden input field with the class of id.

  • It doesn't matter how many times it's called on the page because it's going to be the same value everywhere. –  Sep 19 '14 at 15:32
0

If your id is an integer:

echo '<script> var id = ' . $Row['id'] . '; </script>';

else:

echo '<script> var id = "' . $Row['id'] . '"; </script>';
rneves
  • 2,013
  • 26
  • 35
  • i linked my js file how to access on linked page – Alireza Sep 18 '14 at 20:37
  • Do you call this js at beginning of page? If yes, the best is use at the end. To use this solution, maybe works putting a "var id;" before the function that you use. And remove "var" from this php code. – rneves Sep 18 '14 at 20:44
-1

Rewrite your code by follow my steps

function product($li)
{
    $result = mysqli_query($li, "SELECT * FROM product ORDER BY id ASC") or die(mysqli_error($li));

    while ($Row = mysqli_fetch_array($result)) {
        echo "<div class='product'>";

        echo "<img src=" . $Row['imageurl'] . " width='100' height='75'>";
        echo $Row['farsiname'];
        echo $Row['englishname'];
        echo $Row['description'];
        echo $Row['price'];
            echo getid($li , $Row['id']);
           echo '<button class="add_to_cart" data-datac="'.$Row['id']'.">برو تو سبد</button>';
        echo '</div>';
    }
}

//Jquery

$('.add_to_cart').click(function() {
        var d = $(this).data('datac');      
        alert(d);   
} );
Seegan See
  • 101
  • 1
  • 1
  • 9