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>
First, you can't process a PHP code inside html. So save for file as
.php
and leave youraction
attribute blank since you want to process the POST request within the same page. And atype
ofbutton
for your input element will not going to submit your form. Changed it tosubmit
<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 anundefined 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
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
.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.