php 不通过 _POST 发送数据的简单表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19887112/
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-25 20:10:53  来源:igfitidea点击:

Simple form not sending data via _POST

phphtmlformstwitter-bootstrappost

提问by jacurtis

Wow, so this is embarrassing. This is just supposed to be another boring web form. I have done plenty before, but for some reason this specific one doesn't want to cooperate. Am I somehow missing something? I have been trying to figure this out for hours. I swear I have tried troubleshooting everything, without any luck.

哇,这太尴尬了。这应该是另一种无聊的网络表单。我以前做过很多,但出于某种原因,这个特定的人不想合作。我是不是错过了什么?几个小时以来,我一直试图弄清楚这一点。我发誓我已经尝试过对所有问题进行故障排除,但没有任何运气。

Basically, I am attempting to send via $_POST to a PHP file. Eventually I will use AJAX, but right now I can't even get the stupid thing to POST. So when you click submit, it goes to the correct page. But i get an error "Undefined Index (then points to the line that I call my $_POST variable). I have commented out everything on the PHP side (signup1.php) so that now all it does is var_dump($_POST);so I can see the data and it isnt getting anything from _POST, the stupid thing is empty. I really think the problem is on the HTML side. I will post my code exactly. It has a ton of extra divs for styling and layout purposes. I am going to keep all of that in there just in case that is the cause of the error, maybe one of you will know. Also, you may notice I am using bootstrap. Maybe that is something? I have used bootstrap for forms before and its worked, but who knows.

基本上,我试图通过 $_POST 发送到 PHP 文件。最终我将使用 AJAX,但现在我什至无法将愚蠢的事情发送到 POST。因此,当您单击提交时,它会转到正确的页面。但是我收到一个错误“未定义的索引(然后指向我称之为 $_POST 变量的行)。我已经注释掉了 PHP 端的所有内容(signup1.php),所以现在它所做的就是var_dump($_POST);所以我可以看到数据并且它没有从 _POST 得到任何东西,愚蠢的事情是空的。我真的认为问题出在 HTML 方面。我会准确地发布我的代码。它有大量用于样式和布局目的的额外 div。我将把所有这些都保留在那里,以防万一这是错误的原因,也许你们中的一个人会知道。此外,您可能会注意到我正在使用引导程序。也许那是什么?我以前使用过引导程序作为表单并且它有效,但谁知道呢。

<form action="actions/signup1.php" method="post">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label for="name">Name:</label>
                    <input type="text" id="name" class="form-control" placeholder="John Smith">
                </div>
            </div> <!-- end of name input column -->
            <div class="col-md-6">
                <div class="form-group">
                    <label for="phone">Phone Number:</label>
                    <input type="text" id="phone" class="form-control" placeholder="801-321-9876">
                </div> 
            </div> <!-- end of phone number input column -->
        </div> <!-- end of first .row of inputs -->

        <div class="row">
            <div class="col-md-6 column-sm-12">
                <div class="form-group">
                    <label for="email">Email Address:</label>
                    <input type="text" id="email" class="form-control" placeholder="[email protected]">
                </div>
            </div> <!-- end of email address input column -->

            <div class="col-md-3 col-sm-6">
                <div class="form-group">
                    <label for="city">City:</label>
                    <select id="city" class="form-control">
                        <option>Las Vegas</option>
                        <option>Salt Lake City</option>
                        <option>I'll Choose a Default Later...</option>
                    </select>
                </div>
            </div> <!-- end of city dropdown -->

            <div class="col-md-3 col-sm-6">
                <div class="form-group">
                    <label for="type-of-phone">Type of Phone:</label>
                    <select id="type-of-phone" class="form-control">
                        <option>iPhone</option>
                        <option>Android</option>
                        <option>Windows Phone</option>
                        <option>Blackberry</option>
                        <option>Other Cellphone</option>
                        <option>Landline</option>
                    </select>
                </div>
            </div> <!-- end of Smartphone Question Dropdown -->
        </div> <!-- end of second .row of inputs -->
        <div class="row">
            <div class="col-md-12"><input type="submit" value="Submit"></div>
        </div>
    </form>

If anyone has any idea please let me know. I am pulling my hair out. I figured I would be up all night with problems having it querying the database, or messing with the AJAX to make it polished, but instead I can't even get past the form submitting the RAW data! Very frustrating. I hope someone out there can see the issue.

如果有人有任何想法,请告诉我。我正在拔头发。我想我会因为查询数据库的问题而彻夜难眠,或者弄乱 AJAX 以使其完善,但我什至无法通过提交 RAW 数据的表单!非常令人沮丧。我希望有人能看到这个问题。

For the record this is my exactmessage after the form redirects to the signup1.php (yes it goes to the right page). On the PHP all i do is a Var_dump of the whole $_POST variable.

作为记录,这是我在表单重定向到 signup1.php 后的确切消息(是的,它转到了正确的页面)。在 PHP 上,我所做的只是整个 $_POST 变量的 Var_dump。

var_dump($_POST);

And it shows me this:

它向我展示了这一点:

array(0) { } 

回答by Code Monkey

You should be adding the name attribute to the form fields.

您应该将 name 属性添加到表单字段中。

For example:

例如:

<input name="phone" type="text" id="phone" class="form-control" placeholder="801-321-9876">

回答by Elvis Chidera

Try adding the name attribute to each input tag and the select tag. That should do.

尝试将 name 属性添加到每个输入标记和选择标记。那应该做。

回答by Pank

Use redacted script:

使用编辑脚本:

<form action="actions/signup1.php" method="post">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label for="name">Name:</label>
                    <input type="text" id="name" name="name" class="form-control" placeholder="John Smith">
                </div>
            </div> <!-- end of name input column -->
            <div class="col-md-6">
                <div class="form-group">
                    <label for="phone">Phone Number:</label>
                    <input name="phone" type="text" id="phone" class="form-control" placeholder="801-321-9876">
                </div> 
            </div> <!-- end of phone number input column -->
        </div> <!-- end of first .row of inputs -->

        <div class="row">
            <div class="col-md-6 column-sm-12">
                <div class="form-group">
                    <label for="email">Email Address:</label>
                    <input type="text" name="email" id="email" class="form-control" placeholder="[email protected]">
                </div>
            </div> <!-- end of email address input column -->

            <div class="col-md-3 col-sm-6">
                <div class="form-group">
                    <label for="city">City:</label>
                    <select id="city" name="city" class="form-control">
                        <option>Las Vegas</option>
                        <option>Salt Lake City</option>
                        <option>I'll Choose a Default Later...</option>
                    </select>
                </div>
            </div> <!-- end of city dropdown -->

            <div class="col-md-3 col-sm-6">
                <div class="form-group">
                    <label for="type-of-phone">Type of Phone:</label>
                    <select id="type-of-phone" name="type-of-phone" class="form-control">
                        <option>iPhone</option>
                        <option>Android</option>
                        <option>Windows Phone</option>
                        <option>Blackberry</option>
                        <option>Other Cellphone</option>
                        <option>Landline</option>
                    </select>
                </div>
            </div> <!-- end of Smartphone Question Dropdown -->
        </div> <!-- end of second .row of inputs -->
        <div class="row">
            <div class="col-md-12"><input type="submit" value="Submit"></div>
        </div>
    </form>

Output:

输出:

enter image description here

在此处输入图片说明

回答by rocky gupta

Try To give name to every field of your input fields and also provide name to submit button. So your problem will be solved.

尝试为输入字段的每个字段命名,并提供名称以提交按钮。所以你的问题将得到解决。