在处理操作后,在php中递增变量?

在处理操作后,在php中递增变量?,php,Php,我的课程目标: 在我的index.php文件中,会显示一个图像 我希望,当我的用户点击该图像时,会显示一个新的图像 当他点击新图像时,另一个新图像会出现 到目前为止我做了什么? <?php $mycolor = array("red.jpg", "green.jpg", "blue.jpg"); $i = 0; $cc = $mycolor[$i++]; ?> <form method="post" action="index2.php"> <input typ

我的课程目标:

  • 在我的index.php文件中,会显示一个图像
  • 我希望,当我的用户点击该图像时,会显示一个新的图像
  • 当他点击新图像时,另一个新图像会出现
  • 到目前为止我做了什么?

    <?php
    $mycolor = array("red.jpg", "green.jpg", "blue.jpg");
    $i = 0;
    $cc = $mycolor[$i++];
    ?>
    
    
    <form method="post" action="index2.php">
    <input type="image" src="<?php echo $cc; ?>">
    </form>
    
    
    
    您可以使用会话、cookies或POST变量来跟踪索引,但需要了解如何记住最后一个索引,以便+1它。下面是使用另一个(隐藏)post变量的示例:

    <?php
    
        // list of possible colors
        $mycolor = array('red.jpg', 'green.jpg', 'blue.jpg');
    
        // if a previous index was supplied then use it and +1, otherwise
        // start at 0.
        $i = isset($_POST['i']) ? (int)$_POST['i'] + 1 : 0;
    
        // reference the $mycolor using the index
        // I used `% count($mycolor)` to avoid going beyond the array's
        // capacity.
        $cc = $mycolor[$i % count($mycolor)];
    ?>
    
    <form method="POST" action="<?=$_SERVER['PHP_SELF'];?>">
    
      <!-- Pass the current index back to the server on submit -->
      <input type="hidden" name="id" value="<?=$i;?>" />
    
      <!-- display current image -->
      <input type="button" src="<?=$cc;?>" />
    </form>
    

    您有不同的可能记住$i。e、 g:

    $\u GET

    曲奇饼:

    会议:

    对于这个问题也没有必要使用表单。只需使用超链接包装图像,并通过增加参数(index.php?i=1、index.php?i=2、index.php?i=3等)修改url即可。

    
    
    你需要学习Javascript,因为你所需要的,只能用js来完成。我想你需要学习Javascript:-)我试过这个方法。但它没有起作用。也许,我们没有在任何地方使用过身份证。
    <?php
    $mycolor = array("red.jpg", "green.jpg", "blue.jpg");
    
    if (isset($_POST['i'])) { // Check if the form has been posted
      $i = (int)$_POST['i'] + 1;   // if so add 1 to it - also (see (int)) protect against code injection
    } else {
      $i = 0;  // Otherwise set it to 0
    }
    $cc = $mycolor[$i]; // Self explanatory
    ?>
    
    
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="image" src="<?php echo $cc; ?>">
    <input type="hidden" name="i" value="<?php echo $i; ?>">  <!-- Here is where you set i for the post -->
    </form>