I'm new to PHP and coding in general.
Currently I'm trying to make a Simple Calculator.
For this I have the two scripts calculator.php and array.php
My Idea is to store all the inputs the user gives in an array so I can calculate them using the eval() function. Because I need a string for this I converted the array using a foreach loop.
But when I try to use the variable outside the foreach loop I get the following errors:
Notice: Array to string conversion on line 17
Array
Parse error: syntax error, unexpected ';', expecting '(' in /Applications/XAMPP/xamppfiles/htdocs/dashboard/array.php(22) : eval()'d code on line 1
What can I do to fix this problem?
array.php
<?php
session_start();
if (!is_array($_SESSION)) {
$_SESSION['persistentValues'] = array();
}
if (isset($_POST['button'])) {
$_SESSION['persistentValues'][] = $_POST['button'];
}
foreach ($_SESSION as $value) {
echo $value;
}
if (isset($_POST["result"])) {
$p = eval($value.";");
echo $p;
}
?>
calculator.php
<?php
include ('./array.php');
?>
<div style="padding-left:40px">
<h2> PHP Calculator</h2>
<form method="post" action="">
Enter value: <input type="text" name="value"> <br> <br>
<div style="padding-left: 105px">
<input type="submit" value="9" name="button">
<input type="submit" value="8" name="button">
<input type="submit" value="7" name="button">
<input type="submit" value="+" name="button">
<br>
<input type="submit" value="6" name="button">
<input type="submit" value="5" name="button">
<input type="submit" value="4" name="button">
<input type="submit" value="-" name="button" style="padding-left: 9px">
<br>
<input type="submit" value="3" name="button">
<input type="submit" value="2" name="button">
<input type="submit" value="1" name="button">
<input type="submit" value="/" name="button" style="padding-left: 9px">
<br>
<input type="submit" value="0" name="button" style="padding-left:33px">
<input type="submit" value="." name="button" style="padding-right:9px">
<input type="submit" value="x" name="button" style="padding-left: 7px">
<br>
</div>
<br>
<input type="submit" value="Calculate" name="result">
</form>
</div>
You're repeatedly confusing the whole $_SESSION variable with $_SESSION['persistentValues']. The latter is where you're holding the buttons as you submit them.
The value of $value is an array because you're looping over $_SESSION to set it, and the value of the session variable is an array. The argument to eval() is a string, and when you convert an array to a string it becomes the string Array, which isn't an expression that can be evaluated.
You can use implode() to concatenate all the elements of an array into a string. So if the session array contains ['9', '3', '+', '8'] it will set $value to '93+8', which you can then pass to eval().
The argument to eval() needs to be a statement. So you should put the echo command in the argument.
<?php
session_start();
if (!is_array($_SESSION['persistentValues'])) {
$_SESSION['persistentValues'] = array();
}
if (isset($_POST['button'])) {
$_SESSION['persistentValues'][] = $_POST['button'];
}
$value = implode('', $_SESSION['persistentValues']);
echo $value;
if (isset($_POST["result"])) {
eval("echo $value;");
}
?>
Related
I don't know why it cannot show the result. I have already put isset() Function on it, but the echo string is not come out.
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="button" name="add" value="Add">
<input type="button" name="clear" value="Clear">
</form>
<?php
if (isset($_POST['add'])) {
if (isset($_POST['colors'])) {
$colorVal = $_POST['colors'];
echo "$colorVal";
}
}
?>
The type of your Add button must be submit also the Clear button has to be reset.
By the way that $POST['colors'] needs to be $_POST['colors'].
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="submit" name="add" value="Add">
<input type="reset" name="clear" value="Clear">
</form>
<?php
if (isset($_POST['add'])) {
if (isset($_POST['colors'])) {
$colorVal = $_POST['colors'];
echo "$colorVal";
}
}
?>
Update: (the reset button works as expected)
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="submit" name="add" value="Add">
<input type="reset" name="clear" value="Clear">
</form>
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="submit" name="add" value="Add">
<input type="reset" name="clear" value="Clear">
</form>
<?php
if(isset($_POST['add']))
{
if(isset($_POST['colors']))
{
$colorVal = $_POST['colors'];
echo "$colorVal";
}
}
?>
Defines a submit button (for submitting the form) by making its type as SUBMIT
<input type="submit">
The <input type="button"> defines a clickable button (mostly used with
a JavaScript to activate a script).
Also, you have a syntax error for $_POST
HTML CODE :
<div style="padding-left:40px">
<h2> PHP Calculator</h2>
<form method="post" action="">
Enter value: <input type="text" name="value"> <br> <br>
<div style="padding-left: 105px">
<input type="submit" value="9" name="button">
<input type="submit" value="8" name="button">
<input type="submit" value="7" name="button">
<input type="submit" value="+" name="button"> <br>
<input type="submit" value="6" name="button">
<input type="submit" value="5" name="button">
<input type="submit" value="4" name="button">
<input type="submit" value="-" name="button" style="padding-left: 9px"> <br>
<input type="submit" value="3" name="button">
<input type="submit" value="2" name="button">
<input type="submit" value="1" name="button">
<input type="submit" value="/" name="button" style="padding-left: 9px"> <br>
<input type="submit" value="0" name="button" style="padding- left:33px">
<input type="submit" value="." name="button" style="padding-right:9px">
<input type="submit" value="x" name="button" style="padding left: 7px"><br>
</div> <br>
<input type="submit" value="Calculate" name="Calculator">
</form>
</div>
PHP CODE :
<?php
if (isset($_POST["Calculate"]))
{
echo $_POST["button"];
} ?>
I'm developing a PHP calculator, actually my question is when i click to any of the input buttons the value should display in the text box. above is the code what i have tried with, but im unable to get the result. can any one help me in my work,so that how i can get the desired result.
There is nothing like you cannot do in PHP but if you could do it using Jquery it will be much more better in terms of User experience as well. Looking at your HTML , I can see you have a input type of submit which means on each click it will submit a form unless and until you have written any condition not to submit. Also your name for each button is also same and it will make difficult to track which one is clicked/entered, please make sure you have unique name field. If you want to do it in PHP only then on every submit you can save that value in session or cookies and while loading page you can check against that session variable or cookies and can show it the the input value.
You can see following code as a reference but it is using jquery.
<div style="padding-left:40px">
<h2> PHP Calculator</h2>
<form method="post" action="" id="formID">
Enter value: <input type="text" name="value" id='btnValue'> <br> <br>
<div style="padding-left: 105px">
<input type="button" value="9" name="btn1">
<input type="button" value="8" name="btn2">
<input type="button" value="7" name="btn3">
<input type="button" value="+" name="btn4"> <br>
<input type="button" value="6" name="btn5">
<input type="button" value="5" name="btn6">
<input type="button" value="4" name="btn7">
<input type="button" value="-" name="btn8" style="padding-left: 9px"> <br>
<input type="button" value="3" name="btn8">
<input type="button" value="2" name="btn9">
<input type="button" value="1" name="btn10">
<input type="button" value="/" name="btn11" style="padding-left: 9px"> <br>
<input type="button" value="0" name="btn12" style="padding- left:33px">
<input type="button" value="." name="btn13" style="padding-right:9px">
<input type="button" value="x" name="btn14" style="padding left: 7px"><br>
</div> <br>
<input type="submit" value="Calculate" name="Calculator">
</form>
</div>
$('input[type=button]').on('click',function(){
if($('#btnValue').val()==''){
$('#btnValue').val($(this).val());
}else{
$('#btnValue').val($('#btnValue').val()+($(this).val()));
}
})
Here is the jsfiddle you can look on https://jsfiddle.net/nef1qgzn/13/
Note: I have user Jquery libraray 3.3.1
the variable $res stock just the last submit input clicked. it must stock all submit values which i clicked in.
<html>
<head></head>
<body>
<form method="post" action="">
<p><input type="text" name="textbox" size="13" readonly></p>
<p>
<input type="submit" name="one" value="1">
<input type="submit" name="one" value="2">
<input type="submit" name="one" value="3">
</p>
<p>
<input type="submit" name="one" value="4">
<input type="submit" name="one" value="5">
<input type="submit" name="one" value="6">
</p>
<p>
<input type="submit" name="one" value="7">
<input type="submit" name="one" value="8">
<input type="submit" name="one" value="9">
</p>
<p>
<input type="submit" name="operation" value="+">
<input type="submit" name="one" value="0">
<input type="submit" name="operation" value="-">
</p>
</form>
php code :
<?php
$res="";
if(isset($_POST['one']))
{
$val = $_POST['one'];
$res.=$val;
echo $res;
}
?>
</body>
</html>
concatenation doesn't work on input submit, $res stock just a value of one input.
if u want to concatenate something it goes like
$res = "something";
$res .= $_POST['one']
.= concatenates strings .. what you are doing is that you are assigning the value to the string, so whatever is inside will be replaced by the $_POST['one'] value
from what i can see from your html, your designing a calculator. so you want to enter each number assigned to a button into the text field. try this:
html file
<html>
<head></head>
<body>
<form method="post" action="">
<p><input type="text" name="textbox" size="13" readonly></p>
<p>
<input type="submit" name="one" value="1">
<input type="submit" name="two" value="2">
<input type="submit" name="three" value="3">
</p>
<p>
<input type="submit" name="four" value="4">
<input type="submit" name="five" value="5">
<input type="submit" name="six" value="6">
</p>
<p>
<input type="submit" name="seven" value="7">
<input type="submit" name="eight" value="8">
<input type="submit" name="nine" value="9">
</p>
<p>
<input type="submit" name="plus" value="+">
<input type="submit" name="zero" value="0">
<input type="submit" name="minus" value="-">
</p>
</form>
function.php
<?php
if (isset($_POST['one'])) {
$num1 .= $_POST['one'];
echo $num1;
}
if (isset($_POST['two'])) {
$num2 .= $_POST['two'];
echo $num2;
etc...........
}
?>
I have some buttons in a first_page.php, and some checkboxes in a second_page.php.
I need to select the corresponding checkbox with a query string to get this:
When "first value button" is pressed --> "second_page.php" with "my first value" checkbox already selected.
first_page.php :
<form action="second_page.php">
<input class="btn" type="submit" value="first value button">
<input class="btn" type="submit" value="second value button">
<input class="btn" type="submit" value="third value button">
</form>
second_page.php :
<form name="name" method="post" action="#">
<input type="checkbox" name="mybox[]" value="my first value"/>
<span>my first box</span><br />
<input type="checkbox" name="mybox[]" value="my second value"/>
<span>my second box</span><br />
<input type="checkbox" name="mybox[]" value="my third value"/>
<span>my third box</span><br />
</form>
You have to specify a name for each input to connect it to $_POST then in second_page.php you have to fetch the form value.
In first_page.php:
<form action="second_page.php" method="post">
<input name="first_value_btn" class="btn" type="submit" value="first value button">
<input name="second_value_btn" class="btn" type="submit" value="second value button">
<input name="third_value_btn" class="btn" type="submit" value="third value button">
</form>
In second_page.php :
<input type="checkbox" name="prodotti[]" value="my first value" <?php echo ( isset($_POST['first_value_btn']) ? 'checked="checked"' : '');?> />
Read more
I've used POST as method in my example above, you could use GET instead and then replacing $_POST with $_GET instead.
I would give the Submit buttons names and then do a check on them on the next page:
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit1'])) { echo 'checked'; ?> />
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit2'])) { echo 'checked'; ?> />
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit3'])) { echo 'checked'; ?> />
This is my HTML:
<form method="POST" action="">
<?php
$skillSubCategory = $skills->showSkills(24);
for ($i = 0; $i < count($skillSubCategory); $i++) {
?>
<input type="hidden" name="skillid" value="<?php echo $skillSubCategory[$i]['skill_id']; ?>" />
<?php echo $skillSubCategory[$i]['title']; ?>
<input type="submit" name="add" value="add" /><br />
<?php } ?>
</form>
<?php if (isset($_POST['add'])) {
echo $_POST['skillid'];
} ?>
Resulting source code:
<form method="POST" action="">
<input type="hidden" name="skillid" value="25" />
Animal Grooming
25
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="26" />
Dog Trainer
26
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="27" />
Dog Walking
27
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="28" />
Vet
28
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="29" />
Beekeeping
29
<input type="submit" name="add" value="add" /><br />
</form>
What it looks like:
I get number 29 for any button clicked. Any ideas what's wrong? Why the correct number wont show up when i click add?
You can also use the buttons themselves(without changing their values):
<input type="submit" name="skillid[25]" value="add" />
<input type="submit" name="skillid[26]" value="add" />
<input type="submit" name="skillid[27]" value="add" />
To retrieve the submitted value(its not the value in this case, its the first key of the posted array):
if(isset($_POST['skillid']) && is_array($_POST['skillid']))
{
echo key($_POST['skillid'])
}
Because when you have multiple fields with the same name attribute in a form, the last one always takes precedence (with the exception of submit buttons -- the one clicked will be the only one considered). So the last hidden input with the name skillid will always be sent to the server.
When using forms like this, you usually have to use separate forms for each button. Alternatively, change the value attribute of each button and consider that from your PHP code.
Change:
<form method="POST" action="">
to:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
then change the condition to:
if (isset($_POST['add']) && isset($_POST['skillid'])) {
EDIT: use the <option> tag instead
<select name="skillid">
<option value="25">Animal Grooming</option>
<option value="26">Dog Trainer</option>
...
</select>
Your PHP code now will be:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$skillSubCategory = $skills->showSkills(24);
<select name="skillid">
for ($i = 0; $i < count($skillSubCategory); $i++) { ?>
<option value="<?php echo $skillSubCategory[$i]['skill_id']; ?>"><?php echo $skillSubCategory[$i]['title']; ?></option>
<?php } ?>
</select>
<input type="submit" name="add" value="add" /><br />
</form>
if (isset($_POST['add']) && isset($_POST['skillid'])) {
echo $_POST['skillid'];
} ?>