我如何实现此数组以保存表单中的数据

时间:2018-10-08 07:32:59

标签: php arrays forms arraylist

我一直试图将信息从表单发送到我的数组,以便它可以保存该信息并在需要时显示它。 iv已经设法从一个页面发送数据并在另一页面上回显它,但是我希望将其存储在数组中,从而一次显示多个问题。有人告诉我使用这种样式的数组来完成一个朋友的工作(请参阅下面的数组代码以了解想法),但是它给了我一个语法错误,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">−</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>

1 个答案:

答案 0 :(得分:0)

我在this post中回答,您必须使用指定的结构定义一个数组,但是语法不正确。 您必须使用以下结构:

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;