-1

im start to learn jquery ajax, so i modify my web to ajax but its still error when passing data, its success to acces but always return to first column

here my code for call a value:

while ($r=mysql_fetch_array($query)) {
<a id="tambahkan-cart"  value='.$r[id_product].' data-toggle="modal" href="#modal-one"  class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
}

here my code for calling with click

$("#tambahkan-cart").click(function() {
        event.preventDefault();
        
                var id_product = $(this).attr("value");
                $.ajax({
                    url: 'tambah-cart.php',
                    type: 'POST',
                    data: {
                        id_product: id_product
                    },
                    success: function(data) {
                        $("#modal-body").html(data);
                    }
                });
            });

here tambah-cart.php.. i need to pass the data here,, but its return to value='1',.. I want to pass data dinamically


$idp=$_POST['id_product'];
$q=mysql_query("SELECT * from `product` WHERE id_product='$idp' ");

im appreciate if you tell me where is my wrong ... i used different value for different id_product when click, but its return to first column,,,.. thanks

Jalz Ae
  • 31
  • 1
  • 6
  • and what you get if you visit tambah-cart.php, are you getting single column? – Jerson Sep 25 '20 at 01:55
  • Yes. I will pass the data there – Jalz Ae Sep 25 '20 at 01:57
  • Data is sent... But always call first column only. Let me edit a little – Jalz Ae Sep 25 '20 at 01:57
  • can you debug and output to the console , console.log(id_product) and try clicking the columns if you get proper id – Jerson Sep 25 '20 at 02:01
  • instead of value= use data-id= and get in jquery with $(this).data("id") – Jerson Sep 25 '20 at 02:05
  • Done with it.. And always return to column 1 :( – Jalz Ae Sep 25 '20 at 02:05
  • i dont got it, you want return first data after insert it ? – jack Sep 25 '20 at 02:07
  • I want call dinamically .. If value 3.. Its show table 3.. – Jalz Ae Sep 25 '20 at 02:11
  • But its always show table 1.. Wherever i clicked it. I see the value is right. The problem is when passing data.. It just can pass value 1 – Jalz Ae Sep 25 '20 at 02:12
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 25 '20 at 10:08

2 Answers2

0

I thought I would draw your attention to this code too. It has to do with the event

Change this

$("#tambahkan-cart").click(function() {
   event.preventDefault();
   var id_product = $(this).attr("value");
   $.ajax({
      url: 'tambah-cart.php',
      type: 'POST',
      data: {
      id_product: id_product
      },
      success: function(data) {
        $("#modal-body").html(data);
      }
   });
 });

To

$("#tambahkan-cart").click(function(event) {
   event.preventDefault();
   var id_product = $(this).attr("value");
   $.ajax({
      url: 'tambah-cart.php',
      type: 'POST',
      data: {
      id_product: id_product
      },
      success: function(data) {
        $("#modal-body").html(data);
      }
   });
 });
Dharman
  • 30,962
  • 25
  • 85
  • 135
rMexa
  • 24
  • 2
-1

Change This

$q = mysql_query("SELECT * from `product` WHERE id_product='$idp' ");

To

$q = mysqli_query("SELECT * from `product` WHERE id_product='$idp' ");

rMexa
  • 24
  • 2