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

Posted

技术标签:

【中文标题】如何通过单击更改数组值?【英文标题】:How can I change an array value with a click? 【发布时间】:2015-03-12 06:00:12 【问题描述】:

我已经看到对 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" ></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>

【问题讨论】:

请发布您的完整 HTML。 【参考方案1】:

也许这可以让你知道如何去做。

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" ></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 并使其提交到同一页面

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

尝试根据需要修改

【讨论】:

谢谢,这非常有用,可以轻松修改:D【参考方案2】:
<?php
    $_SESSION["place"] = 0;
    $place = 0;
    $title = 0;
    session_start();
    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" >    </a></div>', "", "");

    if($_POST['clickies'] and $_SERVER['REQUEST_METHOD'] == "POST")
        $_SESSION["place"] =  $_SESSION["place"] + 1;
        $title = 1;
    
    echo $links[$_SESSION["place"]];
    $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 method="post" action="">
      <input type="submit" id="clickies" name="clickies" value="submit" />
</form>

你可以使用php的session。

【讨论】:

以上是关于如何通过单击更改数组值?的主要内容,如果未能解决你的问题,请参考以下文章

如何通过单击按钮更改 UITableView 的内容?

如何在文本框中输入值后单击输入更改图像?

当对象中的字段发生更改时如何从 v-model 数组中删除对象

目标 C - 如何使用按钮单击操作更改 UIslider 值

如何正确更新反应钩子状态中的数组

单击下一步按钮如何更改图像