开发者

How do I post button value to PHP?

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

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

How can I do this?


Change the type to submit and give it a name (and remove the useless onclick and flat out the 90's style uppercased tags/attributes).

<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").


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:

$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"


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:

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

$_POST['letter']

To get the value chosen.


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 name attribute of the buttons so that there's a key by which to access the value.

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

Also, what is your onclick doing?


Using input tag is going to leave the mark on the page. Trying button tag will show the button and also can send the value as well.

<button type="submit" value="the value you want" >Update</button>


use as: button type="submit" value="A" name="btn" input type="submit" value="B" name="btn" when you will post data use: $_REQUEST['btn'];


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜