【问题标题】:HTML and PHP send/return data [closed]HTML 和 PHP 发送/返回数据 [关闭]
【发布时间】:2017-02-04 23:06:55
【问题描述】:

我有一个 HTML 文件,格式为:

<form id="su" class="signup" method="post" action="form.php" name="Sign Up" style='display: block'>

我知道表单输入值会发送到form.php文件,网页会重定向到form.php

form.php 处理来自 HTML 文件(如 $_POST['password'])的数据并获得返回值后,如何将返回值发送到 HTML 页面并将网页重定向到 HTML。

我不确定我的想法是否正确。因为看到有人说Ajax技术,我是web开发新手,请多多指教。

我所做的是关于注册表单,数据发送到php在数据库上进行注册,如果注册成功,它应该向客户发送通知。

【问题讨论】:

标签: php html forms


【解决方案1】:

Ajax 是一个不错的选择

这是一个例子

<form> 
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>

这是一个直接从 HTML 中获取数据的简单表单 并像这样在脚本中处理它

  <script>
    function showHint(str) {
      if (str.length == 0) { 
         //make the txtHint value as Blank
          document.getElementById("txtHint").innerHTML = "";
          return;
       } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
          //status 200 mean it is OK
           if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
        }
    };
    //bass the value of textbox to php
    xmlhttp.open("GET", "gethint.php?q=" + str, true);
    xmlhttp.send();
 }
}
 </script>  

而且 PHP 文件似乎是这样的

      <?php
  // Array with names
  $a[] = "Anna";
  $a[] = "Brittany";
  $a[] = "Cinderella";
  $a[] = "Diana";
  $a[] = "Eva";

  // get the q parameter from URL
  $q = $_REQUEST["q"];

  $hint = "";

 // lookup all hints from array if $q is different from "" 
 if ($q !== "") {
   $q = strtolower($q);
   $len=strlen($q);
    foreach($a as $name) {
        if (stristr($q, substr($name, 0, $len))) {
            if ($hint === "") {
               $hint = $name;
             } else {
                $hint .= ", $name";
             }
         }
    }
 }

// Output "no suggestion" if no hint was found or output correct values 
 echo $hint === "" ? "no suggestion" : $hint;
?>

我建议你看看这个很棒的 tut:Ajax And PHP

【讨论】:

    【解决方案2】:

    你可以使用

    echo $_COOKIES['']
    

    将数据发送回客户端。

    http://php.net/manual/en/features.cookies.php

    【讨论】:

    • 请注意,直接回显用户数据是不安全的。始终转义数据,例如使用内置函数 htmlspecialchars
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    相关资源
    最近更新 更多