PHP:基于POST/GET用户输入加载内容

PHP:基于POST/GET用户输入加载内容,php,post,get,Php,Post,Get,谢谢你调查我的问题。它完全与PHP POST/GET函数相关 这是我的密码: index.php <html> <body> &nbsp;<p> <center> <form action="story_get.php" method="post"> <label for="name">Name:</label> <input type="text" name="name"> <labe

谢谢你调查我的问题。它完全与PHP POST/GET函数相关

这是我的密码:

index.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> 


姓名:
年龄:
地点:
选择一个故事:
海边
山

重置
story_get.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>

欢迎光临,年龄

我的代码运行良好,但我不想打印与用户选择内容相关的200多个单词的故事。我希望你能理解我想要实现的目标,并能提出简单的解决方案。提前感谢

…我不想打印200多字的关于用户选择内容的故事。

使用有条件的
if()
switch
语句查看您的$\u POST['story']是否已设置并等于您的故事选项之一


//-->关于story_get.php
//-->(前提是$\u POST变量实际上具有分配给全局数组的值)
//-->在story\u get.php上使用var\u dump($\u POST)检查全局$\u POST数组是否具有
//-->来自index.php页面的键/值对
//设置包含故事信息的变量。
$seaside=/-->您关于海边的故事
$mountain=/-->您关于这座山的故事
$output=null;//-->空变量用于保存条件中的显示信息
//-->现在看看选择故事的表单元素是否使用isset设置
如果(isset($_POST['story'])){
//-->现在我们知道了保存story isset值的表单中的输入
//-->检查是否已设置,然后声明一个变量并将其分配给该变量
$story=$_POST['story'];
如果($story==‘sea’){
$output=$seaside;
}elseif($story==='mount'){
$output=$mountain;
}否则{
$output=/-->设置默认设置,如果未选择任何一个故事,则在此处显示输出。
}
}
回显变量
$output
,您希望在html文档中显示您的故事内容


…我想打印200多个单词的故事,讲述用户选择的内容。

使用有条件的
if()
switch
语句查看您的$\u POST['story']是否已设置并等于您的故事选项之一


//-->关于story_get.php
//-->(前提是$\u POST变量实际上具有分配给全局数组的值)
//-->在story\u get.php上使用var\u dump($\u POST)检查全局$\u POST数组是否具有
//-->来自index.php页面的键/值对
//设置包含故事信息的变量。
$seaside=/-->您关于海边的故事
$mountain=/-->您关于这座山的故事
$output=null;//-->空变量,用于保存来自条件的显示信息
//-->现在看看选择故事的表单元素是否使用isset设置
如果(isset($_POST['story'])){
//-->现在我们知道了保存story isset值的表单中的输入
//-->检查是否已设置,然后声明一个变量并将其分配给该变量
$story=$_POST['story'];
如果($story==‘sea’){
$output=$seaside;
}elseif($story==='mount'){
$output=$mountain;
}否则{
$output=/-->设置默认设置,如果未选择任何一个故事,则在此处显示输出。
}
}
回显变量
$output
,您希望在html文档中显示您的故事内容


你需要从某个地方得到这个故事。有几种方法可以达到目的


如果要从文本文件加载,请执行以下操作:

// 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\u id
story\u text


您还可以有一个单独的文件,将所有故事分配到变量中(假设您确实只有几个,加载未使用的故事对性能的影响最小):


然后(使用上述任何选项),只需:


这会把“从前有{{name}}探索{{place}}…”变成“从前有{place}探索勘察加半岛…”等等。

你需要从某处获得这个故事。有几种方法可以到达那里


如果要从文本文件加载,请执行以下操作:

// 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\u id
story\u text


您还可以有一个单独的文件,将所有故事分配到变量中(假设您确实只有几个,加载未使用的故事对性能的影响最小):


然后(使用上述任何选项),只需:


这将把“从前有{{{name}}探索{{{place}}}…”变成“从前有{name}探索勘察加半岛…”,等等。

200多个单词。它是由用户以这种形式插入的,还是您从数据源获得的?数据源,先前存在的故事,其中只更改名称、年龄和地名。不要使用
,它们已经过时了。谢谢您!如果您可以在文本中添加占位符,您可能可以使用
>sprintf()
或sprint家族的其他成员。200多个单词。它是由用户以那种形式插入的,还是您从数据源获得的?数据源,先前存在的故事,其中只更改名称、年龄和地名。不要使用
,它们已经过时了。谢谢您!如果您可以在文本中放置占位符,您可能会看到能够使用
sprintf()实现这一点
或sprint家族的其他成员。很好的例子,到目前为止,我设法从文本文件中获取了一个故事,但也意识到浏览器没有从sea.txt中读取代码,而不是从.txt中读取代码。我可以使用.php吗?你不应该在
sea.txt
中需要php代码。只需将文本存储在那里。我在
<?php echo $story_text; ?>
$story_text = str_replace('{{ name }}', $_POST['name'], $story_text);
$story_text = str_replace('{{ place }}', $_POST['place'], $story_text);