Name from textbox is not echoing from php - php

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.

Related

A swap button for a currency converter

I have a problem with this code, what happens is that I want to create a swap button to change two selectbox of place, such as the google currency converter and those of the internet, I would greatly appreciate your help if you taught me how to do it, the code I did with php. And an apology if I said something wrong I am a beginner in this world
// Output text
$out = $ans = $txtAmount =
// txtSelectBox1 selection
$selA0 = $selA1 = $selA2 = $selA3 =
// txtSelectBox2 selection
$selB0 = $selB1 = $selB2 = $selB3 = "";
// Check if form was submitted
if(isset($_POST['txtSelectBox1'])) {
// Get post values
$txtSel1 = $_POST['txtSelectBox1'];
$txtSel2 = $_POST['txtSelectBox2'];
$txtAmount = $_POST['txtAmount'];
// Exchange rates
$cur = array("MXN", "USD", "EUR", "JPY");
$php = array("1", "0.050", "0.048", "6.78");
$cad = array("20.12", "1", "0.96", "136.50");
$hkd = array("20.99", "1.04", "1", "142.51");
$jpy = array("0.15", "0.0073", "0.0070", "1");
// Switch statement for conversion
switch ($txtSel1) {
case "php":
$selA0 = ${"selB".$txtSel2} = "selected";
$ans = $txtAmount * floatval($php[$txtSel2]);
$case = 0;
break;
case "cad":
$selA1 = ${"selB".$txtSel2} = "selected";
$ans = $txtAmount * floatval($cad[$txtSel2]);
$case = 1;
break;
case "hkd":
$selA2 = ${"selB".$txtSel2} = "selected";
$ans = $txtAmount * floatval($hkd[$txtSel2]);
$case = 2;
break;
case "jpy":
$selA3 = ${"selB".$txtSel2} = "selected";
$ans = $txtAmount * floatval($jpy[$txtSel2]);
$case = 3;
break;
}
// Display output
$out = '<div class="answer">'.$txtAmount.' '.$cur[$case].' = '.$ans.' '.$cur[$txtSel2].'</div>';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Conversor de Divisas</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<main>
<div class="container">
<div class="title">
<h1>CONVERSOR</h1>
</div>
<div class="container-wrapper">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" class="frmSelect" name="frm">
<div class="selectWrapper">
<select class="txtSelectBox" name="txtSelectBox1" id="txtSelectBox1">
<option value="php" <?php echo $selA0; ?>>Peso Mexicano (MXN)</option>
<option value="cad" <?php echo $selA1; ?>>Dolar (USD)</option>
<option value="hkd" <?php echo $selA2; ?>>Euro (EUR)</option>
<option value="jpy" <?php echo $selA3; ?>>Yen Japones (JPY)</option>
</select>
<select class="txtSelectBox" name="txtSelectBox2" id="txtSelectBox2">
<option value="0" <?php echo $selB0; ?>>Peso Mexicano (MXN)</option>
<option value="1" <?php echo $selB1; ?>>Dolar (USD)</option>
<option value="2" <?php echo $selB2; ?>>Euro (EUR)</option>
<option value="3" <?php echo $selB3; ?>>Yen Japones (JPY)</option>
</select>
</div>
<input type="number" class="txtbox" name="txtAmount" id="txtAmount" value="<?php echo $txtAmount; ?>" placeholder="Input Amount" required>
<div class="btnWrapper">
Restaurar
<button type="submit" class="btn btnConvert" name="btnConvert">Convertir</button>
</div>
<?php echo $out; ?>
</form>
</div>
</div>
</main>
</body>
</html>```

Why isn't this code parsing properly for the answer?

The code will not produce a calculated result for me.. can anyone help me?
The HTML displays properly, I can input the constants, they are updated in the URL. The PHP GET function seems to be working but not producing any results on the page.
<form>
<input type="text" name="num1" placeholder="Constant">
<input type="text" name="num2" placeholder="Constant">
<select name="Operator">
<option>Select Option</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<br>
<br>
<button type="Submit" name="Submit" value="Submit">Calculate</button>
</form>
<p>The answer is: </p>
if (isset($_GET['Submit'])) {
$result1 = $_GET['num1'];
$result2 = $_GET['num2'];
$operator = $_GET['Operator'];
switch ($operator) {
case 'Select Option':
echo "Error";
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;
}}

php calculator - Switch statement issues

Here is my form
<form>
<input type="text" name="num1" placeholder="Digets">
<input type="text" name="num2" placeholder="Digets">
<select name="operator">
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_GET['$submit'])){
$result1 = $_GET['$num1'];
$result2 = $_GET['$num2'];
$operator = $_GET['$operator'];
switch ('$operator'){
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
When i click on the calculate button I don't see an answer and i tried fixing all my bugs but it seems to me as if their is something wrong with the switch.
Try this:-
you are making some mistake while getting values and switch parameter.
like you are geetting value $_GET['$num1'] instead you need to use $_GET['num1']
as you just need to pass variable switch ($operator) instead switch ('$operator') of quotation mark.
<?php
if (isset($_GET['submit'])){
$result1 = $_GET['num1'];
$result2 = $_GET['num2'];
$operator = $_GET['operator'];
switch ($operator){
case 'Add':
$FResult =$result1 + $result2;
echo "Result:"+ $FResult ;
break;
case 'Subtract':
$FResult =$result1 - $result2;
echo "Result:"+ $FResult ;
break;
case 'Multiply':
$FResult =$result1 * $result2;
echo "Result:"+ $FResult ;
break;
case 'Divide':
$FResult =$result1 / $result2;
echo "Result:"+ $FResult ;
break;
default :
echo "wrong";
}
}
?>
The mistake you have made:
1). You are passing variable directly into get element i.e, it should be like $_GET['submit'] instead of $_GET['$submit'];
2). You are passing string instead of variable i.e, switch ('$operator') should be switch ($operator)
check it out where you have made mistake
<form method="GET" action="">
<input type="text" name="num1" placeholder="Digets">
<input type="text" name="num2" placeholder="Digets">
<select name="operator">
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_GET['submit'])){
$result1 = $_GET['num1'];
$result2 = $_GET['num2'];
$operator = $_GET['operator'];
switch ($operator){
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
You were looking for PHP solution, here is one:-
<?php
if (isset($_POST['submit'])){
$result1 = $_POST['num1'];
$result2 = $_POST['num2'];
$operator = $_POST['operator'];
switch($operator){
case "Add":
$res = +$result1 + +$result2;
break;
case "Subtract":
$res = +$result1 - +$result2;
break;
case "Multiply":
$res = +$result1 * +$result2;
break;
case "Divide":
$res = +$result1 / +$result2;
break;
}
}
?>
<form method="post" action="">
<input type="text" name="num1" id="num1" placeholder="Digets">
<input type="text" name="num2" id="num2" placeholder="Digets">
<select name="operator" id="operator">
<option value="Add">Add</option>
<option value="Subtract">Subtract</option>
<option value="Multiply">Multiply</option>
<option value="Divide">Divide</option>
</select>
<input type="submit" name="submit" id="submit" value="Calculate" />
<p>Your answer is : <input name="res" value="<?php echo $res; ?>"></p>
</form>
This is what you were looking for .
<form>
<input type="number" name="num1" placeholder="Digets">
<input type="number" name="num2" placeholder="Digets">
<select name="operator">
<option value="+">Add</option>
<option value="-">Subtract</option>
<option value="*">Multiply</option>
<option value="/">Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_REQUEST['submit'])){
$result1 = $_REQUEST['num1'];
$result2 = $_REQUEST['num2'];
$operator = $_REQUEST['operator'];
$res = $result1.$operator.$result2;
$p = eval('return '.$res.';');
print $p;
}
?>
<form>
<input type="number" name="num1" placeholder="Digets">
<input type="number" name="num2" placeholder="Digets">
<select name="operator">
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_REQUEST['submit'])){
$result1 = $_REQUEST['num1'];
$result2 = $_REQUEST['num2'];
$operator = $_REQUEST['operator'];
switch ($operator){
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
Another way:
<form>
<input type="number" name="num1" placeholder="Digets">
<input type="number" name="num2" placeholder="Digets">
<select name="operator">
<option value="+">Add</option>
<option value="-">Subtract</option>
<option value="*">Multiply</option>
<option value="/">Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_REQUEST['submit'])){
$result1 = $_REQUEST['num1'];
$result2 = $_REQUEST['num2'];
$operator = $_REQUEST['operator'];
$res = $result1.$operator.$result2;
$p = eval('return '.$res.';');
print $p;
}
?>
You should set option value in select tag (operator which you want for calculator) <option value="">.
change you input type="text" into type="number"
<form method="GET">
<input type="number" name="num1">
<input type="number" name="num2">
<select name="operator">
<option value='+'>Add</option>
<option value='-'>Subtract</option>
<option value='*'>Multiply</option>
<option value='/'>Divide</option>
</select>
<button type="submit" value="submit" name="submit">calculate</button>
<p>Answer :</p>
</form>
<?php
if (isset($_GET['submit'])){
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$operator = $_GET['operator'];
switch ($operator){
case "+":
echo $num1 + $num2;
break;
case "-":
echo $num1 - $num2;
break;
case "*":
echo $num1 * $num2;
break;
case "/":
echo $num1 / $num2;
break;
}
}
?>
what you have done is that you are calculating it in the back-end and the form is refreshing the page , but not displaying the result anywhere on the page . Its better to do with a JQuery so that result will come instantly without page refresh . Just check the code below:-
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$("#submit").on('click', function(){
var num1 = $("#num1").val() ;
var num2 = $("#num2").val() ;
var operator = $("#operator").val() ;
var result = 0 ;
switch(operator){
case "Add":
result = +num1 + +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
case "Subtract":
result = +num1 - +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
case "Multiply":
result = +num1 * +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
case "Divide":
result = +num1 / +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
}
});
});
</script>
<div>
<input type="text" name="num1" id="num1" placeholder="Digets">
<input type="text" name="num2" id="num2" placeholder="Digets">
<select name="operator" id="operator">
<option value="Add">Add</option>
<option value="Subtract">Subtract</option>
<option value="Multiply">Multiply</option>
<option value="Divide">Divide</option>
</select>
<button name="submit" id="submit">Check for the answer</button>
<p>Your answer is : <span id="res"></span></p>
</div>
This is running successfully .

How do I display the answer of my PHP and HTML program?

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"];

PHP Calculator: My solutions won't show up I click calculate

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 );

Categories