I have my code for my PHP:
<?php
$num1= $_REQUEST["number1"];
$num2= $_REQUEST["number2"];
$arithmetic = $_POST['arithmetic'];
switch ($arithmetic) {
case "addition":
$answer = $num1+$num2;
echo "<p>$num1 added with $num2 is " . number_format($answer,2) . "</p>";
break;
case "subtraction":
$answer = $num1-$num2;
echo "<p>$num2 subtracted from $num1 is " . number_format($answer,2) . "</p>";
break;
case "multiplication":
$answer = $num1*$num2;
echo "<p>$num1 multiplied by $num2 is " . number_format($answer,2) . "</p>";
break;
case "division":
$answer = $num1/$num2;
echo "<p>$num1 divided by $num2 is " . number_format($answer,2) . "</p>";
break;
case "modulus":
$answer = $num1%$num2;
echo "<p>The remainder of $num1 divided by $num2 is " . number_format($answer,2) . "</p>";
break;
}
?>
<br>
<form action="calcD.html" target="iframeMain">
<input type="submit" value="go back">
</form>
<?php
echo "<HR>";
highlight_file("calcD.php");
?>
and my code for my HTML:
<html>
<head>
<link href="reset.css" rel="stylesheet" type="text/css">
<link href="css3.css" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Scada:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
<h2>Dropdown Calculator</h2>
<form action="calcD.php" method="POST" target="iframeMain" autocomplete="off">
<p>Number 1:<input type="text" name="number1"></p>
<p>Number 2:<input type="text" name="number2"></p>
<h3>Type of Arithmetic</h3>
<select name="arithmetic">
<option name="addition">Addition</option>
<option name="subtraction">Subtraction</option>
<option name="multiplication">Multiplication</option>
<option name="division">Division</option>
<option name="modulus">Modulus</option>
</select>
<input type="submit" name="compute" value="Compute">
</form>
Back to Menu
</body>
</html>
But when I try to use my programs, the answer doesn't display. I have check on multiple sites and there are no errors with my HTML nor my PHP but my answer is not displaying. I have even tried using If and Elseif statements for each program. Is there a link I am missing for them?
EDIT: I need to use a switch (strtolower($arithmetic)) instead of the standard switch to allow it to display.
The error is in your options. You have
<option name="subtraction">Subtraction</option>
They should have values, not names:
<option value="subtraction">Subtraction</option>
Use switch (strtolower($arithmetic)). you have up case in your arithmetic post value...
Try :
$num1= $_POST["number1"];
$num2= $_POST["number2"];
Related
I have just written this code in which I have a form. You have to write your name, surname and country. You also have to choose your favourite colour. After that, you push a submit button so that you can see the data afterwards. I'm using the GET method with 1 page, but I have to use a second one with the POST method so that each echo is on that second page.
How could I do that? My code is:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form</title>
<link rel="stylesheet" type="text/css" href="form.css">
</head>
<body>
<section>
<?php
if (isset($_GET["name"])){
$name = $_GET["name"];
if ($name != ""){
$surname = $_GET["surname"];
$country = $_GET["country"];
$colour = $_GET["colour"];
echo "<p>";
echo "<h2>Data</h2>";
echo $name . " " . $surname . "</br />";
echo $country . "<br />";
echo $colour;
echo "</p>";
}else
echo "<strong>Complete the blank spaces</strong>";
}else{
?>
<h1>Form</h1>
<form class="elegant" method="GET" action="?">
<fieldset>
<legend>Favourite colour</legend>
<div>
<label for="nombre">Name</label>
<input type="text" placeholder="Name" name="name"
id="name" />
</div>
<div>
<label for="surname">Surname</label>
<input type="text" placeholder="Surname" name="surname"
id="surname" size="50" />
</div>
<div>
<label for="country">Country</label>
<input type="text" placeholder="Country" name="country" id="country"
size="10" maxlength="9" />
</div>
<div>
<select name="colour" id="colour">
<option value="yellow" <?php if ($colour == "yellow" ) echo "selected" ?> >yellow</option>
<option value="red" <?php if ($colour == "red" ) echo "selected" ?> >red</option>
</div>
<input class="btn" type="submit" value="Save" />
</fieldset>
</form>
<?php
}
?>
</section>
</body>
</html>
I know i have to use a link to that second page, but that's all I know. Thanks in advance!
If I understand correctly, the code you show is for Page 1 where the user can:
Input data if data don't exist;
View data and confirm them if they exist. At this point store data in SESSION and send the user to an other page.
To do so, remember you have to add the session_start() command right at the beginning of every page in which you will be able to manipulate Session Data.
<?php
session_start();
if (isset($_GET["name"])){
$name = $_GET["name"];
if ($name != ""){
$surname = $_GET["surname"];
$country = $_GET["country"];
$colour = $_GET["colour"];
echo "<p>";
echo "<h2>Data</h2>";
echo $name . " " . $surname . "</br />";
echo $country . "<br />";
echo $colour;
echo "</p>";
$_SESSION["name"] = $name;
$_SESSION["surname"] = $surname;
$_SESSION["country"] = $country;
$_SESSION["colour"] = $colour;
Confirm
}else
echo "<strong>Complete the blank spaces</strong>";
}else{
?>
...
In "another_page.php" then you will find that you can access your data simply querying the $_SESSION array.
<?php
session_start();
...
// Echo session variables that were set on previous page
echo "Name is " . $_SESSION["name"] . ".<br>";
echo "Surname is " . $_SESSION["surname"] . ".";
// etc.
?>
Complete reference is the PHP Manual and simple reference is in W3C PHP Sessions page.
In the form tag you can specify where to form gets submitted to:
<form class="elegant" method="GET" action="YOUR_PAGE_URL_HERE">
On that second page you get the values via the $_GET Variable like
<?php
echo $_GET['name'].' '.$_GET['surname'];
I can't figure out what to target to change my output to a different color besides black. I was thinking maybe operator but I'm not really sure. Couldn't find anywhere on here with this question so here I am. I know you can fix this issue by using Javascript but I'm not looking to do that. Any help would be appreciated, thanks.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Calculator</title>
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="container">
<form method="POST" action="calculator.php">
<h1>PHP Calculator</h1>
<input type="text" class="form-control" name="num1" placeholder="Number 1"><br>
<input type="text" class="form-control" name="num2" placeholder="Number 2"><br>
<select name="operator">
<option>None</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<br>
<button name="submit" value="submit" type="submit">Calculate</button>
</form>
<p>The answer is:</p>
<?php
if (isset($_POST['submit'])) {
$result1 = $_POST['num1'];
$result2 = $_POST['num2'];
$operator = $_POST['operator'];
switch ($operator) {
case "None":
echo "You need to select a method!";
break;
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
</body>
</html>
Put the php around some html tag like or and then give it css attributes like:
<a style="color:red;"> <?php // php calculator here ?> </a>
If you want one and the same color for all output, you can simply use inline CSS. Check out this tutorial - it has an example for how to make text blue.
<p>The answer is:</p>
<p style="color: blue;">
<?php
if (isset($_POST['submit'])) {
$result1 = $_POST['num1'];
$result2 = $_POST['num2'];
$operator = $_POST['operator'];
switch ($operator) {
case "None":
echo "You need to select a method!";
break;
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
</p>
The key is to wrap your answer in an HTML tag (such as <p> or <span> or <div>) and adding style="color: red;".
In your echo statement you can wrap the text in HTML tags and then use CSS.
PHP:
$result = $result1 + $result2;
echo "<span class='output'>" . $result . "</span>";
CSS:
<style> .output{ color:blue; } </style>
I am creating a calculator on the web to challenge my ability to think an challenge my creative edge and i got the basics down it works but the issue i am having is the error displaying whem there is no error is just displays when the page loads and then displays the value when the calculator is used can someone please help?
<?php
$value1 = $_POST['value1'];
$symbol = $_POST['operator'];
$value2 = $_POST['value2'];
$nulr = "nulrl";
if($nulr === "nulrl"){
if($symbol === "+"){
$output = $value1 + $value2;
} elseif($symbol === "-"){
$output = $value1 - $value2;
} elseif($symbol === "/"){
$output = $value1 / $value2;
} elseif($symbol === "*"){
$output = $value1 * $value2;
} else{
$output = "Error could not perform operation";
}
}
echo $output;
?>
EDIT
Here is my html code
<!DOCTYPE html>
<html lang="en">
<head>
<title>The Calculator</title>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>The Goal</h1>
<p>Through my studies of coding languages i have come to realize that just about every coding langauage no matter how different the syntax can handle user input and variables and produce and output. With this being said i wanna make an interactive calculator for each coding language and then advance on each one and see which language can produce the best calculator!</p>
<main>
<h2>PHP Calculator</h2>
<form method="post">
<input type="number" name="value1" value="value1" class="number-box">
<br>
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<br>
<input type ="number" name="value2" value="value2" class="number-box">
<br>
<input type ="submit" name="submit" class="submit-button">
</form>
<?include("calculator.php")?>
<footer>
<h3>Comment Box</h3>
<form method="post" action="comment.php">
<input type="text" value="name" name="name">
<br>
<textarea name="comment">Comments</textarea>
<input type="submit" name="submit" class="submit-button">
</form>
<?php
echo file_get_contents("comments.txt");
?>
</footer>
</main>
</header>
EDIT:
Still have not recieved any correct answers
The simplest way:
<?php
if(isset($_POST['value1']){
//.......
else{
$output = '';
echo "Error, could not perform operation";
}
}
echo $output;
}
?>
Put your code like this:
$http_post = ($_SERVER['REQUEST_METHOD'] == 'POST') ;
if($http_post)
{
$value1 = $_POST['value1'];
$symbol = $_POST['operator'];
$value2 = $_POST['value2'];
$nulr = "nulrl";
if($nulr === "nulrl"){
if($symbol === "+"){
$output = $value1 + $value2;
} elseif($symbol === "-"){
$output = $value1 - $value2;
} elseif($symbol === "/"){
$output = $value1 / $value2;
} elseif($symbol === "*"){
$output = $value1 * $value2;
}
else
{
$output = "Error could not perform operation";
}
}
echo (!empty($output)) ? $output : '';
If any errors occur it will appear only after post.
The code:
<!DOCTYPE html>
<html>
<head>
<title>Greeting Service!</title>
</head>
<body>
<center>
<form method="post" action="">
<h1>What's Your Name?</h1>
<input type="text" name="name" placeholder="Name Here" />
<h4>Greet me in:
<select name="language">
<option value="option1">English</option>
<option value="option2">Chinese</option>
<option value="option3">French</option>
</select>
</h4>
<input type="submit" value="Greet Me!" />
<?php
if (isset($_POST['language'])) {
switch ($language) {
case "option1":
$result = "Hello, $name!";
break;
case "option2":
$result = "你好, $name!";
break;
case "option3":
$result = "Bonjour, $name!";
break;
}
}
?>
</form>
</center>
</body>
</html>
I'm not sure why it doesn't display the name i enter in from the input text box with the greeting according to each different language. I have used $_POST to submit the form and used a switch and case in my php code. Can someone help me out, i would like the name that is entered in the textbox to display when i have chosen which language i want the greeting to come out with. Thanks.
You don't have $language defined. What you should use is $_POST['language'] :
if (isset($_POST['language'])) {
switch ($_POST['language']) {
EDIT: didn't notice the $name variable as said by Meenesh Jain.
And the last thing: you never do anything with the result. Not sure if that's intended or not, but I would add an echo $result; at the end.
This is happening because you are not initializing any value to $language
just place code like $language = $_POST['language'];
above the switch statement.
declare a varibale $name and assign $_POST['name'] to it
<!DOCTYPE html>
<html>
<head>
<title>Greeting Service!</title>
</head>
<body>
<center>
<form method="post" action="">
<h1>What's Your Name?</h1>
<input type="text" name="name" placeholder="Name Here" />
<h4>Greet me in:
<select name="language">
<option value="option1">English</option>
<option value="option2">Chinese</option>
<option value="option3">French</option>
</select>
</h4>
<input type="submit" value="Greet Me!" />
<?php
if (isset($_POST['language'])) {
$name = $_POST['name'];
switch ($language) {
case "option1":
$result = "Hello, $name!";
break;
case "option2":
$result = "你好, $name!";
break;
case "option3":
$result = "Bonjour, $name!";
break;
}
}
?>
</form>
</center>
</body>
</html>
$name is not defined
change your code like this
if (isset($_POST['language'])) {
switch ($_POST['language']) {
case "option1":
$result = "Hello, {$_POST['name']}!";
break;
case "option2":
$result = "你好, {$_POST['name']}!";
break;
case "option3":
$result = "Bonjour, {$_POST['name']}!";
break;
}
}
?>
tried using php to solve your question but could not get it through but Javascript does it seamlessly. Tested though. Find code below
<center>
<form method="post" action="index.php">
<h1>What's Your Name?</h1>
<input type="text" name="myName" placeholder="Name Here" id="myName"/>
<h4>Greet me in:
<select name="lang" onchange="getValue()" id="language">
<option value="English">English</option>
<option value="Chinese">Chinese</option>
<option value="French">French</option>
</select>
</h4>
<input type="submit" value="Greet Me!" />
<div id="show"></div>
<script type="text/javascript">
function getValue(){
var myName = document.getElementById("myName").value;
var lang = document.getElementById("language").value;
if(myName == null){
document.getElementById("show").innerHTML = "No name selected";
}
else{
switch(lang) {
case 'English':
document.getElementById("show").innerHTML = "Hello" + ' ' + myName;
break;
case 'Chinese':
document.getElementById("show").innerHTML = "你好" + ' ' + myName;
break;
case 'French':
document.getElementById("show").innerHTML = "Bonjour" + ' ' + myName;
break;
}
}
}
</script>
Copied the code and replace it where you have same.
Glad i could help.
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 );