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;'
)
));
Related
I'm new to php and html and therefore need to get the basics of both php and html.
I'm making a mini calculator and so far I have only made the layout. I need help in knowing that how do you get the value from a button pressed(from HTML form) in your php code section (on the same page), process it in php and then return it back to be shown on the text box of the HTML form.
<?php
/**
**need help here
*/
?>
<html lang="en">
<head>
<title>Title</title>
</head>
<body>
<form name="calculator" method="post" action="index.html">
<input type="text" name="disp" value="" /> <br> <br>
<input type="button" name="one" value="1">
<input type="button" name="two" value="2">
<input type="button" name="three" value="3">
<input type="button" name="bksp" value="DEL"> <br>
<input type="button" name="four" value="4">
<input type="button" name="five" value="5">
<input type="button" name="six" value="6">
<input type="button" name="reset" value="AC"> <br>
<input type="button" name="seven" value="7">
<input type="button" name="eight" value="8">
<input type="button" name="nine" value="9">
<input type="button" name="equal" value="="> <br>
<input type="button" name="zero" value="0"> <br>
<input type="button" name="plus" value="+">
<input type="button" name="minus" value="-">
<input type="button" name="mult" value="*">
<input type="button" name="divide" value="/">
</form>
</body>
</html>
HTML has a more modern element for the button: <button>. Unlike the input element, it is a container and takes the following form:
<button type="submit" name="something" value="whatever">Send Stuff</button>
It has a number of benefits:
button puts the text between the opening and closing tags, and allows the text to be more elaborate.
button is easier to style in CSS since it is a separate element to input.
As you see from the above example, you have the optional value attribute. In the input element, the the value attribute doubles up as both the text and the value. In button, the value is independent.
Be aware that certain Legacy Browsers™ get the actual value wrong, so you will need to check browser support. If you are worried about that, here is an alternative which relies on a special trick in PHP:
<button type="submit" name="something[whatever]">Send Stuff</button>
In PHP, the data will be sent as an array, and you would read the value as a key or the array:
$value=key($_GET['something']);
BTW, I assume that you’re using Ajax to do the rest. In this case it is often the $_GET array being used.
First, you can't process a PHP code inside html. So save for file as .php and leave your action attribute blank since you want to process the POST request within the same page. And a type of button for your input element will not going to submit your form. Changed it to submit
<form name="calculator" method="post" action="">
<input type="submit" name="one" value="someString">
Second, PHP needs to know or should have a reference of what specific input field you're trying to get value from. So the name attribute will be the reference. But you MUST know the exact name you're trying to pull or it will throw you an undefined index error, PHP will not know that for you.
$string = ""; //declare your variable as global to be used later
if(!empty($_POST)){
$string = $_POST['one']; //assuming you know that you are getting the value of input field `name=one`
}
From there, you can now use the variable string to display in your HTML input/textarea.
<textarea><?php echo $string; ?></textarea>
<input type="text" value="<?php echo $string;?>">
Note:
This is for PHP only, this kind of scenario is mostly done via javascript, but it depends on your preference or purpose though
I want a list of buttons in a form and on postback I'd like to interrogate the list to see what status the buttons are at (by background colour if you are interested.)
The following codes says that Buttonx is undefined
However using the text boxes it works fine.
In javascript I can get at the array of buttons - at least in my real program.
If this is not possible, and an explanation would be useful, does anyone have a workaround at how I can get at the buttons in my postback. (In the real code the buttons are dynamically created depending on an sql query list)
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
var_dump($_POST['Buttonx']);
}
?>
<form name="RegisterStudents" action="" method="post">
<button type="submit" name="myButton">Submit</button>
<!--
<input type="text" name="Buttonx[]" id="B0" value="0" />
<input type="text" name="Buttonx[]" id="B1" value="1" />
<input type="text" name="Buttonx[]" id="B2" value="2" />
<input type="text" name="Buttonx[]" id="B3" value="3" />
-->
<button type="button" name="Buttonx[]" id="B0" >0</button>
<button type="button" name="Buttonx[]" id="B1" >1</button>
<button type="button" name="Buttonx[]" id="B2" >2</button>
<button type="button" name="Buttonx[]" id="B3" >3</button>
</form>
Thanks
Gordon
Using forms you cannot pass the form elements with type="button" to server their values are not passed to server instead you have to use any other type to send the information to server
on client side you can access their values this is the default nature of html
You cannot pass the type="button" to the server, instead use checkboxes?
You have the id="B0" or so but instead it should be value="B0" and not "id"
The <button> is not really mature in browsers yet. Look at this link: http://www.w3schools.com/tags/tag%5Fbutton.asp
If you use the <button> element in an HTML form, different browsers may
submit different values. Use <input> to create buttons in an HTML form.
I have never really used radio buttons before. So what I am trying to achieve is, using two radio buttons 'Public' and 'Private'. This is for the user's profile. My HTML code for the buttons is;
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn btn-primary" id="privacy" name="privacy" value="0">Private</button>
<button type="button" class="btn btn-primary" id="privacy" name="privacy" value="1">Public</button>
</div>
As for the PHP, I'm not sure how to get the value and store it. Any help is appreciated!
Use a form.
<form method="get" action="yourscript.php">
<input type="radio" id="private" name="privacy" value="0">
<label for="private">Private</label>
<input type="radio" id="public" name="privacy" value="1">
<label for="public">Public</label>
<input type="submit" value="Save">
</form>
Change your buttons to input elements, which, together with the form, provide a HTML native way to send data to a script without the need for Javascript (although you can use Javascript to enhance the usability later on).
Using the label tag, you can assign a text to a radiobutton. This allows this text to be clicked as well, and it also provides better support for screen readers, because they can actually see which text belongs to the radiobutton.
An id needs to be unique, so I changed it to private and public. The ids are also used to link the label to.
The name determines by which name the value is sent to your script.
In PHP, you can use the superglobal $_GET. $_GET['privacy'] will contain '0' or '1' depending on the choice made. If the method of the form was post, you could use the superglobal $_POST instead, or you can use $_REQUEST, which contains values from either, so in that case your script doesn't care whether the values were send in the url (get) or in the chunk of invisible post data that is sent along with the request.
change type to radio and donot use the same id for morethan one element id is supposed to be unique
Also when you post the form type=button values will not be passed with the form
<div class="btn-group" data-toggle="buttons-radio">
<input type="radio" class="btn btn-primary" name="privacy" value="0"/>Private
<input type="radio" class="btn btn-primary" name="privacy" value="1"/>Public
</div>
assuming its posted on $_POST['privacy'] should contain either a 1 or a 0
also dont use an ID twice - they are supposed to be unique
You don't have form and submit button.
HTML page:
<div class="btn-group" data-toggle="buttons-radio ">
<form name="privacyForm" action="action.php" method="post">
<button type="radio" class="btn btn-primary" name="privacy" value="0">Private</button>
<button type="radio" class="btn btn-primary" name="privacy" value="1">Public</button>
<input type="submit" value="Submit">
</form>
</div>
action.php
<?php
echo $_POST['privacy']; // should be 0 or 1
?>
i have assumes that your form is something as follows.
<form name="sample" action="form_submit.php" method="POST">
<div class="btn-group" data-toggle="buttons-radio">
<input type="button" class="btn btn-primary" id="private" name="privacy" value="0">Private</input>
<label for="private">Private</label>
<input type="button" class="btn btn-primary" id="public" name="privacy" value="1">Public</input>
<label for="public">Public</label>
</div>
<input type="submit" name ="submit" value ="Submit"/>
</form>
you can use the following code inside the action (form_submit.php) to get the selected radio button value.
form_submit.php
<?php
$selected_radio = $_POST['privacy'];
print $selected_radio;
?>
make sure that you are not duplicating the IDs because they should be unique.
I am writing a search form in PHP.
I want the user to be able to add search fields using following submit button:
<input type="submit" name="fields" value="<?php echo $fields+1 ?>" />
Now the button shows the value of $fields + 1.
Actually what I want is the button to show something else (like add new field).
Just adding text between the <input>...</input> tags does not help.
The text just appears right of the button.
How do I change the text on the button and still pass the value of $fields + 1 to GET/POST?
You can use an hidden input to store the value count, it will be available after GET/POST:
<input type="submit" name="fields" value="add new field" />
<input type="hidden" name="fieldsCount" value="<?php echo $fields+1 ?>" />
You can use the button element:
<button type="submit" name="seven" value="7">Push Me</button>
Refs: http://www.w3schools.com/tags/att_button_type.asp
You Can use the hidden field to store or post the $fields+1 value:
<input type="hidden" name="fields" value="<?php echo $fields+1 ?>" />
<input type="submit" name="submitter" value="Send" />
i've a form POST with multiple submit buttons. i understand to get this to work i must have them with different name.
however, i wanna keep the name to be the same because i wanna handle the POST using a single script.
im not sure if there is other way but i know javascript can be used. however, how do i get the value of the hidden value associated to the button since now they have only a single ??
my example is as follows:
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="1" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
<input type="hidden" name="removeid" value="2" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
<input type="hidden" name="removeid" value="2" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
</form>
Your hidden values are not associated with the buttons at all. Furthermore, you cannot use the same value for the ID attribute on multiple elements.
What I usually do in this situation is check the POST vars. Name them something like remove_1, remove_2, etc. Then you can search through your POST vars, find all of them beginning with remove_ (or whatever format you choose... don't use it for other things) and then you can parse out the ID of what you are trying to remove.
You could always just use 3 different forms, all with the same action. No JavaScript needed.
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="1" />
<input type="submit" value="Remove" inputbutton/>
</form>
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="2" />
<input type="submit" value="Remove" inputbutton/>
</form>
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="2" />
<input type="submit" value="Remove" inputbutton/>
</form>
It's possible using two different methods:
If you absolutely have to show 3 different buttons, use a separate <form> wrapper for each one. Put each "removeid" element in a different form.
Otherwise, just have a single button, and when submitted, use JavaScript to set the value of a single hidden input element before posting the form. You can find sample code for this easily with a Google query for "javascript+form+post".
You can have one form with more than one submit button sharing the same name, your initial assumption was wrong.
The following code is perfectly valid, and the value of the clicked submit button will be passed along with its name:
<form action="TestZone.html" method="GET">
<input type="submit" name="MySubmit" value="First" /><input type="submit" name="MySubmit" value="Second" /><input type="submit" name="MySubmit" value="Third" />
</form>
You can't have multiple elements with same ID, but same name for form elements is common and valid.
Hi i have resolved my questions by following Brad solution. to get the POST var, i did this:
//Check if Remove btn is clicked
$isClickRemove = false;
$cid = "";
foreach($_POST as $k=>$v){
$pos = strpos($k,"btnremovecart_");
if($pos !== false){
$pos2 = strpos($k,"_"); //2nd pos to get cartID
$cid = substr($k,$pos2+1);
$isClickRemove = true;
break;
}
}
my html looks like this:
<input type="submit" id="btnremovecart_11" name="btnremovecart_11" value="Remove" />
hope this helps =)
You can't because there is no way of distingushing the different fields.