猿问

PHP:根据 POST / GET 用户输入加载内容

感谢您调查我的问题。它纯粹与 PHP POST / GET 函数有关。


这是我的代码:


索引.php


<html>

<body>

&nbsp;<p>

<center>

<form action="story_get.php" method="post">

<label for="name">Name:</label> <input type="text" name="name">

<label for="age">Age:</label><input type="number" size=2 name="age"><p>

<label for="place">Place:</label> <input type="text" name="place"><p>


 <label for="storys">Choose a story:</label>

  <select name="story" id="story">

    <option value="sea">Sea side</option>

    <option value="mount">Mountain</option>

  </select>


<p>


<input type="submit" value="Enter">

<button type="reset" value="Reset">Reset</button>

</form>


</body>

</html> 

故事获取.php


<html>

<body>



<center>

<h1>Welcome <font color=red><i><?php echo $_POST["name"]; ?></i></font>, age of <font color=red><i><?php echo $_POST["age"]; ?></i></font><br>

to the wonderfull land of <br><font color=red><i><?php echo $_POST["place"]; ?></i></font>

<?php echo $_POST["story"]; ?>




 <p><a href=index.php>Edit</a>

</body>

</html>

我的代码工作正常,但<?php echo $_POST["story"]; ?>我不想打印与用户选择的内容相关的 200 多个单词的故事。我希望您理解我想要实现的目标,并能够提出简单的解决方案。提前致谢


天涯尽头无女友
浏览 100回答 2
2回答

12345678_0001

你需要从某个地方得到那个故事。有几种方法可以到达那里。如果要从文本文件加载它:// Here we sanitize the story ID to avoid getting hacked:$story_id = preg_replace('#[^a-zA-Z0-9_ -]#', '', $_POST['story']);// Then we load the text file containing the story:$path = 'stories/' . $story_id . '.txt';$story_text = is_file($path) ? file_get_contents($path) : 'No such story!';如果你想从数据库加载它:$conn = new mysqli($servername, $username, $password, $dbname);$stmt = $conn->prepare("SELECT * FROM stories WHERE story_id = ? LIMIT 1");$stmt->bind_param("s", $_POST['story']);$stmt->execute();$result = $stmt->get_result();$story = $result->fetch_assoc();$story_text = !empty($story['story_text']) ? $story['story_text'] : 'No such story!';stories这假设您有一个名为字段story_id和的表story_text。你也可以有一个单独的文件,将你所有的故事分配到变量中(假设你真的只有几个,加载未使用的故事对性能的影响是最小的):$stories['Seaside'] = <<<EOLHere is the seaside story.EOL;$stories['Mountain'] = <<<EOLHere is the mountain story.EOL;然后在你的故事文件中,你“加载”它:$story_text = !empty($stories[$_POST['story']) ? $stories[$_POST['story']] : 'No such story!';然后(使用上述任何选项),只需:<?php echo $story_text; ?>在上述选项中,如果您正在寻找简单且易于维护的东西,我会选择文本文件加载来获取您的故事文本。祝你讲故事好运。:)假设您想在故事中使用表单变量。您需要使用标记,例如{{ place }},并在输出故事文本之前替换它们:$story_text = str_replace('{{ name }}', $_POST['name'], $story_text);$story_text = str_replace('{{ place }}', $_POST['place'], $story_text);这会将“曾几何时有 {{ name }} 探索 {{ place }}...”变成“曾几何时有 Светослав 探索堪察加半岛...”等。

哔哔one

...instead of <?php echo $_POST["story"]; ?> I want to print 200+ words story relating to what User has chosen.使用条件if()或switch语句查看您的 $_POST['story'] 是否已设置并等于您的故事选项之一。//--> ON story_get.php&nbsp;//--> (Provided the $_POST variable in fact has the values assigned to the global array)//--> use var_dump($_POST) on story_get.php to check if the global $_POST array has&nbsp;//--> key/value pairs coming from your index.php page// set variables that have your story information.&nbsp;$seaside = //--> Your story about the sea side$mountain = //--> Your story about the mountain$output = null; //--> Empty variable to hold display info from conditional//--> Now to see if the form element that selects story is set using issetif(isset($_POST['story']){&nbsp; //--> Now that we know the input in the form that holds the value for story isset&nbsp; //--> Check to see if it is set and then declare a variable and assign it to that variable&nbsp; $story = $_POST['story'];&nbsp; if($story === 'sea'){&nbsp; &nbsp; $output = $seaside;&nbsp; }elseif($story === 'mount'){&nbsp; &nbsp; $output = $mountain;&nbsp; }else{&nbsp; &nbsp; $output = //--> Set a default setting that displays output here if neither story is selected.&nbsp; }}$output在 html 文档中回显您希望显示故事内容的变量<div>&nbsp; <?=$output?>&nbsp; <!--// OR //-->&nbsp; <?php echo $output; ?></div>
随时随地看视频慕课网APP
我要回答