【问题标题】:Php $_POST method to get textarea valuephp $_POST 方法获取 textarea 值
【发布时间】:2011-09-28 11:32:40
【问题描述】:

我正在使用 php 使用 post 方法获取 textarea 值,但得到一个奇怪的结果,让我向您展示我的代码

<form method="post" action="index.php">
    <textarea id="contact_list" name="contact_list"></textarea>
    <input type="submit" name="submit" value="Send" id="submit"/>
</form>

我在 textarea 中输入了一些名称和他们的电子邮件地址,每次我回显值或 textarea 它都会跳过电子邮件地址,只显示名称让我显示我在 textarea 中输入值的方式

"name1" <name@email.com>, "name2" <name2@email.com> 

一旦我使用 php 回显,它只会回显名称并跳过电子邮件地址。

【问题讨论】:

  • 它没有跳过电子邮件,您的网络浏览器将其解释为标签并且没有显示。看html源码。
  • 请试试这个:

    stackoverflow.com/questions/8719186/… 使用带有文本区域的 $_POST

标签: php post


【解决方案1】:

总是(总是,总是,我是not kidding)使用htmlspecialchars():

echo htmlspecialchars($_POST['contact_list']);

【讨论】:

    【解决方案2】:

    确保您对 HTML 字符进行转义

    例如

    // Always check an input variable is set before you use it
    if (isset($_POST['contact_list'])) {
        // Escape any html characters
        echo htmlentities($_POST['contact_list']);
    }
    

    这是因为尖括号和浏览器认为它们是标签。

    见:http://php.net/manual/en/function.htmlentities.php

    【讨论】:

    • 我相信 htmlentities 会更好,因为没有 html_entity_encode 方法
    • 是的,是的,在我查找真正的功能之前快速回答:P
    【解决方案3】:

    使用htmlspecialchars():

    echo htmlspecialchars($_POST['contact_list']);
    

    您甚至可以通过使用strip_tags() 去除所有标签并使用trim() 删除所有空格来改进您的表单处理:

    function processText($text) {
        $text = strip_tags($text);
        $text = trim($text);
        $text = htmlspecialchars($text);
        return $text;
    }
    
    echo processText($_POST['contact_list']);
    

    【讨论】:

      【解决方案4】:

      尝试使用不同的 id 和 name 参数,目前你这里有相同的。请访问下面的链接以获取相同的信息,这可能会对您有所帮助:

      Issue using $_POST with a textarea

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
      • @ManishKr.Shukla 虽然我同意您的审查决定,但值得一提的是,此特定链接指向 SO 网站上的另一个问题。所以我投票“看起来不错”。
      • @ManishKr.Shukla 和 user1032613 感谢您提供信息。我会处理的:)
      【解决方案5】:

      删除一些 textarea 类,例如

      <textarea name="Address" rows="3" class="input-text full-width" placeholder="Your Address" ></textarea>
      

      到

      <textarea name="Address" rows="3" class="full-width" placeholder="Your Address" ></textarea>
      

      这取决于您的模板(购买的模板)。 开发人员包含了一些 JavaScript 来从 UI 上的正确对象中获取值, 但是像input-text 这样的类只能找到$('input[type=text]'),这就是原因。

      【讨论】:

      • 请不要对不同的问题提交完全相同的答案。如果是这种情况,请将问题标记为“重复”。
      【解决方案6】:
      //My Form
      <form id="someform">
              <div class="input-group">
                  <textarea placeholder="Post your Comment Here ..." name="post" class="form-control custom-control" rows="3" style="resize:none"></textarea> 
                  <span class="input-group-addon">                                            
                      <button type="submit" name="post_comment" class="btn btn-primary">
                          Post
                      </button>
                  </span>
              </div>
          </form>
      
      //your text area get value to URL
      <?php
              if(isset($_POST['post_comment']))
              {
                  echo htmlspecialchars($_POST['post']);
              }
      
          ?>
      
      //print the value using get
      echo $_GET['post'];
      
      //url must be like this
      http://localhost/blog/home.php?post=asdasdsad&post_comment=
      
      //post value has asdasdsad so it will print to your page
      

      【讨论】:

        【解决方案7】:

        非常简单。只需在 textarea 标签之间写下你的 php 值代码。

        <textarea id="contact_list"> <?php echo isset($_POST['contact_list']) ? $_POST['contact_list'] : '' ; ?> </textarea>
        

        【讨论】:

          【解决方案8】:

          试试这个:

          <?php /* the php */ ?>
          <?php 
              if ($_POST['submit']) {
                  // must work
                  echo $_POST['contact_list'];
              };
          ?>
          
          
           <?php /* and the html */ ?>
          
          <!doctype html>
          <html lang="en">
              <head>
                  <meta charset="UTF-8">
                  <title>teszt</title>
              </head>
              <body>
                  <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
                      <textarea id="contact_list" name="contact_list"></textarea>
                      <input type="submit" name="submit" value="Send" id="submit"/>
                  </form>
              </body>
          </html>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-27
            • 2012-10-13
            相关资源
            最近更新 更多