PHP $_POST 方法获取 textarea 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7582577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:59:30  来源:igfitidea点击:

Php $_POST method to get textarea value

phppost

提问by Frank

I am using php to get textarea value using post method but getting a weird result with that let me show you my code

我正在使用 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>

I am entering some names and their email address in the textarea, and everytime i echo the value or textarea it skips the email address and only showing the name let me show the way i am entering value in textarea

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

"name1" <[email protected]>, "name2" <[email protected]> 

and once I will echo using php it will only echo the name and will skip the email address.

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

回答by Tomalak

Always (always, always, I'm not kidding) use htmlspecialchars():

总是(总是,总是,我不是在开玩笑)使用htmlspecialchars()

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

回答by Petah

Make sure your escaping the HTML characters

确保您转义 HTML 字符

E.g.

例如

// 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']);
}

This would occur because of the angle brackets and the browser thinking they are tags.

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

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

请参阅:http: //php.net/manual/en/function.htmlentities.php

回答by Gottlieb Notschnabel

Use htmlspecialchars():

使用htmlspecialchars()

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

You can even improve your form processing by stripping all tags with strip_tags()and remove all white spaces with trim():

您甚至可以通过使用以下命令剥离所有标签strip_tags()并删除所有空格来改进表单处理trim()

function processText($text) {
    $text = strip_tags($text);
    $text = trim($text);
    $text = htmlspecialchars($text);
    return $text;
}

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

回答by Sanchit Gupta

Try to use different id and name parameters, currently you have same here. Please visit the link below for the same, this might be help you :

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

Issue using $_POST with a textarea

将 $_POST 与 textarea 一起使用的问题

回答by jundell agbo

//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

回答by Vusal

it is very simply. Just write your php value code between textarea tag.

这很简单。只需在 textarea 标记之间编写您的 php 值代码。

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

回答by ZenithS

Remove some of your textarea class like

删除一些你的 textarea 类,如

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

To

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

It's dependent on your template (Purchased Template). The developer has included some JavaScript to get the value from correct object on UI, but class like input-textjust finds only $('input[type=text]'), that's why.

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

回答by Fehér Norbert

Try this:

尝试这个:

<?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>