简体   繁体   English

PHP回显消息

[英]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. 我创建了一个简单的if语句,我认为它应该很好地解决此问题,但是什么也没有发生。 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: belov代码是文件形式的代码,其中列出了产品,然后将上面的代码片段链接到cart.php:

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 好的,因为大家都能从代码中看到我正在循环产品数据库并列出一些用ADD按钮分配的项目.....现在我在此按钮中添加了一些JavaScript通知用户是否这是他们想要的项目添加到他们的篮子里

This isn't possible in php without resend the whole page. 不重新发送整个页面,这在php中是不可能的。 You could use javascript to react on some button click events. 您可以使用javascript对某些按钮单击事件做出反应。

In combination with jQuery this results in something like this 与jQuery结合使用时,结果如下所示

<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. 如@patashu所述,仅靠PHP不可能做到这一点。 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. 您应该添加用于处理通过AJAX将商品添加到购物车中的商品的javascript,然后侦听您已回显的响应,如果成功,则显示成功消息。

this could be a starting point: 这可能是一个起点:

html: 的HTML:

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

javascript: 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: 正在侦听您的ajax调用的PHP函数:

//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. 而且您也不必使用mysql_ *,不再使用任何原因。 You can use http://php.net/manual/en/book.mysqli.php or http://php.net/manual/en/book.pdo.php 您可以使用http://php.net/manual/en/book.mysqli.phphttp://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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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