I'm trying to make a simple php calculator which multiplies quantity to predefined prices.
Here is my code but I got error 500. I can't manage to fix it by myself.
It's html form with $_post checks and $_post submit button
// Check if form was submitted
$product = $_POST['product'];
$quantity = $_POST['quantity'];
$first = 0,6;
$second = 0,5;
$third = 2,8;
if ( isset($_POST['submit']) ) {
switch($_POST['product']) {
case '1':
$calculation = $quantity * $first;
echo "Цена" . $calculation;
break;
case '2':
$calculation = $quantity * $second;
echo "Цена" . $calculation;
break;
case '3':
$calculation = $quantity * $third;
echo "Цена" . $calculation;
break;
default:
}
}
?>
<form action="" method="post">
<select name="product" id="product">
<option>Choose servicve</option>
<option value="1">Service 1</option>
<option value="2">Service 2</option>
<option value="3">Service 3</option>
</select>
<br>
<input type="number" name="quantity" id="quantity">
<br>
<input type="submit" name="submit" value="submit">
</form><br /> ```
Just simple mistake for simple calculator, you printed "," instead of point(".")
$first = 0,6;
$second = 0,5;
$third = 2,8;
Related
I want to display results of the calculation at the end of the program as a list. After each post, the result is added to the list of previous results.
Simply, I want to display a history of the result of the calculations done.
<form method="post" >
<p>A:<input value="<?php echo $_POST['num1']?>" min="0" max="1000" type="number" name="num1"></p>
<p>B:<input value="<?php echo $_POST['num2']?>" min="0" max="1000" type="number" name="num2"></p>
<p>
<select name="op">
<option value="jaam">+</option>
<option value="zarb">*</option>
<option value="taghtsim">/</option>
<option value="tafrigh">-</option>
</select>
</p>
<input type="submit" name="">
<hr>
</form>
<?php
if (isset($_POST['num1']) && isset($_POST['num2']) && isset($_POST['op']))
{
switch ($_POST['op'])
{
case 'jaam':
$r = $_POST['num1'] + $_POST['num2'];
break;
case 'zarb':
$r = $_POST['num1'] * $_POST['num2'];
break;
case 'taghtsim':
$r = $_POST['num1'] / $_POST['num2'];
break;
case 'tafrigh':
$r = $_POST['num1'] - $_POST['num2'];
break;
default:
$r = "Error!";
break;
}
}
?>
I have the following code for a simple calculator. The code works fine, but I want the value in the dropdown list to stay there after submitted. How would I do this?
Essentially, I want the operator to stay selected once the calculation has been done. At the moment, it just shows a '+', even if the sum is 25 / 5.
<?php
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
$operation = $_POST['operator'];
Switch ($operation) {
case 'add': $answer = $number1 + $number2;
break;
case 'minus': $answer = $number1 - $number2;
break;
case 'divide': $answer = $number1 / $number2;
break;
case 'multiply': $answer = $number1 * $number2;
break;
}
?>
<form name='calculator' method='post' action=''>
<table>
<tr>
<td>
<input name="number1" type="text" value="<?php i if(isset($_POST['number1'])) { echo htmlentities($_POST['number1']);}?>" />
</td>
<td>
<select name="operator">
<option value="add">+</option>
<option value="minus">-</option>
<option value="divide">/</option>
<option value="multiply">x</option>
</select>
</td>
<td>
<input name="number2" type="text" value="<?php if(isset($_POST['number2'])) { echo htmlentities($_POST['number2']);}?>" />
</td>
<td>
<input name="submit" type="submit" value="=" />
</td>
<td>
<input name="" type="text" value="<?php echo $answer ?>" />
</td>
</tr>
</table>
You have to set the selected attribute for the option that was submitted. So you have to check the submitted value for each option. In my solution I am using the ternary operator to echo the selected attribute only for the correct operator.
<select name="operator">
<option value="add" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'add') ? 'selected' : ''; ?>>+</option>
<option value="minus" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'minus') ? 'selected' : ''; ?>>-</option>
<option value="divide" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'divide') ? 'selected' : ''; ?>>/</option>
<option value="multiply" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'multiply') ? 'selected' : ''; ?>>x</option>
</select>
The code above is somewhat repetitive. It keeps repeating a lot of code and html. It would be great if we could factor out the repetitive stuff. Luckily we can do that by creating an array that stores the options and loop through them using a foreach, like this:
<?php
$options = [
'add' => '+',
'minus' => '-',
'divide' => '/',
'multiply' => 'x'
];
?>
<select name="operator">
<?php foreach ($options as $key => $label) { ?>
<option value="<?= $key ?>" <?= (isset($_POST['operator']) && $_POST['operator'] == $key) ? 'selected' : '' ?>><?= $label ?></option>
<?php } ?>
</select>
I'm trying to use this search for searching more than one words or single word at a time from a database. Now the problem is that my script only running properly but please help me...
<form name="ds" method="post">
<input type="text" name="name" placeholder="Entername" />
<!--<input type="text" name="search" placeholder="Location" />-->
<select name="location[]" multiple="multiple">
<option value="delhi">Delhi</option>
<option value="Mumbai">Mumbai</option></select>
<input type="submit" name="submit" value="Search" />
</form>
<?
$conn=mysql_connect("localhost","root","");
mysql_select_db("dbrozgarexpress",$conn);
echo $search =$_POST['location'];
$description = "";
$name2=$_POST['name'];
if(isset($name2)&&$name2 != ""){
if($flag){
$cheack.= "AND ";
}
$cheack.="user_first_name ='$name2' ";
$flag =true;
}
if(isset($search)&&$search != ""){
if($flag){
$cheack.= "AND ";
}
foreach($search AS $s)
{
$cheack.="user_current_location_id ='$s' or ";
$flag =true;
}
}
$cheack = substr($cheack, 0, -4);
echo $query = "SELECT * FROM `tb_user` WHERE $cheack ";
?>
error SELECT * FROM `tb_user` WHERE user_first_name ='kum
I think I have a general idea of what you are after.
P.S. the query will only show after you submit the form - i.e. after you click search button
Try this:
<?php
// Handle Post
if (count($_POST))
{
// Load Params
$name = isset($_POST['name']) ? $_POST['name'] : '';
$locations = isset($_POST['location']) ? $_POST['location'] : array();
// Start Query
$sql = 'SELECT * FROM `tb_user` WHERE ';
// Build Query
$sql_parts = array();
if (!empty($name)) {
$sql_parts[] = "`user_first_name` = '$name'";
}
if (sizeof($locations)) {
foreach ($locations as $location) {
$sql_parts[] = "`user_current_location_id` = '$location'";
}
}
$sql = $sql . implode(' AND ', $sql_parts);
// Debug
echo $sql ."<br><br>";
}
?>
<form action="" name="ds" method="post">
<input type="text" name="name" placeholder="Entername" />
<select name="location[]" multiple="multiple">
<option value="delhi">Delhi</option>
<option value="Mumbai">Mumbai</option></select>
<input type="submit" name="submit" value="Search" />
</form>
Here's an example of this working:
No location selected, name only
name and one location
name and two location
Hello i'm trying to make a calculator.And i have this problem : I'm tryng to make the selection that i made it to stay after submit. I've found some code on google for my selection to stay after submit but my calculator won't work anymore. Can you help me ?
here is the code when my calculator works but my selection doesn"t stays after submit
<html>
<body>
<center>
<form method="post">
Food:
<Select name="Dropdown"><br>
<option>Cheese</option>
<option>Apple</option>
<option>Egg</option>
</select>
</br>
Amount:
<input name="amount" type="text">grams<br><br>
<br><input type="Submit" value="Calculate">
<br><br>
<?php
$result=$_POST['result'];
$Dropdown=$_POST['Dropdown'];
$amount = $_POST['amount'];
switch ($Dropdown){
case 'Cheese':
$result= (7.74 * $amount) / 100;
echo "<p> $result </p>";
break;
case 'Apple':
$result= (1.94 * $amount) / 100;
echo "<p> $result </p>";
break;
case 'Egg':
$result= (13.74 * $amount) / 100;
echo "<p> $result </p>";
}
?>
</form>
</center>
</body>
</html>
and here is my code when my slection stays after submit but my calculator won't work
<html>
<head></head>
<body>
<center>
<?php
if (!empty($_POST['Dropdown'])) {
$dropDownVal = $_POST['Dropdown'];
} else {
$dropDownVal = 1;
}
?>
<form method="post">
<select name="Dropdown" >
<option value="1" <?php if ($dropDownVal==1) echo 'selected="selected"'; ?>>Cheese </option>
<option value="2" <?php if ($dropDownVal==2) echo 'selected="selected"'; ?>>Apple</option>
<option value="3" <?php if ($dropDownVal==3) echo 'selected="selected"'; ?>>Egg</option>
</select>
<?php
$result=$_POST['result'];
$Dropdown=$_POST['Dropdown'];
$amount = $_POST['amount'];
switch ($Dropdown){
case 'Cheese':
$result= (7.74 * $amount) / 100;
echo "<p> $result </p>";
break;
case 'Apple':
$result= (1.94 * $amount) / 100;
echo "<p> $result </p>";
break;
case 'Egg':
$result= (13.74 * $amount) / 100;
echo "<p> $result </p>";
}
?>
<input name="amount" type="text">grams<br><br>
<br><input type="Submit" value="Calculate">
</form>
</center>
</body>
</html>
Thanks
What you receive back in the $_POST['Dropdown'] is the value not the content so you will get 1 or 2 or 3 and not the Cheese or Apple or Egg.
So try:
switch ($Dropdown){
case 1: // Cheese
$result= (7.74 * $amount) / 100;
break;
case 2: //Apple
$result= (1.94 * $amount) / 100;
break;
case 3: // Egg
$result= (13.74 * $amount) / 100;
break;
default:
$result = 0;
}
echo "<p> $result </p>";
?>
In future if you are not sure what is in a variable that is returned from the user do a
echo '<pre>' . print_r( $_POST, TRUE ) . '</pre>';
to get a nice display of the array on the browser.
Or a
var_dump( $var );
I've recently started the assignment of building a calculator out of PHP and I can't seem to find what I'm doing wrong in my code. Every time I press calculate it doesn't give me back my solution.
<?php
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$cal = $_GET['opt'];
if($num2, $num2 != (int)){
$num1=0;
$num2=0;
}
switch($cal) {
case 'add':
echo $num1+$num2;
break;
case 'sub':
echo $num1-$num2;
break;
case 'mul':
echo $num1*$num2;
break;
case 'div':
echo $num1/$num2;
break;
default:
echo "Invalid Operator";
}
?>
Here is the HTML
<form action="calculate.php" method="GET"/>
Number 1:<input type="text" name="num1"/>
<br />
<select>
<option type="text" name="opt" value="add"> + </option>
<option type="text" name="opt" value="sub"> - </option>
<option type="text" name="opt" value="mul"> * </option>
<option type="text" name="opt" value="div"> / </option>
</select>
<br />
Number 2:<input type="text" name="num2"/>
<br />
<input type="submit" value="calculate"/>
</form>
if($num2, $num2 != (int)) looks like a syntax error to me (the comma).
You just say it doesn't work, are you getting an error message? Have you made sure error reporting is on and displaying errors to your browser? I think it should tell you about the syntax error.
try :
$num1 = intval($_GET['num1']);
$num2 = intval($_GET['num2']);
and remove
if($num2, $num2 != (int)){
$num1=0;
$num2=0;
}
I would initialise the var differently:
<?php
$num1 = $num2 = 0;
if (isset($_GET['num1']) && isset($_GET['num1']) && isset($_GET['num1']))
{
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
// edit: added validation
if (!is_numeric($num1) || !is_numeric($num2))
{
$res = NULL;
}
else
{
$cal = $_GET['opt'];
switch($cal)
{
case 'add':
$res = $num1+$num2;
break;
case 'sub':
$res = $num1-$num2;
break;
case 'mul':
$res = $num1*$num2;
break;
case 'div':
$res = $num1/$num2;
break;
default:
$res = NULL;
}
}
}
// display html on the same file
?>
<html>
<body>
<form action="calculate.php" method="GET"/>
Number 1:<input type="text" name="num1"/>
<br />
<select>
<option type="text" name="opt" value="add"> + </option>
<option type="text" name="opt" value="sub"> - </option>
<option type="text" name="opt" value="mul"> * </option>
<option type="text" name="opt" value="div"> / </option>
</select>
<br />
Number 2:<input type="text" name="num2"/>
<br />
<input type="submit" value="calculate"/>
</form>
<? if (isset($res) && $res != NULL): ?>
<span class="result-label">Result:</span> <span class="result"><?=$res?></span>
<? endif ?>
</body>
</html>
This is wrong
if($num2, $num2 != (int)){
$num1=0;
$num2=0;
}
try
$num1 = intval( $num1 );
$num2 = intval( $num2 );