在文本框后定义php var

So I have a variable well defined in a php page and I'm using it in an HTML page using include. I am currently building a page where I can change the Var ( because it's a long text, more than one actually, and to change them it will be nice to have a page with a layout just for that) so I'm using a textbox and a submit button just like this:

<?php
$titre= 'Bienvenido a PARIS EXPERT LIMOUSINE !  ' ;
?>

<form method="post">
 Titre: <input name="titre" type="text" id="titre" value="<?php echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<input type="submit" name="submit">
 </form>

<?php
if (isset($_POST['submit']))
{
$titre = $_POST['titre'];
echo($titre);
}
?>

The problem is that in the echo it shows the new text but if I do a refresh it will show the old one... any ideas how can I do this?

EDIT: Added extra fields and data handler. See extra code below original answer.


Here is some code I came up with to write content to a file.

Note: To add to the file with content written one under the other, use the a or a+ switch.

To create and write content to file and overwrite previous content, use the w switch.

This method uses the fwrite() function.

(tested)

Added to OP's code: action="write.php"

FORM

<?php

$titre= 'Bienvenido a PARIS EXPERT LIMOUSINE !  ' ;
?>

<form method="post" action="write.php">
Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); } 
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">


<input type="submit" name="submit">
 </form>

PHP write to file handler (write.php)

This example uses the w switch.

<?php
if (isset($_POST['submit']))
{
$titre = $_POST['titre'];
echo($titre);
}
?>

<?php

$filename = "output.txt"; #Must CHMOD to 666 or 644
$text = $_POST['titre']; # Form must use POST. if it uses GET, use the line below:
// $text = $_GET['titre']; #POST is the preferred method

$fp = fopen ($filename, "w" ); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
    fwrite ($fp, $text. "
");
    fclose ($fp);
    echo ("File written");
}
else {
    echo ("File was not written");
}

?>


EDIT: Added extra fields and data handler.

Extra fields can be added, and must be followed in the same fashion in the file handler.

NEW FORM with extra fields

File data example: test | email@example.com | 123-456-7890

<?php

$titre= 'Bienvenido a PARIS EXPERT LIMOUSINE !  ' ;
?>

<form method="post" action="write.php">
Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); } 
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">

<br>
Email: <input name="email" size="50" maxlength="50">

<br>
Telephone: <input name="telephone" size="50" maxlength="50">


<input type="submit" name="submit">
 </form>

<?php
if (isset($_POST['submit']))
{
$titre = $_POST['titre'];
echo($titre);
}
?>

PHP write to file handler

<?php

$titre = $_POST['titre'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$data = "$titre | $email | $telephone";

$fp = fopen("data.txt", "a"); // a-add append or w-write overwrite

if ($fp) {
    fwrite ($fp, $data. "
");
    fclose ($fp);
    echo ("File written successfully.");
}

else{
  echo "FAILED";
}

?>

Try this :

Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); } 
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">

If you need to keep your value for ever, you should store it in a database or save it in a file (could be .txt).

[EDIT]

Here is the code for .txt solution (you first create a file.txt in the same folder):

<?php
$file = 'file.txt';

$lines  = file("file.txt");
if (!isset($lines[0])) {$titre='Bienvenido a PARIS EXPERT LIMOUSINE !  ';}
else {$titre=$lines[0];}
?>
<form method="post">
 Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); } 
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<input type="submit" name="submit">
 </form>

<?php
if (isset($_POST['submit']))
{
echo($_POST['titre']);
$titre = $_POST['titre']."
".$titre;
file_put_contents($file, $titre);
}

?>

Hope it helps :)

this is normal because you're showing the new content upon form submission. When you refresh the page, unless you tell it to send the POST data again with the refresh (which the browser asks you for confirmation), your form (and hence the input field) will have nothing in.

<?php

if(!($titre = file_get_contents("filename.txt"))){
  $titre= 'Bienvenido a PARIS EXPERT LIMOUSINE !  ' ;
}

?>

<form method="post">
  Titre: <input name="titre" type="text" id="titre" value="<?php echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
  <input type="submit" name="submit">
</form>

<?php
if (isset($_POST['submit'])) {
  $titre = $_POST['titre'];
  if(@file_put_contents("filename.txt", $titre))){
    echo 'Success - var stored.';
  } else { echo 'Some error.'; }
  echo($titre);
}
?>