donglao9606 2018-10-23 21:04
浏览 28
已采纳

将文本文件下载到服务器的内存但不起作用(html,php)

I'm doing work for my school about programming a web page, and I would like to have a page where once the user put some data in a textfile, this automatically downloads to where I want in my computer. My page is in HTML but I'm trying to do this action in PHP. The problem is, that I can't make the code run even though I have checked various times and everything should be fine. What do I have to do to solve this problem?

Thanks :D

This is the main page:

 <!DOCTYPE html>
<html>
<head>
    <title> Recuperació i actualització de contrasenyes</title>
    <meta charset="utf-8">
    <meta name="description" content=" Aquest lloc és un espai on es pot recuperar les contrasenyes dels clients.">
    <meta name="keyboards" content="Banc, Contrasesnyes, Clients, Manianet" >
        <script type="text/javascript">
            function animaMissatge() {
                $("missatgeError").animate{
                    opacity: 0.25,
                    left: "+=50",
                    height:"toggle"
                }, 1500, function(){}
            };
            function comprovaFormulari() {
                var pmissatge = document.getElementById("missatgeError")
                var nom = document.getElementById("usuari").value;
                if (nom == "") {
                    pmissatge.innerHTML = "Has d'introduir l'usuari";
                }
                else {
                    var comentari = document.getElementById("contrasenya").value;
                    if (comentari == "") {
                        pmissatge.innerHTML = "Has d'introduir la contrasenya";
                    }
                    else{
                        var form = document.getElementById("blog.txt");
                        form.submit();
                    }                           }
                }

                return false;
            }

        </script>
</head>
<body>
<header>
    <nav>
        Pàgina web oficial del banc manianetià. Les seves dades estan segures amb nosaltres!
    </nav>
</header>
<article>
<h1 id="h1_title">
Actualització de contrasenya
</h1>
<section id="section_1">
<div id="column_1">
    <p>
    A través d'aquesta pàgina web, podreu recuperar o actualitzar la vostra contrasenya de manera gratuita i totalment fiable. Aixi, podreu iniciar sessió més cómodament i sense cap problema.
    </p>
</div>
    <h3>
    Inserti aqui les seves dades:
    </h3>
</section>

<section>
        <form method="post" action="prova.php">
            <p>Usuari:<input type="text" name="usuari" id="usuari"></p>
            <p>Contrasenya:<input type="text" name="contrasenya" id="contrasenya"></p>
            <input type="button" value="envia" name="envia" onclick="comprovaFormulari();">
            <input type="button" value="anima" name="anima" onclick="animaMissatge()">
        </form>
    <p id="missaatgeError">
    </p>
</section>

<section>
    <p>Gràcies a la vostra fidelització, hem aconseguit:</p>
        <ul id="llista_1">
            <li> Un banc segur, fiable i compromés amb els clients. </li>
            <li> Noves hipoteques amb un 0% d'interessos. </li>
            <li> Premi a la millor pàgina web de l'any per l'associació "El Tribunal de Recerca" </li>
        </ul>
</section>B0
<footer id="footer_1">
    <p> <i> Si vol més informació, faci click <a href="https://www.lescorts.manyanet.org/"><u>aqui</u></a></i> </p>
</footer>
</article>

</body>
</html>

And this is prova.php

 <?php
    $fitxer = "blog.txt";
    $nom = $_GET["usuari"];
    $msg = $_GET["contrasenya"];

    file_put_contents($fitxer, $nom.": ".$msg,"</br", FILE_APPEND);

?>

<!DOCTYPE html>
<html>
<body>
<h1> Comentaris enviats!</h1>
<p> Gràcies per la teva participació!</p>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dongshou6041 2018-10-23 21:20
    关注

    Ok, I was going to just edit the answer, but this requires a full rewrite.

    There were multiple errors, all the way through the code. I'll start from the top:

    Main Page

    1. You never imported jQuery. You use jQuery for the animation, but never required it.
    2. There were multiple syntax errors within the animaMissatge function.
    3. The form that you were calling in the comprovaFormulari function didn't exist. I replaced it with the real one.
    <!DOCTYPE html>
    <html>
    <head>
        <title>Recuperació i actualització de contrasenyes</title>
        <meta charset="utf-8">
        <meta name="description" content=" Aquest lloc és un espai on es pot recuperar les contrasenyes dels clients.">
        <meta name="keyboards" content="Banc, Contrasesnyes, Clients, Manianet" >
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script type="text/javascript">
          function animaMissatge() {
              $("missatgeError").animate({
                  opacity: 0.25,
                  left: "+=50",
                  height:"toggle"
              }, 1500);
          }
          function comprovaFormulari() {
              var pmissatge = document.getElementById("missatgeError")
              var nom = document.getElementById("usuari").value;
              if (nom == "") {
                  pmissatge.innerHTML = "Has d'introduir l'usuari";
              }
              else {
                  var comentari = document.getElementById("contrasenya").value;
                  if (comentari == "") {
                      pmissatge.innerHTML = "Has d'introduir la contrasenya";
                  }
                  else{
                      var form = document.getElementById("msForm");
                      form.submit();
                  }                           
              }
              return false;
          }
        </script>
      </head>
      <body>
        <header>
          <nav>
            Pàgina web oficial del banc manianetià. Les seves dades estan segures amb nosaltres!
          </nav>
        </header>
        <article>
          <h1 id="h1_title">Actualització de contrasenya</h1>
          <section id="section_1">
            <div id="column_1">
              <p>A través d'aquesta pàgina web, podreu recuperar o actualitzar la vostra contrasenya de manera gratuita i totalment fiable. Aixi, podreu iniciar sessió més cómodament i sense cap problema.</p>
            </div>
            <h3>Inserti aqui les seves dades:</h3>
          </section>
          <section>
            <form method="post" action="prova.php" id="msForm">
              <p>Usuari:<input type="text" name="usuari" id="usuari"></p>
              <p>Contrasenya:<input type="text" name="contrasenya" id="contrasenya"></p>
              <input type="button" value="envia" name="envia" onclick="comprovaFormulari();">
              <input type="button" value="anima" name="anima" onclick="animaMissatge()">
            </form>
            <p id="missaatgeError"></p>
          </section>
          <section>
            <p>Gràcies a la vostra fidelització, hem aconseguit:</p>
            <ul id="llista_1">
              <li> Un banc segur, fiable i compromés amb els clients. </li>
              <li> Noves hipoteques amb un 0% d'interessos. </li>
              <li> Premi a la millor pàgina web de l'any per l'associació "El Tribunal de Recerca" </li>
            </ul>
          </section>
          <footer id="footer_1">
            <p> <i>Si vol més informació, faci click <a href="https://www.lescorts.manyanet.org/"><u>aqui</u></a></i> </p>
          </footer>
        </article>
      </body>
    </html>
    

    prova.php

    There were multiple errors in here too.

    1. The two variables you named, $nom and $msg, were calling $_GET variables. Since they were sent through a form, they are $_POST variables.
    2. The file_put_contents function was badly written. I replaced the </br with a , and put the content into one pair of quotes.
    <?php
        $fitxer = "blog.txt";
        $nom = $_POST["usuari"];
        $msg = $_POST["contrasenya"];
    
        file_put_contents($fitxer, "$nom: $msg
    ", FILE_APPEND);
    
    ?>
    
    <!DOCTYPE html>
    <html>
    <body>
    <h1> Comentaris enviats!</h1>
    <p> Gràcies per la teva participació!</p>
    </body>
    </html>
    

    Working example: https://tricksfortheweb.com/stack/a64f.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制