PHP: Loading content based on POST / GET user input
thank you for looking into my question. It's purely related to PHP POST / GET functions.
Here is my code :
index.php
<html>
<body>
<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>
My code is working fine, but instead of <?php echo $_POST["story"]; ?>
I want to print 200+ words story relating to what User has chosen. I hope You understand what I'm trying to achieve, and be able to suggest easy solutions. Thanks in advance
Answer
Solution:
You need to get that story from somewhere. A couple of ways to get there.
If you want to load it from a text file:
// 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!';
If you want to load it from database:
$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!';
This assumes you have a table called stories
with fields story_id
and story_text
.
You could also have a separate file with all your stories assigned into variables (assuming you really just have a few, the performance impact of loading unused stories is minimal):
$stories['Seaside'] = <<<EOL
Here is the seaside story.
EOL;
$stories['Mountain'] = <<<EOL
Here is the mountain story.
EOL;
And then in your story-telling file, you "load" it:
$story_text = !empty($stories[$_POST['story']) ? $stories[$_POST['story']] : 'No such story!';
Then (with any of the above options), simply:
<?php echo $story_text; ?>
Of the options above, I would choose the text-file loading for sourcing your story text, if you're looking for something simple and easy to maintain. Good luck in telling your stories. :)
Assuming you want to use the form variables inside your stories. You'll want to use tokens, something like {{ place }}
, and replace them before you output the story text:
$story_text = str_replace('{{ name }}', $_POST['name'], $story_text);
$story_text = str_replace('{{ place }}', $_POST['place'], $story_text);
This would turn "once upon a time there was {{ name }} exploring {{ place }}..." into "once upon a time there was ?????�?�?????�?�?? exploring Kamchatka Peninsula...", etc.
Answer
Solution:
...instead of <?php echo $_POST["story"]; ?> I want to print 200+ words story relating to what User has chosen.
Use a conditional if()
or switch
statement to see if your $_POST['story'] is set and equal to one of your options for story.
//--> ON story_get.php
//--> (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
//--> key/value pairs coming from your index.php page
// set variables that have your story information.
$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 isset
if(isset($_POST['story']){
//--> Now that we know the input in the form that holds the value for story isset
//--> Check to see if it is set and then declare a variable and assign it to that variable
$story = $_POST['story'];
if($story === 'sea'){
$output = $seaside;
}elseif($story === 'mount'){
$output = $mountain;
}else{
$output = //--> Set a default setting that displays output here if neither story is selected.
}
}
Echo the variable $output
where you wish to display your story content within the html doc
<div>
<?=$output?>
<!--// OR //-->
<?php echo $output; ?>
</div>
Source