php - 如何通过单击更改数组值?

标签 php html arrays ajax

为此,我看到了对 Ajax 的引用,但我不完全确定如何将其与数组集成并仍然拥有一个正常运行的系统。我想在每次用户按下按钮时更改 $place 和 $title 的值,我知道我需要一个 if 语句和某种处理表单的方法(在 PHP 中没有点击),但我不需要知道除此之外的任何事情。我在下面粘贴了我的 PHP 和 HTML 按钮:

<!DOCTYPE html>
<html>
<head>
<title>StatsCalc V1</title>
<link rel="stylesheet" type="text/css" href="sheen.css">
<link rel="script" type="text/javascript" href="slide.js">
<link rel="icon" type="image/png" href="favicon.ico">
<link href='http://fonts.googleapis.com/css?family=Raleway:500' rel='stylesheet' type='text/css'>
</head>
<body>
      <?php
        $place = 0;
        $title = 0;

        echo '<div class="boxes"><h1>Statistics Calculator: Version 1</h1></div>';

        $links = array('<div class="fancyBoxes"><a href="standardisedForm.php"><img class="mainPic" src="normalDist.svg" alt="a normal distribution, courtesy openclipart.org"></a></div>', "", "");
        echo $links[$place];

        /*if($_GET){
            if(isset($_GET['clickies'])){
                $place = 1;
                $title = 1;
            }
        }*/

        if($_POST['clickies'] and $_SERVER['REQUEST_METHOD'] == "POST"){
            $place = 1;
            $title = 1;
        }

        $subtitle = array('<div class="boxes"><h1>Standardised Score</h1></div>', '<div class="boxes"><h1>Standard Deviation</h1></div>', '<div class="boxes"><h1>Averages</h1></div>');
        echo $subtitle[$title];

      ?>
      <input type="submit" id="clickies" name="clickies" value="" />
      <script src="move.js"></script>
      <script src="slide.js"></script>
      <script src="jquery-1.11.2.js"></script>
      <!--The calculator must be able to field standardised score, standard deviation, and averages (mean/median/mode)-->
      <!--Headings will be assigned with an array; slides will be navigated with tabs on either side of the window-->
      <!--Credit to https://openclipart.org/detail/171055/normal-distn-shaded-outside-1s-by-oderwald-171055 for the Standardised Score image/label thingy-->
</body>
</html>

最佳答案

也许这可以让您了解如何去做。

if($_SERVER['REQUEST_METHOD'] == "POST") {

      if (isset($_POST['clickies'])) {
         $count = intval($_POST['count']);
         $count++;
         $place = $count;
         $title = $count;
      }

   } else {
      $place = 0;
      $title = 0;
   }

        echo '<div class="boxes"><h1>Statistics Calculator: Version 1</h1></div>';

        $links = array('<div class="fancyBoxes"><a href="standardisedForm.php"><img class="mainPic" src="normalDist.svg" alt="a normal distribution, courtesy openclipart.org"></a></div>', "", "");

        echo $links[$place];

        echo $place; echo "<br/>";

        $subtitle = array('<div class="boxes"><h1>Standardised Score</h1></div>', '<div class="boxes"><h1>Standard Deviation</h1></div>', '<div class="boxes"><h1>Averages</h1></div>');

        echo $subtitle[$title];

?>
   <form action="" method="POST">
      <input type="submit" id="clickies" name="clickies" value="" />
      <input type="hidden" id="count" name="count" value="<?php echo $place;?>" />   
   </form>

我所做的是需要将按钮放在一个表单中并放置一个 method= post 并让它提交到同一页面

我还将计数存储在隐藏的输入中,并在提交表单时始终传递它并更新其在表单上的值,

尽量根据自己的需要修改

关于php - 如何通过单击更改数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27936393/

相关文章:

php - 如何使用 DB insert 将 boolean 值 "false"插入为 0?

Javascript:图像预览无法正常工作

php - 如何安全地存储网站的配置文件?

html - 如何使无序列表具有响应性,并在右侧显示图标

c++ - 如何使用 vector 中包含的 double[4][4] 数组?

php - MySQL 从 ID 最小的列中选择列

Javascript 和 jQuery 在同一个 HTML 文件中

javascript - 仅在页面初始加载时启动 Pace.js

javascript - 数组数组和值数组的区别

php - array_combine with key 只有一个来自数组 1 和多个来自数组 2 的值