Javascript 如何将按钮值发布到 PHP?

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

How do I post button value to PHP?

phpjavascripthtmlforms

提问by homlyn

I want use A-Z buttons on a html page like shown below (only sample and few words)

我想在如下所示的 html 页面上使用 AZ 按钮(只有示例和几个字)

<INPUT TYPE="BUTTON" VALUE=" A " ONCLICK="A">
<INPUT TYPE="BUTTON" VALUE=" B " ONCLICK="B">
<INPUT TYPE="BUTTON" VALUE=" C " ONCLICK="C">
<INPUT TYPE="BUTTON" VALUE=" D " ONCLICK="D">
<INPUT TYPE="BUTTON" VALUE=" E " ONCLICK="E">
<INPUT TYPE="BUTTON" VALUE=" F " ONCLICK="F">
<INPUT TYPE="BUTTON" VALUE=" G " ONCLICK="G">
<INPUT TYPE="BUTTON" VALUE=" H " ONCLICK="H">
<INPUT TYPE="BUTTON" VALUE=" I " ONCLICK="I">
<INPUT TYPE="BUTTON" VALUE=" J " ONCLICK="J">

when I click on each button I want to post respective values on button to a PHP variable.

当我单击每个按钮时,我想将按钮上的相应值发布到 PHP 变量。

How can I do this?

我怎样才能做到这一点?

回答by BalusC

Change the typeto submitand give it a name(and remove the useless onclickand flat out the 90's style uppercased tags/attributes).

更改typetosubmit并给它一个name(并删除无用的onclick和扁平化的 90 年代风格的大写标签/属性)。

<input type="submit" name="foo" value="A" />
<input type="submit" name="foo" value="B" />
...

The value will be available by $_POST['foo'](if the parent <form>has a method="post").

该值将由$_POST['foo'](如果父级<form>具有method="post")可用。

回答by Josh

Give them all a name that is the same

给他们起一个相同的名字

For example

例如

<input type="button" value="a" name="btn" onclick="a" />
<input type="button" value="b" name="btn" onclick="b" />

Then in your php use:

然后在你的 php 中使用:

$val = $_POST['btn']

Edit, as BalusC said; If you're not going to use onclick for doing any javascript (for example, sending the form) then get rid of it and use type="submit"

编辑,正如 BalusC 所说;如果您不打算使用 onclick 来执行任何 javascript(例如,发送表单),则摆脱它并使用type="submit"

回答by Jeremy Dentel

As Josh has stated above, you want to give each one the same name (letter, button, etc.) and all of them work. Then you want to surround all of these with a form tag:

正如 Josh 上面所说的,你想给每一个都起相同的名字(字母、按钮等),并且它们都可以工作。然后你想用一个表单标签包围所有这些:

<form name="myLetters" action="yourScript.php" method="POST">
<!-- Enter your values here with the following syntax: -->
<input type="radio" name="letter" value="A" /> A
<!-- Then add a submit value & close your form -->
<input type="submit" name="submit" value="Choose Letter!" />
</form>

Then, in the PHP script "yourScript.php" as defined by the action attribute, you can use:

然后,在由 action 属性定义的 PHP 脚本“yourScript.php”中,您可以使用:

$_POST['letter']

To get the value chosen.

获取选择的值。

回答by David

Keep in mind that what you're getting in a POST on the server-side is a key-value pair. You have values, but where is your key? In this case, you'll want to set the nameattribute of the buttons so that there's a key by which to access the value.

请记住,您在服务器端的 POST 中得到的是键值对。你有价值观,但你的钥匙在哪里?在这种情况下,您需要设置name按钮的属性,以便有一个键可以访问该值。

Additionally, in keeping with conventions, you'll want to change the typeof these inputs (buttons) to submitso that they post their values to the form properly.

此外,按照惯例,您需要将type这些inputs(按钮)的更改为,submit以便它们正确地将其值发布到表单中。

Also, what is your onclickdoing?

还有,你onclick在做什么?

回答by hihi

    $restore = $this->createElement('submit', 'restore', array(
        'label' => 'FILE_RESTORE',
        'class' => 'restore btn btn-small btn-primary',
        'attribs' => array(
            'onClick' => 'restoreCheck();return false;'
        )
    ));