I have a form on my website for users to submit blog
Within that form, I have a drop-down select menu for different blog categories.
E.g.U.S. News
, World News
, Economic News
, etc......'
Each form option allows for a single value input.
In my php, I would like to create two separate variables for that 'category' form field and use each value for two different tasks.
TASK #1:
I would like to use the selected value as part of the URL path to post/store the particular blog files.
For this task, I have been able to use the values as they are embedded in the form.
And... in my php, I created the variable $catPath
with a category array to handle this task.
For example:
*If the user selects "U.S. News" as the category their blog should appear in, the value for U.S. News is set to 'US'.
So, in the example above... the 'US' value would be used as such;
file_put_contents("../Blogs/$catPath/$unique_id.html", ob_get_contents());
Ex: ../Blogs/US/0001.html
So for, so good.....
However...
TASK #2:
I would like to create a separate variable named $catTag
to be used within the blog header itself.
For Example:
Sticking with the example above... if the user selects 'U.S. News' in the form... I would like the full category text "U.S. News" to appear in the header of the blog, not as 'US' as it is in the url path.
DEMO:
I included a very stripped down demo version of the form and the php below.
In the php, please notice I have created the variable $catTag
, and I am successfully able to echo the adjusted value in php echo example 'A'.
However, when I attempt to use the variable $catTag
as a simple php echo, as in php example 'B'... it still echos the original value, not the adjusted value.
I don't want to have to create two redundant fields in my form for 'category'.
I need to be able to use a separate variable so it echos the adjusted value.
I apologize in advance for my limited knowledge with this. I hope I have explained this well enough to be understood.
I have tried several things, but so far, I have had no luck.
Any advice would be greatly appreciated.
THE FORM:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="MyPHP.php" method="post" enctype="multipart/form-data">
<label>Select A Category: </label>
<select name="category">
<option value="US">U.S. News</option>
<option value="World">World News</option>
<option value="Government-Politics">Government & Politics</option>
<option value="Economic">Economic News</option>
<option value="National-Weather">National Weather News</option>
<option value="Entertainment">Entertainment News</option>
<option value="Science-Technology">Science & Technology</option>
<option value="Sports">Sports News</option>
<option value="Health">Health News</option>
</select>
<br>
<br>
<input name="Submit" type="submit" value="Submit">
</form>
</body>
</html>
THE PHP: (MyPHP.php)
<?php ob_start(); ?>
<html>
<head>
</head>
<body>
<!---| Start php Example A |---------------------------->
<?php
$catTag = $_REQUEST['category'];
if ($catTag == "US") { echo "U.S. News"; }
if ($catTag == "World") { echo "World News"; }
if ($catTag == "Government-Politics") { echo "Government & Politics"; }
if ($catTag == "Economic") { echo "Economic News"; }
if ($catTag == "National-Weather") { echo "National Weather News"; }
if ($catTag == "Entertainment") { echo "Entertainment News"; }
if ($catTag == "Science-Technology") { echo "Science & Technology"; }
if ($catTag == "Sports") { echo "Sports News"; }
if ($catTag == "Health") { echo "Health News"; }
?><br>
<!---| End php Example A |---------------------------->
<!---| Start php Example B |------------------------------>
<?php echo $catTag; ?>
<!---| End php Example B |-------------------------------->
</body>
</html>
<?php echo '';
$catPath = $_REQUEST['category'];
$valid_categories = array('US','World','Government-Politics','Economic','National-Weather','Entertainment','Science-Technology','Sports','Health');
if (!in_array($catPath, $valid_categories)) { die("Invalid Category"); } file_put_contents("$catPath.html", ob_get_contents()); ?>