2017-01-08 62 views
1
<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窗體頁陣列不在這裏繼續響應頁面

<?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> 

你好樣的一個小白用PHP。有一個問題,出於某種原因,我無法讓選定的數組繼續到響應頁面,它將顯示從前一個數組中選擇的內容,並且如果選擇了該報紙,第二個數組中的正確價格將出現?

回答

-1

您使用報紙的位置作爲期權價值的關鍵。這意味着你可以使用它像這樣:

<?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> 
  • 注:請檢查輸入張貼(isset()
+0

爲了調試,你可以使用的print_r ($ _POST) – Phil

+1

正確。我認爲最好使用多維數組而不是兩個數組。但這更多取決於這個腳本將被使用的方式以及代碼創建者的「樣式」。 –

1

echo更換print S,像yarwest說。

<?php 
echo "<p>Your Newspaper is $newspaper</p>"; 
if ($newspaper == "The Guardian") { 
    echo "<p>Your newspaper cost {$newspaperPrice[0]}</p>"; 
} 
?> 
1

要在PHP中打印某些東西,您必須使用echo(就像您在表單中所做的那樣)。你也可以在帶有雙引號的字符串中使用變量。
例如:
echo "<p> Your Newspaper is $newspaper</p>"

最重要的是,你有沒有疾病或身體的其他聲明。

解決這個問題,我相信你會更接近某些工作。

1

來自把你的陣列中的另一個文件,並include它:

<?php 
$newspapers = [ 
    [ 
     'name' => 'The Guardian', 
     'price' => '0.9', 
    ], 
    [ 
     'name' => 'The Times', 
     'price' => '1', 
    ], 
    ... 
] 

在選擇頁面

<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'];?>