HTML 表单提交到 PHP 脚本

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

HTML form submit to PHP script

phphtmlformspost

提问by Joe

I am making an HTML form. I want the results to appear in the PHP script.

我正在制作一个 HTML 表单。我希望结果出现在 PHP 脚本中。

<form action="chk_kw.php" method="post"> <br />
    <select> name="website_string" 
        <option value="" selected="selected"></option>
    <option VALUE="abc"> ABC</option>
    <option VALUE="def"> def</option>
        <option VALUE="hij"> hij/option>   
    </select>
    <input type="submit" name="website_string"  >
</form>

The problem is I cannot get the value passed to the PHP. if I use value:

问题是我无法获得传递给 PHP 的值。如果我使用价值:

<INPUT TYPE="submit" name="website_string" value="selected" >

It always passes the text in the quotes. In this case "selected". How do I pass one of the strings from the option?

它总是通过引号中的文本。在这种情况下"selected"。如何传递选项中的字符串之一?

回答by jondavidjohn

Try this:

尝试这个:

<form method="post" action="check.php">
    <select name="website_string">
        <option value="" selected="selected"></option>
        <option VALUE="abc"> ABC</option>
        <option VALUE="def"> def</option>
        <option VALUE="hij"> hij</option>
    </select>
    <input TYPE="submit" name="submit" />
</form>

Both your select control and your submit button had the same nameattribute, so the last one used was the submit button when you clicked it. All other syntax errors aside.

您的选择控件和提交按钮具有相同的name属性,因此最后一个使用的是您单击它时的提交按钮。抛开所有其他语法错误。

check.php

检查.php

<?php
    echo $_POST['website_string'];
?>

Obligatory disclaimer about using raw $_POSTdata. Sanitize anything you'll actually be using in application logic.

关于使用原始$_POST数据的强制性免责声明。清理您将在应用程序逻辑中实际使用的任何内容

回答by Babiker

<form method="POST" action="chk_kw.php">
    <select name="website_string"> 
        <option selected="selected"></option>
        <option value="abc">abc</option>
        <option value="def">def</option>
        <option value="hij">hij</option>   
    </select>
    <input type="submit">
</form>



  • As your form gets more complex, you can a quick check at top of your php script using print_r($_POST);, it'll show what's being submitted an the respective element name.
  • To get the submitted value of the element in question do:

    $website_string = $_POST['website_string'];

  • 随着您的表单变得更加复杂,您可以使用 快速检查您的 php 脚本顶部print_r($_POST);,它会显示正在提交的内容以及相应的元素名称。
  • 要获取相关元素的提交值,请执行以下操作:

    $website_string = $_POST['website_string'];

回答by Kyle

It appears that in PHP you are obtaining the value of the submit button, not the select input. If you are using GET you will want to use $_GET['website_string']or POST would be $_POST['website_string'].

看来在 PHP 中,您获取的是提交按钮的值,而不是选择输入的值。如果您正在使用 GET 您将想要使用$_GET['website_string']或 POST 将是$_POST['website_string'].

You will probably want the following HTML:

您可能需要以下 HTML:

<select name="website_string">
  <option value="" selected="selected"></option>
  <option value="abc">ABC</option>
  <option value="def">def</option>
  <option value="hij">hij</option>   
</select>
<input type="submit" />

With some PHP that looks like this:

使用一些看起来像这样的 PHP:

<?php

$website_string = $_POST['website_string']; // or $_GET['website_string'];

?>

回答by Derek H

Assuming you've fixed the syntax errors (you've closed the select box before the name attribute), you're using the same name for the select box as the submit button. Give the select box a different name.

假设您已经修复了语法错误(您已经在 name 属性之前关闭了选择框),那么您将使用与提交按钮相同的选择框名称。给选择框一个不同的名字。

回答by TonyPall

For your actual form, if you were to just post the results to your same page, it should probably work out all right. Try something like:

对于您的实际表单,如果您只是将结果发布到您的同一页面,它应该可以正常工作。尝试类似:

<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="POST>

回答by Mark Giblin

Here is what I find works

这是我发现的作品

  1. Set a form name
  2. Use a default select option, for example...

    <option value="-1" selected>Please Select</option>

  1. 设置一个 form name
  2. 使用一个default select option,例如...

    <option value="-1" selected>Please Select</option>

So that if the form is submitted, use of JavaScript to halt the submission process can be implemented and verified at the server too.

这样,如果表单被提交,也可以在服务器上实现和验证使用 JavaScript 来停止提交过程。

  1. Try to use HTML5 attributes now they are supported.
  1. 尝试使用现在支持的 HTML5 属性。

This input

这个输入

<input type="submit">

should be

应该

<input name="Submit" type="submit" value="Submit">

whenever I use a form that fails, it is a failure due to the difference in calling the button name submitand name as Submit.

每当我使用失败的表单时,由于调用按钮名称submit和名称为Submit.

You should also set your enctype attribute for your form as forms fail on my web host if it's not set.

您还应该为您的表单设置 enctype 属性,因为如果未设置,我的 Web 主机上的表单将失败。