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
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 calledstories
with fieldsstory_id
andstory_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.
...instead of <?php echo $_POST["story"]; ?> I want to print 200+ words story relating to what User has chosen.
Use a conditionalif()
orswitch
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>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.