【问题标题】:Button Onclick change the value , permanent until next click Jquery [closed]按钮 Onclick 更改值,永久直到下一次单击 Jquery [关闭]
【发布时间】:2019-12-24 07:40:53
【问题描述】:

我正在创建一个购物车,如果用户单击“添加到购物车”按钮,该按钮应更改为“继续购物”,它应该是永久性的(即使在刷新页面后)。如果再次点击按钮,它应该变回“添加到购物车”。

我有这段代码,但刷新后会发生变化

    <script>
        $('.add').click(function() {
            this.value = 'Continue shopping';
        });
    </script>

我使用 php 作为后端。

【问题讨论】:

  • 尝试将值存储在db中
  • @nishanth siraj 最好打一个ajax 调用并将SESSION 变量存储在后端,该购物车不为空。重新加载后只需检查会话以获取相应的输出
  • 如果您不想参与任何会话或数据库,请尝试本地存储
  • 您也可以尝试使用 HTML 本地存储或尽可能多的建议您可以更新数据库中的数据

标签: php jquery button onclick


【解决方案1】:

这是一个解释一切的教程。您没有要求每次看到教程,但它是一本非常好的读物。 https://phppot.com/php/simple-php-shopping-cart/

【讨论】:

    【解决方案2】:

    从数据库和视图文件中获取值。

     <?php
             $htmlResponse = "";
        //by which column values you want change button text check condition for that.
              if ($data->column_name == 0) {
         $htmlResponse .= '<span onclick="changeActivityStatus(' . $data->id . ', 1, this)" style="cursor: pointer;">add to cart</span>';
            }
    else {
         $htmlResponse .= '<span onclick="changeButton(' . $data->id . ', 0, this)" data-value="Blocked" style="cursor: pointer;">Continue shopping</span>';
                 }
               ?>
      <?php echo $htmlResponse; ?>
    
    call function in js file
    //test.php file is just an example you put here your php file name
    function changeButton(id, status) {
        $.post(base_url + "test.php", {id: id, status: status}, function (data) {
            if (data.code === "1") {
               alert('success');
                window.location.reload();
            } else if (data.code === "0") {
               alert('something went wrong');
            }
        });
    }
    

    在 test.php 中,如果单击添加到购物车按钮值将更新并且按钮文本将更改,则必须使用更新查询

    【讨论】:

    • 为什么有人要“试试这个”?为什么这会有什么不同?
    • 发布相同的问题作为答案,你想说什么
    【解决方案3】:

    反映数据库中的变化。尝试在数据库中创建 bool 列并尝试使用 ajax 在数据库中更改它

    <script>
    $('.add').click(function() {
        this.value = 'Continue shopping';
       $.ajax({
        type: "POST",
         url: "currentfilename.php",
         data: {change:'true'},
      });
    
    });
    </script>
    

    PHP 文件

     if(isset($_POST['change']))
    {
     $connection= mysqli_connect("localhost","my_user","my_password","my_db");
     $sql="update tablename set colname=1";
     mysqli_query($connection,$sql);
    } 
    

    HTML 文件 编写选择查询并获取列并编写以下内容

    if($row['col']==1)
    {echo"<button type='button'>Continue shopping</button>
     }
    

    【讨论】:

    • 这应该是一个cmnt
    • 由于我的声誉低于 50,我无法发表评论。这并不意味着解决方案是错误的,所以你投了反对票
    • 这不是解决方案
    • 我已经编辑了我的解决方案检查它
    猜你喜欢
    • 1970-01-01
    • 2013-04-25
    • 2013-12-27
    • 2021-09-18
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多