PHP表单页面数组不继续响应页面 报纸 要多少钱

PHP表单页面数组不继续响应页面 报纸 要多少钱,php,arrays,response,Php,Arrays,Response,这张表格可以让你知道一份报纸要多少钱 你对哪份报纸感兴趣? 按以获得价格 响应页面 <html> <head> <title>Newspaper</title> </head> <body> <h1>How much does it cost</h1> <p>This form will allow you to

这张表格可以让你知道一份报纸要多少钱

你对哪份报纸感兴趣?

按以获得价格

响应页面

<html>
    <head>
        <title>Newspaper</title>
    </head>
    <body>
        <h1>How much does it cost</h1>
        <p>This form will allow you to find out how much a newspaper costs</p>
        <form name="choose" action="response.php" method="POST">
            <p>Which newspaper are you interested in? 
            <?php
                $newspaper = array("The Guardian", "The Times", "The Sun", "The Mirror");

                echo '<select name="newspaper">';
                for ($i = 0; $i < count($newspaper); $i++) {
                    echo '<option value="' . $i . '">' ;
                    echo $newspaper[$i] ;
                    echo '</option>';
                }
                echo '</select>';
            ?> 
            </p>
            <p>Press to get the price<input type="submit" name="continue" value="continue" /></p>
        </form>
    </body>
</html>

报纸
要多少钱

嗨,这里有一个关于php的noob。有一个问题,由于某种原因,我无法让所选的阵列继续到响应页面,其中将显示从上一个阵列中选择的内容,如果选择了该报纸,将显示第二个阵列中的正确价格

您使用报纸的位置作为选项值中的键。这意味着您可以这样使用它:

<?php
    $newspaper=$_POST['newspaper'];
    $newspaperNames= array ("The Guardian", "The Times", "The Sun", "The Mirror");
    $newspaperPrice= array('0.9','1','0.5','0.5');
?>

<html>
    <head>
        <title>Newspaper</title>
    </head>
    <body>
        <h1>How much does it cost</h1>
        <?php
            print "<p> Your Newspaper is " .$newspaper. "</p>";

            print "<p>Your  Newspaper is " .$newspaper. "</p>";
            if ($newspaper== "The Guardian") {
                print "<p>Your newspaper cost " .$newspaperPrice[0]."</p>";
            } else
        ?>
    </body>
</html>

报纸
要多少钱
  • 注意:请检查输入是否已发布(
    isset()

    • echo
      替换
      print
      s,就像yarwest说的那样


      要在PHP中打印某些内容,您必须使用
      echo
      (就像您在表单中所做的那样)。您还可以在带有双引号的字符串中使用变量。
      例如:
      echo“您的报纸是$paper

      除此之外,还有一个没有条件或主体的else语句


      修复此问题,我相信您将更接近工作状态。

      将您的数组放入另一个文件中,并从以下两个文件中包括它:

      <?php
          $newspaper = $_POST['newspaper'];
          $newspaperNames= array ("The Guardian", "The Times", "The Sun", "The Mirror");
          $newspaperPrice= array('0.9','1','0.5','0.5');
      ?>
      
      <html>
          <head>
              <title>Newspaper</title>
          </head>
          <body>
              <h1>How much does it cost</h1>
              <?php
                  echo "<p>Your  Newspaper is " .$newspaperNames[$newspaper]. "</p>";
                  echo "<p>Your newspaper cost " .$newspaperPrice[$newspaper]."</p>"; 
              ?>
          </body>
      </html>
      
      
      ...
      
      在您的回复页面中

      <p>Which newspaper are you interested in?
      <select name="newspaper">
      <?php
      include 'papers.inc';# or whatever
      foreach ($newspapers as $index => $newspaper) {
          echo '<option value="', $index, '">', $newspaper['name'], '</option>';
      }
      ?>
      ...
      
      
      ...
      你的报纸很贵,要多少钱
      
      对于调试,您可以使用print\r($\u POST)True。我认为最好使用多维数组而不是两个数组。但这更多地取决于脚本的使用方式和代码创建者的“风格”。
      <p>Which newspaper are you interested in?
      <select name="newspaper">
      <?php
      include 'papers.inc';# or whatever
      foreach ($newspapers as $index => $newspaper) {
          echo '<option value="', $index, '">', $newspaper['name'], '</option>';
      }
      ?>
      ...
      
      <?php
      include 'papers.inc';
      $index = $_POST['newspaper'];
      ?>
      ...
      <p>Your newspaper is <?php echo $newspapers[$index]['name'];?>, which costs <?php echo $newspapers[$index]['price'];?>