I want users to be able to dynamicly change the title of the websites.
I've tried ALL of the Dynamic Website Title questions on Stackoverflow, but none resolves my issues.
This is my HTML
<fieldset>
<legend style='width:500px;'><h3>Website Title</h3></legend>
<label><h4> Change Website Title </h4></label>
<form action='Config.php' method='POST'>
<input type='text' name='websitetitle' placeholder='Website Title'/> <br />
<input type='submit' value='Update' class='btn btn-small btn-primary' name='update'>
</form>
</fieldset>
Config.php
<?php
if(isset($_REQUEST['update'])) {
$title=strip_tags(stripslashes($_REQUEST['websitetitle']));
}
Global.php (Where the title is located)
<title><?php include 'Config.php'; echo $title; ?> </title>
Thanks in advance!
Looks like you have your variable websitetitle
in $_REQUEST
wrong ??? As I don't "believe" you can pass the value of a "submit".
<fieldset>
<legend style='width:500px;'><h3>Website Title</h3></legend>
<label><h4> Change Website Title </h4></label>
<form action='Config.php' method='POST'>
<input type='text' name='websitetitle' placeholder='Website Title'/> <br />
<input type='submit' value='Update' class='btn btn-small btn-primary' name='update'>
</form>
</fieldset>
Then
<?php
if(isset($_REQUEST['websitetitle'])) {
$title=strip_tags(stripslashes($_REQUEST['websitetitle']));
}
Then
<title><?php include 'Config.php'; echo $title; ?> </title>
<?php
// config.php
if(isset($_POST['update'])) {
$title=strip_tags(stripslashes($_POST['websitetitle']));
}
?>
<title><?php require_once('Config.php'); echo $title; ?> </title>
<fieldset>
<legend style='width:500px;'><h3>Website Title</h3></legend>
<label><h4> Change Website Title </h4></label>
<form method='POST'>
<input type='text' name='websitetitle' placeholder='Website Title'/> <br />
<input type='submit' value='Update' class='btn btn-small btn-primary' name='update'>
</form>
</fieldset>
What you did is redirecting your user to the config.php after submitting the form. what you want to do is to reload your actual page and then let the included script do what it should before the title tag is set.
The answer above looks ok, but I would use php sessions. You could get rid of global.php and change config.php to
<?php
if (issest($_REQUEST['websitetitle'])) {
$_SESSION['title'] = $_REQUEST['websitetitle'];
}
?>
Then place $_SESSION['title']
where you need a dynamic title. Also, be sure to add session_start();
at the VERY top of the document.
No where in any of your code did you save the value to be re-iterated later. Every time your browser loads a new page it forgets everything that you didn't specifically tell it to keep.
You can write the information to a database, a file, a cookie or a session.
For a session example:
<?php
$title = ( isset($_SESSION) && isset($_SESSION['title']) ) ? $_SESSION['title'] : 'Original, unaltered page title here';
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
Page that receives the form:
<?php
if ( isset($_REQUEST['websitetitle']) && !empty($_REQUEST['websitetitle']) )
{
if ( !isset($_SESSION) ) session_start();
$_SESSION['title'] = $_REQUEST['websitetitle'];
}
The reason for the !isset($_SESSION)
condition to session_start();
is that if you call session_start()
when there is already an active session it throws an error. If you are on a production system that is configured correctly then this error will be tucked away in a log file out of site, but it will still be there filling up your log files with pointless warnings. It is possible to configure PHP to automatically generate sessions, making our calling session_start()
pointless as well.
In my example, I don't start a session unless the person submits the form. You don't need a session for every tom, dick, harry and bot that crawls across your page. You only need sessions for the ones that click "Update".
Keep in mind that sessions are not the right answer if you want the site title to change permanently and/or for all users. A session is an exclusive storage of information between the server and client. If I change the site title to "Hello World!" then I will see "Hello World!" for the duration of my session, but you will not.