简体   繁体   中英

PHP echo message

I am creating a online store Website at the moment I am working on Basket which is working. I am trying to add a functionality which will echo a message once a user clicks the add button confirming that the item indeed has been added to basket.

I created a simple if statement that i believe should take care of this nicely but nothing is happening. No message is echoed.

code:

if (isset($_GET['add'])){
    $quantity = mysql_query('SELECT product_id, product_qua FROM products WHERE product_id='.$_GET['add']);
    while($quantity_row = mysql_fetch_assoc($quantity)){
        if($quantity_row['product_qua'] !=$_SESSION['cart_'.$_GET['add']]){
            $_SESSION['cart_'.$_GET['add']]+='1'; 
        }
    }
}

if (isset($_GET['add'])){
    $basketMessage = "Item Sucesfully Added To Your Basket";
    echo $basketMessage;
}

Any suggestions...?

ANSWER:

OK forget the above code........

the belov code is code form a file where products are listed and then linked to cart.php the slice of code above:

code:

while($get_row = mysql_fetch_assoc($get)){
    ?>
        <table id='display'>
        <tr><td><?php echo "<img src=$get_row[$product_image] class='grow'>" ?></td></tr>

        <tr>
            <th></th>
            <th><strong>Avalible</strong></th>
            <th><strong>Price</strong></th>
            <th><strong>Description</strong></th>
        </tr>

        <tr>
        <td width='290px'><?php echo "$get_row[$product_name]" ?></td>
        <td width='290px'><?php echo "$get_row[$product_qua]" ?></td>
        <td width='290px'><?php echo "&pound $get_row[$product_price]" ?></td>
        <td width='290px'><?php echo "$get_row[$product_des]" ?></td>

        </tr>
        <tr>
        <td><?php echo '<a href="cart.php?add='.$get_row['product_id'].'" onclick="return basketMessage()">Add</a>';?></td>
        </tr>
        </table>

    <?php
        }
    }

    ?>

<script>    
function basketMessage(){
      var confirmed = confirm("Add this item to Basket ?");
      return confirmed;
}
</script>

OK as you can all see form the code I am looping the products database and listing some items that are assigned with the ADD button.....Now i added some javascript to this button to inform the user if this is the item they want to add to their basket

This isn't possible in php without resend the whole page. You could use javascript to react on some button click events.

In combination with jQuery this results in something like this

<div id="messagefield"></div>
<button id="buttonid">click me!</button>
<script>
    $("#buttonid").click(function() {
        $("#messagefield").html("The button was clicked");
    });
</script>

as @patashu stated, this is not possible with PHP alone. You should add javascript that handles the adding of the item to your cart via AJAX, then listens for the response you've echoed back and if it was successful, display the success message.

this could be a starting point:

html:

<button class="mybutton">add to cart</button>

javascript:

 <script type="text/javascript">
    $(document).ready(function(){
      $('.mybutton').click(function(){
      $.ajax({
        url:"yourscript.php", 
        data: {stuff: 'your cart data', productid: 134}
        success: function(data){
          alert("your success message'); 
        }
      }); 
    }); 

    }); 
    </script>

PHP function that is listening for your ajax call:

//does stuff 
$basketMessage = "Item Sucesfully Added To Your Basket";
echo json_encode($basketMessage);

obviously this is not complete...this is really just a starting off point / a general idea of what you should be doing.

First of all, you have an error at this line:

$quantity = mysql_query('SELECT product_id, product_qua FROM products WHERE product_id='.$_GET['add']);

if must be

$add = $_GET['add'];
$quantity = mysql_query("SELECT product_id, product_qua FROM products WHERE product_id='$add'");

or

$quantity = mysql_query("SELECT product_id, product_qua FROM products WHERE product_id='".$_GET['add']."'");

And you don't have to use mysql_* any more cause is deprecated. You can use http://php.net/manual/en/book.mysqli.php or http://php.net/manual/en/book.pdo.php

Why are you doing it in two phases? Try this:

if (isset($_GET['add'])){
$quantity = mysql_query('SELECT product_id, product_qua FROM products WHERE product_id='.$_GET['add']);
while($quantity_row = mysql_fetch_assoc($quantity)){
                if($quantity_row['product_qua'] !=$_SESSION['cart_'.$_GET['add']]){
                    $_SESSION['cart_'.$_GET['add']]+='1'; 
  }
}
$basketMessage = "Item Sucesfully Added To Your Basket";
echo $basketMessage;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM