PHP 表单页面数组不继续响应页面

PHP Form page array not continuing to response page

<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
    $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 的菜鸟。有一个问题,由于某种原因我无法让选定的数组继续到响应页面,它将显示从前一个数组中选择的内容,如果选择了该报纸,第二个数组中的正确价格将出现?

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

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

print 替换为 echo,如 yarwest 所说。

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

要在 PHP 中打印某些内容,您必须使用 echo(就像您在表单中所做的那样)。您还可以在带双引号的字符串中使用变量。
例如:
echo "<p> Your Newspaper is $newspaper</p>"

最重要的是,您有一个没有条件或正文的 else 语句。

解决这个问题,我相信你会更接近于正常工作。

将您的数组放在另一个文件中,然后 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'];?>