duan20081202 2013-08-05 13:26
浏览 12
已采纳

在文本框后定义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?

  • 写回答

4条回答 默认 最新

  • douchen2025 2013-08-05 13:59
    关注

    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";
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?