0

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?

0

4 Answers 4

1

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. "\n");
    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 protected] | 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. "\n");
    fclose ($fp);
    echo ("File written successfully.");
}

else{
  echo "FAILED";
}

?>
Sign up to request clarification or add additional context in comments.

5 Comments

it works.. however is it possible to have several vars in the same text file?
@Alpha Sure. It depends what vars you would like to have. What would you like to do?
I have 9 var and they should do exactly the same as the $titre.. ofc I'll have 9 textboxes ans 9 buttons
@Alpha There are two ways to go about this, pre-define each variable < (more control), or use a foreach.
@Alpha Edited my answer with extra options. You will have to add/edit your own fields. (tested)
1
<?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);
}
?>

Comments

1

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']."\n".$titre;
file_put_contents($file, $titre);
}

?>

Hope it helps :)

4 Comments

If I refresh after changing the value the old value comes up.. He doesn't keep the new one..
It works!! thanks... BTW is it possible to have several variables inside the same .txt file?
Yes, Check my edit :) Now it keeps all previous values in file.txt and displays last one
If you would like yo do same thing with other variables than titre, you can either put each var in a separate file or store all vars in the same file with a specific delimiter (newline or ; etc...);
0

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.

7 Comments

But I need to save the new value.. not only save for the moment but change it 4ever..
@ImaneFateh is right. if you need to store the value, then you need to store it for later use, which is not possible through HTML
@ImaneFateh I like the idea of a file.. but what type of file and how?
You can use .txt file, use the function file_put_contents()
@ImaneFateh or the OP can use fwrite() php.net/manual/en/function.fwrite.php
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.