【问题标题】:Echoing in php without changing pages在php中回显而不更改页面
【发布时间】:2011-04-06 21:33:06
【问题描述】:

我的网站上有一个简单的输入表单,供人们输入信息以提交。在他们不输入任何内容的情况下,代码如下所示:

这是form.php

if ($_POST['q'])  == NULL ){
   echo "Please enter your information"

代码效果很好,但它会将用户发送到 form.php 并带有回显,我希望在输入框正下方的主页 index.html 上回显它 - 基本上这样它就不会离开这页纸。这在 php 中是否可行,或者我需要一些 javascript。我会寻找方法来做到这一点,但我不知道这个方法叫什么。

谢谢!

【问题讨论】:

    标签: php html echo


    【解决方案1】:

    不要在 url 中设置操作,它会提交给它自己,如果仍然不起作用,你将需要重写规则。

    【讨论】:

      【解决方案2】:

      如果您不想离开该页面,则需要使用 Javascript。在表单中添加一个onSubmit,然后让你在那里调用的函数返回false,如果输入不完整并且不应该提交表单。

      【讨论】:

      • 关于如何在网上完成这项工作有什么好的参考吗?
      • @user695653 thesitewizard.com/archive/validation.shtml 然后您可以在 div、p 或任何具有给定 id 的内容中输入一些文本,如下所示:tizag.com/javascriptT/javascript-getelementbyid.php(而不是警报)
      • 你不需要 javascript 主要是因为如果禁用了 javascript 会怎样
      • @Laurence Burke:我认为最好的选择是 AJAX 来满足他的要求,但结合 php 重定向回到与其他答案相同的页面。这样,如果用户有 javascript,表单是响应式的,否则它仍然可以正常工作。
      【解决方案3】:

      您可以将其回发给自己,然后将页面重定向到 post.php?q=value 如果输入字段下方有值 else echo 。

      <?php
      $qisempty = false;
      if(!empty($_POST['q']))
      {
          header("Location:http://../post.php?q=".$_POST['q']);
      }
      else
          $qisempty = true;
      ?>
          <input name="q" type="text">
          <?php if($qisempty) echo "Please enter your information";?>
      

      【讨论】:

      • 我需要在我的 html 文件中创建一个文本字段来发布这个吗?
      【解决方案4】:

      你可以使用 AJAX 来做这件事。当您不想重新加载页面以在 HTML 页面的特定位置或 Div 中执行任务时,AJAX 非常适合此类问题。

      对于您的问题,您需要创建一个包含表单的 HTML 文件,然后通过 AJAX 提交。并通过相同的 AJAX 获得您的响应。

      <script type="text/javascript">
      function submit_ajax(val1,val2,param1,param2,subfile){
      var http = new XMLHttpRequest();
      var url = subfile;
      var params = val1+"="+param1+"&"+val2+"="+param2;
      http.open("POST", url, true);
      
      //Send the proper header information along with the request
      http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http.setRequestHeader("Content-length", params.length);
      http.setRequestHeader("Connection", "close");
      
      http.onreadystatechange = function() {//Call a function when the state changes.
          if(http.readyState == 4 && http.status == 200) {
              alert(http.responseText);
              //Remove the alert and use whatever you want to do with http.responsetext like
          // document.getElementById("YourDiv").innerHTML=document.getElementById("YourDiv").innerHTML +http.responsetext
          }
      }
      http.send(params);
      }
      </script>
      </head>
      <body>
      
      
      <form>
      <input type="text" id="user" value="user" name="user" />
      <input type="password" id="password" value="pass" name="pass" />
      <button onclick="submit_ajax(user.name,password.name,user.value,password.value, "submit_file.php");">Submit</button>
      </form>
      
      <div id="YourDiv">
      <!--Something appears here after callback-->
      </div>
      

      这是第一页。现在根据需要在您的 PHP 文件(可能是 submit_file.php)中使用您的脚本,然后通过验证或其他方式在您的 div 中回显您想要的文本.. 示例将是

      <?php
      
      $username=$_POST['user'];
      $password=$_POST['pass'];
      
      if(validateuser($username,$password)){
          if(checkuserpass($username,$password){
              echo "You were successfully logged in!";
          }
      echo "Sorry Username/Password Mismatch";
      }
      else {
      echo "There was an error in your input!Please Correct";
      }
      ?>
      

      希望你得到你想要的......

      【讨论】:

        【解决方案5】:

        最简单的方法是将错误消息分配给一个名为(例如 $errorMsg)的变量 使用回显或打印功能在页面上打印。

        <?php
        
            if ($_POST['q'])  == NULL ){
               $errorMsg =' Please enter your information';
            }
        
            ?>
        

        将此代码放在您希望出现错误的位置

        <? print $errorMsg; ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-20
          • 2021-09-23
          • 2019-09-07
          • 1970-01-01
          • 2015-03-04
          • 2010-12-26
          • 2011-06-01
          • 2011-05-30
          相关资源
          最近更新 更多