点击PHP中的计数器(无表格)

时间:2022-01-26 08:21:45

I did once counter based on the form. Everything was fine until I have to make changes on the site.

我根据表格做了一次反击。一切都很好,直到我必须在网站上进行更改。

Old code:

<?php
if( isset($_POST['b1']) ) {
    $count += 1;
    $_SESSION['count'] = $count;
}

(...)

?>

<form action="start.php" method="post">
    <button id="b1" class="btn" name="b1"> B1 </button>
    <button id="b2" class="btn" name="b2"> B2 </button>
</form>

The form makes problems, so I'm looking for another solution. I was told that I tried Ajax , but it works only $_GET for me (I need post)

表格会产生问题,所以我正在寻找另一种解决方案。我被告知我尝试过Ajax,但它对我来说只有$ _GET(我需要发帖)

What am I doing wrong?

我究竟做错了什么?

<script>
  $(document).ready(function(){
    $("#b1").click(function(){
      $.post("start.php", { name: "b1" });
      location.reload();
    });
  });
</script>

1 个解决方案

#1


0  

what you check for is b1 but actually you pass name with b1 as value

你检查的是b1,但实际上你传递的名字是b1作为值

then you need to change your php code to this

那么你需要将你的PHP代码更改为此

<?php
if( isset($_POST['name']) && $_POST['name'] == 'b1' ) {
    $count += 1;
    $_SESSION['count'] = $count;
}

(...)

?>

UPDATE

if( isset($_POST['name']) && $_POST['name'] == 'b1' ) {
    $_SESSION['count'] += 1;
}

#1


0  

what you check for is b1 but actually you pass name with b1 as value

你检查的是b1,但实际上你传递的名字是b1作为值

then you need to change your php code to this

那么你需要将你的PHP代码更改为此

<?php
if( isset($_POST['name']) && $_POST['name'] == 'b1' ) {
    $count += 1;
    $_SESSION['count'] = $count;
}

(...)

?>

UPDATE

if( isset($_POST['name']) && $_POST['name'] == 'b1' ) {
    $_SESSION['count'] += 1;
}