首页 > 解决方案 > 我如何实现这个数组来保存表单中的数据

问题描述

我一直在尝试将信息从表单发送到我的数组,以便它可以保存该信息并在需要时显示它。iv 已经设法从一个页面发送数据并将其回显到另一个页面,但我希望它存储在一个数组中,以便一次显示多个问题。有人告诉我使用这种样式的数组来让朋友完成工作(请参阅下面的数组代码以获得一个想法),但它给了我一个语法错误,我尝试了不同类型的数组,但我总是得到一个语法错误。

我的购物车代码(包含我的数组)

  <?php
  session_start();
  ?>


  <?php
  require_once("head.php");
  ?>
  <?php
  require_once("nav.php");
  ?>




    <link id='stylecss' type="text/css" rel="stylesheet" href="css/cartstyle.css">
    <link id='stylecss' type="text/css" rel="stylesheet" href="css/style.css">
    <!DOCTYPE html>
    <html lang="en">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <head> 
    <meta charset="utf-8">
    <title> Shopping Cart </title>
    <div class="shopping-cart">
    <!-- Title -->
    <div class="title">
      Shopping Cart
    </div>

   <?php
  if(!isset($_SESSION['Array'])) {
  (
  Array
  (
       [got] => Array
        (
            [id] => MO1
            [option]
              [qty]
              [price]
    )

       [friends] => Array
          (
              [id] => MO2
            [option] 
            [qty]
              [price]
      )

       [inception] => Array
        (
            [id] => MO3
            [option] 
            [qty]
            [price]
      )

  )
  )
  }
  ?>

    <?php
    var_dump ($_POST);
    echo $_POST['option'];
    echo $_POST['id'];
    echo 'x';
    echo $_POST['qty'];
    ?>

  <script>
  $('.like-btn').on('click', function() {
     $(this).toggleClass('is-active');
  });
  </script>

  <script>
  $('.minus-btn').on('click', function(e) {
      e.preventDefault();
      var $this = $(this);
      var $input = $this.closest('div').find('input');
      var value = parseInt($input.val());

      if (value &amp;gt; 1) {
          value = value - 1;
      } else {
          value = 0;
      }

    $input.val(value);

  });

  $('.plus-btn').on('click', function(e) {
      e.preventDefault();
      var $this = $(this);
      var $input = $this.closest('div').find('input');
      var value = parseInt($input.val());

      if (value &amp;lt; 100) {
          value = value + 1;
      } else {
          value =100;
      }

      $input.val(value);
  });


  </script>
  <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>  <br><br><br><br><br><br><br><br><br>
  <br><br><br><br><br><br><br><br><br><br><br><br>
  <?php
  require_once("footer.php");
  ?>

和我的产品代码(我的表格在这里):

<div class="color">
<?php
session_start();
?>


    <!-- Keep wireframe.css for debugging, add your css to style.css -->
    <link id='wireframecss' type="text/css" rel="stylesheet" href="../wireframe.css" disabled>
    <link id='stylecss' type="text/css" rel="stylesheet" href="css/style.css">
    <script src='../wireframe.js'></script>



    <?php
$title = "WatchFlix";
$style="css/style.css";
/*include is a php fuction which place all codes(php, html) at the location where the include statement is placed.*/
?>
<?php
include("head.php")
?>
<?php
include("nav.php")
?>


<?php
$movie = array("got"=>"Game of Thrones: Season 1", "frd"=>"Friends: Season 1", "incp"=>"Inception");
?>


<main class="row">
<div class="column">
<img class="moviephoto" src="gotimage.jpg" alt="friends"/> <br>
<div class="bmove">
<form action="cart.php" method="post">
Game of thrones season 1
   <input type = "hidden" name = "id" value = "M01" />
   <br>
  <select name="option">
  <option value="HD">HD</option>
  <option value="Full HD">Full HD</option>
  <option value="Blu-ray">Blu-ray</option>
</select>
<div class="widthc">
   <button class="prod" id="minus">−&lt;/button>
<input type="number" name="qty" value="0" id="qty" min="0" max="15"/>
<button class="prod" id="plus">+</button>
<br><br>
<button class="prod" type="submit"> Submit</button>
   </form>
   </div>
</div>

</div>

<div class="column">
<h2 class="movieheading"> Game of Thrones </h2>



<div class="rating">
<h4> Rating: <span style ="font-weight:normal;">MA 15+</span></h4>
</div>
<br>

<div class="release">
<h4> Release Date: <span style ="font-weight:normal;">17 April 2011</span> </h4>
</div>
<br>

<div class="cast">
<h4> Cast: <span style ="font-weight:normal;">Peter Dinklage, Lena Headey, Emilia Clarke, Kit Harington, Sophie Turner, Maisie Williams</span></h4>
</div>
<br>

<div class="category">
<h4> Category: <span style ="font-weight:normal;"> Action/Adventure/Drama/Fantasy/Romance </span> </h4>
</div>
<br>

<div class="moviedetail">
<h4>T.V Show Details:</h4>

<p>Years after a rebellion spurred by a stolen bride to be and the blind ambitions of a mad king, Robert of the house Baratheon sits on 
the much desired Iron Throne. In the mythical land of Westeros, nine noble families fight for every inch of control and every drop 
of power. The King's Hand, Jon Arryn, is dead. And Robert seeks out his only other ally in all of Westeros, his childhood friend 
Eddard Stark. The solemn and honorable Warden of the North is tasked to depart his frozen sanctuary and join the King in the 
capital of King's Landing to help the now overweight and drunk Robert rule. However, a letter in the dead of night informs Ned 
that the former Hand was murdered, and that Robert will be next. So noble Ned goes against his better desires in an attempt to 
save his friend and the kingdoms. But political intrigue, plots, murders, and sexual desires lead to a secret that could tear the 
Seven Kingdoms apart. And soon Eddard will find out what happens when you play the Game of Thrones.</p>
</div>


<div class="column">

</div>



<br><br><br>

</main>




 <script>
const minusButton = document.getElementById('minus');
const plusButton = document.getElementById('plus');
const inputField = document.getElementById('qty');
var currentValue = 0;

minusButton.addEventListener('click', event => {
  event.preventDefault();
  currentValue = Number(inputField.value) >> 0;
  currentValue = currentValue - 1;
  if (currentValue < inputField.min) {
    inputField.value = inputField.min;
  } else {
    inputField.value = currentValue;
  }
});

plusButton.addEventListener('click', event => {
  event.preventDefault();
  currentValue = Number(inputField.value) >> 0;
  currentValue = currentValue + 1;
  if (currentValue > inputField.max) {
    inputField.value = inputField.max;
  } else {
    inputField.value = currentValue;
  }
});
</script>

<br><br><br>

<?php
include("footer.php")
?>
</div>

标签: phparraysformsarraylist

解决方案


正如我在这篇文章中回答的那样,您必须定义一个具有指定结构的数组,但您的语法不正确。您必须在以下结构中使用:

movieList[0][name] = 'got';
movieList[0][id] = 'MO1';
movieList[0][option][qty]=2;
movieList[0][option][price]=20;

movieList[1][name] = 'friends';
movieList[1][id] = 'MO2';
movieList[1][option][qty]=2;
movieList[1][option][price]=20;

movieList[2][name] = 'inception';
movieList[2][id] = 'MO3';
movieList[2][option][qty]=5;
movieList[2][option][price]=30; 

推荐阅读