0

Hello,

I am able to add one item into the basket. But, I was told that using Javascript I can add more items. Can you please help me?

<?php
     require "connect.php";
     $query = "SELECT `DVDID`, `NameOfTheDVD`, `Quantity`, `Price` FROM `DVD` ";
     $stmt = $dbhandle->prepare($query);
     $stmt->execute();

     $num = $stmt->rowCount();

     echo "<table border='3';>";
        echo '<tr>';
        echo '<th>DVD</th>';
        echo '<th>Quantity</th>';
        echo '<th>Price</th>';
        echo '<th>Add To Basket</th>';
        echo '<th> Description </th>';
        echo '</tr>';

     if($num>0){
      while ($row = $stmt->fetch(PDO::FETCH_OBJ)){

        $mydvd = $row->DVDID;
        $name = $row->NameOfTheDVD;
        $Quantity = $row -> Quantity;
        $Price = $row -> Price;

        echo '<input type="hidden" name="id" value="'.$mydvd.'">';
        echo '<input type="hidden" name="item" value="'.$name.'">';
        echo '<input type="hidden" name="Quantity" value="'.$Quantity.'">';
        echo '<input type="hidden" name="Price" value="'.$Price.'">';
        echo '<input type="hidden" name="Cart" value="'.$cartItemCount.'">';

        echo '<tr>';
        echo '<td>Name Of the DVD : <br>'.$row->NameOfTheDVD.'<br></td>';
        echo '<td>Quantity :  '.$row->Quantity.'</td>';
        echo '<td>Price:  '.$row->Price.'</td>';
        echo '<td><button><a href = "basket.php?id='.$mydvd.'&name='.$name.'&Quantity='.$Quantity.'&Cart='.$cartItemCount.'&Price='.$Price.'"> Add To Basket</a><br></button></td>';
        echo '<td><button><a href = "Description.php">View Description</a></td>';
        echo '</tr>';



        echo $mydvd;
        //echo '<form method="post" action="basket.php?id='.$mydvd.'">';


        //echo '<input type="submit" value="Add To Basket">';




        }


        }



        // no products in the database
        else{
          echo "No products found.";
        }


     ?>

Above is my PHP code. As you can see I am using URL to send the the parameters.

Just a note, I am not interested in security as it is just a assignment

Thank You

2
Contributors
2
Replies
13
Views
3 Years
Discussion Span
Last Post by Mohamed_26
0

Hi,
You need make this change in your php code:

echo '<td><button onclick="AddToBasket()">Add To Basket</a><br></button></td>';

AddToBasket() is a Javascript function. But I will use some ajax.

//This function create an ajax object
//It's a standart function don't need to modify it
function getxmlhttp()
{
    if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    else {
    // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function AddToBasket()
{
   var mydvd = document.getElementByName("id").value;
   var name  = document.getElementByName("item").value;
   var quantity = document.getElementByName("Quantity").value;
   var price = document.getElementByName("Price").value;
 //I not undestand what do you do with cartItemCount

 //Now i use ajax
 var ajax1 = getxmlhttp();
 ajax1.onreadystatechange = function() {
    if (ajax1.readyState == 4 && ajax1.status == 200) 
    {
        alert(ajax1.responseText);
        //here should be doing other something else with responseText
        //give a message or refresh the cart display
        }
    }
    ajax1.open("GET", "basket.php?id="+ mydvd+"name="+name+"Quantity="+quantity+"Price="+price.'",true);
    ajax1.send(); 
}

And now, in basket.php you need to get the values like this:

$mydvd = $_GET['id'];
$name  = $_GET['name'];
$quantity = $_GET['Quantity'];
$price    = $_GET['Price'];

This data must be saved in your cart table. After that you must redisplay the cart content using echo. The echo output will be ajax1.responseText.

But, your inputs are hidden, your code is not correct to add in basket.
To add products in basket you need a list of products with a "Add to basket" button next to every product.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.