PHP Currency Converter Not Working - php

To any Advanced PHP users out there I am a Begginer. So, I have this PHP currency converter that I am currently working on:
<!DOCTYPE html>
<html>
<head>
<title>Currency Converter</title>
</head>
<body>
<h1>Currency Converter</h1>
<form action="index.php" method="get">
<input type="text" name="input" placeholder="Enter Amount"></input>
<select name="dropdown">
<option value="USD">US Dollar (USD)</option>
<option value="GDP">Great British Pound (GDP)</option>
<option value="EUR">Euro (EUR)</option>
</select>
<br />
<input type="text" value="<?php echo #$output; ?>" disabled>
<select name="odropdown">
<option value="USD">US Dollar (USD)</option>
<option value="GDP" selected>Great British Pound (GDP)</option>
<option value="EUR">Euro (EUR)</option>
</select>
<br />
<input type="submit" name="sbmt" value="Convert!"></input>
</form>
</body>
</html>
<?php
$cc_input = $_GET['input'];
$cc_dropdown = $_GET['dropdown'];
$cc_odropdown = $_GET['odropdown'];
if(isset($_GET['smbt']))
{
if($cc_dropdown == 'USD') {
if($cc_odropdown == 'USD') {
$output = $cc_input * 1;
echo $output;
} elseif($cc_odropdown == 'EUR') {
$output = $cc_input * 0.897748;
echo $output;
} elseif($cc_odropdown == 'GDP') {
$output = $cc_input * 0.753608;
echo $output;
}
}
elseif($cc_dropdown == 'EUR') {
if($cc_odropdown == 'USD') {
$output = $cc_input * 1.11390;
echo $output;
} elseif($cc_odropdown == 'EUR') {
$output = $cc_input * 1;
echo $output;
} elseif($cc_odropdown == 'GDP') {
$output = $cc_input * 0.83944;
echo $output;
}
}
elseif($cc_dropdown == 'GDP') {
if($cc_odropdown == 'USD') {
$output = $cc_input * 1.32695;
echo $output;
} elseif($cc_odropdown == 'EUR') {
$output = $cc_input * 1.19127;
echo $output;
} elseif($cc_odropdown == 'GDP') {
$output = $cc_input * 1;
echo $output;
}
}
}
?>
When I Try it and Click on Convert! nothing happens why? Also there are no errors in the php built in developer server. it just says /index.php?input=1&dropdown=USD&odropdown=GDP&sbmt=Convert%21 in green.

Related

How can I show the output vertically after submitting the html form?

The who;e sequence(having operands , opertors and answer) in output is generating randomly which is perfect. but I want to show that in vertical manner
and this is the output I want to show after clicking on generate
then Q(2)will show up in the same way but after Q(1).
this is my html form.
<form action="" method="POST">
<div class="row">
<div class="col-25">
<label for="qnum">Select no.of questions:</label>
</div>
<div class = "col-75"><input type="number" id="qnum" name="que" value="1" min="1" max="100"></div>
<br /><br />
</div>
<br />
<div class="row">
<div class="col-25">
<label for="int">How many numbers You want in a sequence:</label></div>
<div class="col-75">
<select name="select" id="int">
<option value="2"> 2 </option>
<option value="3"> 3 </option>
<option value="4"> 4 </option>
<option value="5"> 5 </option>
<option value="6"> 6 </option>
<option value="7"> 7 </option>
<option value="8"> 8 </option>
<option value="9"> 9 </option>
<option value="10"> 10 </option>
</select>
</div>
</div>
<br />
<div class="row">
<div class="col-25">
<label for="dig">Select no. of digits:</label></div>
<div class="col-75">
<select name="digits" id="dig">
<option value="1"> 1 digit</option>
</select>
</div>
<br /><br /><br /><br />
</div>
<div class="row">
<div class="col-25"><label>Select operations:</label><br /></div>
<div class="col-75">
<input type="checkbox" id="mix" value="all" class="toggle-button"><label>All</label>
<input type="checkbox" id="add" name="operation[]" value="Addition" checked><label>Addition</label>
<input type="checkbox" id="sub" name="operation[]" value="Subtraction"><label>Subtraction</label>
<input type="checkbox" id="mult" name="operation[]" value="Multiplication"><label>Multiplication</label>
<input type="checkbox" id="div" name="operation[]" value="Division"><label>Division</label>
</div>
<br /><br />
<br /><br />
</div><br />
<input type="submit" name="submit" value="Generate"><br>
<br />
</form>
</div>
<!---Toggle button to select all operations if user select checkbox named "All" -->
<script language="JavaScript">
$( '.col-75 .toggle-button' ).click( function () {
$( '.col-75 input[type="checkbox"]' ).prop('checked', this.checked)
})
</script>
Basically I generating a random sequence of arithmetic expressions which is working perfectly without any error.
But after clicking generate button the output is showing horizontally and I want to generate it vertically.
So how can I do that?
Below is my php code which is generating the sequence perfectly without any error. How can I generate the output vertically?
<?php
error_reporting(E_ALL & ~E_NOTICE);
if(isset($_POST['submit']))
{
//validation for no of operation selected
$operation = $_POST['operation'];
if(empty($operation))
{
echo "Please Select at least one operation <br/>";
}
else
{
$N = count($operation); //count selected operation
echo "<br />";
echo "You have selected $N operation(s):";
for($i=0; $i < $N; $i++) //for loop for for operator array.
{
echo($operation[$i] . ", "); //display selected operation
}
echo "<br />";
//checkbox operations
if($N==1) //if only one operation is selected
{
if($operation[0]=='Addition')
{
$rOperators = array('+');
}
else if($operation[0]=='Subtraction')
{
$rOperators = array('-');
}
else if($operation[0]=='Multiplication')
{
$rOperators = array('*');
}
else
{
$rOperators = array('/');
}
}
else if ($N==2) //if only two operations are selected by the user
{
if($operation[0]=='Addition')
{
if($operation[1]=='Subtraction')
{
$rOperators = array('+','-');
}
else if($operation[1]=='Multiplication')
{
$rOperators = array('+','*');
}
else
{
$rOperators = array('+','/');
}
}
else if($operation[0]=='Subtraction')
{
if($operation[1]=='Multiplication')
{
$rOperators = array('-','*');
}
else
{
$rOperators = array('-','/');
}
}
else
{
$rOperators = array('*','/');
}
}
else if ($N==3) //if 3 operators gets selected by user
{
if($operation[0]=='Addition')
{
if($operation[1]=='Subtraction')
{
if($operation[2]=='Multiplication')
{
$rOperators = array('+','-','*');
}
else
{
$rOperators = array('+','-','/');
}
}
else
{
$rOperators = array('+','*','/');
}
}
else
{
$rOperators = array('-','*','/');
}
}
else
{
$rOperators = array('+','-','*','/');
}
}
//display sequence having only single digit numbers
if($_POST['digits'] == 1)
{
$q = $_POST['que'];
$previousInt = null; //Track the previous operand
$s = $_POST['select'];
for ($x = 1; $x<=$q; $x++) //for loop for no. of questions
{
$randomOperands = array();
$randomOperators = array();
// loop over $i n times
for ($i = 1; $i <=$s; $i++) //for loop for no. of integers user entered
{
$nextInt = rand(1, 9); // assign random operand to array slot
$randomOperands[] = $nextInt;
if($i < $s)
{
if($previousInt === 0) //No operator after last opearnd
{
//avoid division-by-zero
$randomOperators[] = $rOperators[rand(0, 2)];
}
else
{
$randomOperators[] = $rOperators[rand(0, $N-1)];
}
}
$previousInt = $nextInt;
}
// print array values
$exp = ''; //Generating the expression
foreach($randomOperands as $key=>$value1)
{
$exp .= $value1 . " " ;
if(isset($randomOperators[$key]))
{
$exp .= $randomOperators[$key] ." ";
}
}
$res = eval("return ($exp);");//evaluate the expression
//print expression
echo ("This is Q(".$x."):"), $exp."=". $res."<br>";
}
}
}
?>
Modify the code in which you are generating the expression which you are using as the input to the eval() function. Create another string say $output_string which can be used to output your answer. In this newly created string use proper HTML tags along with the generated string, for formatting it the way you want your output to look like.
It should look something like this:
$exp = ''; //Generating the expression
$output_string = ''; // new string which can be used for generating the output string
foreach ( $randomOperands as $key => $value1 ) {
$exp .= $value1 . " " ;
$output_string .= $value1 . " <br>" ;
if ( isset($randomOperators[$key]) ) {
$exp .= $randomOperators[$key] ." ";
$output_string .= $randomOperators[$key] ." <br> ";
}
}
$res = eval("return ($exp);");//evaluate the expression
//print expression
echo ("This is Q(".$x."): <br>"), $output_string."= <br>". $res."<br>";

Only ahow error message when error occurs

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.

if elseif php simple calculator

This is form for calculator
<form method='post' action='result.php' name='calc_form'>
<input type='text' name='input1' size='15'>
<select name='operation'>
<option value="plus">+</option>
<option value="minus">-</option>
<option value="multi">*</option>
<option value="invalid">**</option>
<option value="divide">/</option>
</select>
<input type='text' name='input2' size='15'>
<input type='submit' value='go'>
</form>
This is php with if elseif statement
<?php
define ('INVALID_INPUT', 'ERROR: invalid input');
define ('INVALID_OPERATOR', 'ERROR: invalid operator');
$result = null;
if
(isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
{
$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
$operation = $_POST['operation'];
switch ($operation)
{
case 'plus':
$result = $input1 + $input2;
break;
case 'minus':
$result = $input1 - $input2;
break;
case 'multi':
$result = $input1 * $input2;
break;
case 'divide':
$result = $input1 / $input2;
break;
default:
$result = INVALID_OPERATOR;
break;
}
}
elseif
(!isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
{
$result = INVALID_INPUT;
}
if ($result !== null)
{
echo <<<EOM
<h2>you calculate</h2> $input1
<h2>and</h2> $input2
<h2>result is:</h2>
$result
EOM;
}
?>
I even tried with only else statement and without condition but it wont show INVALID INPUT when nothing is added in form. What is wrong here?
The problem is with this piece of code:
if
(isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
Even if a user enters nothing in the input fields, they will still pass isset($_POST['input1']), since they are 'set' to an empty string. Try switching it to this instead:
if
(isset($_POST['input1']) && strlen($_POST['input1']) &&
isset($_POST['input2']) && strlen($_POST['input2']) &&
isset($_POST['operation']))
You could just use an else statement instead of an else if statement. That way if you change the inputs you don't have to list each of them twice.
eg:
if (isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
{
// result = calculation
} else
{
// result == code
}
Try this - I tested this on my server and it works!
calculator.php
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors',1);;
define ('INVALID_INPUT', 'ERROR: invalid input');
define ('INVALID_OPERATOR', 'ERROR: invalid operator');
$input1 = $input2 = $operation = null;
if (isset($_POST['input1']) and isset($_POST['input2']) and isset($_POST['operation']))
{
$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
$operation = $_POST['operation'];
if(is_numeric($input1) and is_numeric($input2))
{
switch ($operation)
{
case 'plus':
$result = $input1 + $input2;
break;
case 'minus':
$result = $input1 - $input2;
break;
case 'multi':
$result = $input1 * $input2;
break;
case 'divide':
$result = $input1 / $input2;
break;
default:
$result = INVALID_OPERATOR;
break;
}
}
else
{
$input1 = '';
$input2 = '';
$operation = 'invalid';
$result = INVALID_INPUT;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body class="">
<form method='POST' action='calculator.php'>
<input type='text' name='input1' size='15' value="<?php echo $input1; ?>">
<select name='operation'>
<option <?php if($operation == 'plus') { echo 'selected';} ?> value="plus">+</option>
<option <?php if($operation == 'minus') { echo 'selected';} ?> value="minus">-</option>
<option <?php if($operation == 'multi') { echo 'selected';} ?> value="multi">*</option>
<option <?php if($operation == 'invalid') { echo 'selected';} ?> value="invalid">**</option>
<option <?php if($operation == 'divide') { echo 'selected';} ?> value="divide">/</option>
</select>
<input type='text' name='input2' size='15' value="<?php echo $input2; ?>">
<span>=</span>
<input type='text' name='result' size='50' value="<?php echo $result; ?>">
<input type='submit' value='go'>
</body>
</html>
Your condition is wrong; this is impossible:
!isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation'])
I suspect you mean
!isset($_POST) or
!isset($_POST['input1']) or
!isset($_POST['input2']) or
!isset($_POST['operation'])

My $_POST doesn't get my select name when there is no value

I am doing a form in PHP where it checks if you inserted your choises, but having problems with multiple choices and arrays, if the Value is empty, the Name isn't even in the $_POST array for me to deal with, unlike other types of input.
<!doctype html>
<html>
<head>
<title>Form</title>
</head>
<body>
<?php
$error = 0;
$stored = "";
if(isset($_POST['submit'])){
foreach ($_POST as $key => $value){
if ($key == 'options'){
if (empty($value)){
$error = 1;
}else {
foreach ($value as $el) {
$stored .= $el;
}
}
} else {
if (empty($value)){
$error = 1;
}else {
$stored .= $value;
}
}
}
if (empty($error)){
echo $stored . "Sent.";
} else{
echo 'Error';
}
}
?>
<form action="form.php" method="post">
<input name="email" type="email" maxlength="100" class="<?php if ($error == 1 && empty($email)) echo 'borderred';?>"/>
<select name="options[]" size="3" multiple="multiple" class="<?php if ($error == 1 && empty($options)) echo 'borderred';?>">
<option value="one">Option 1</option>
<option value="two">Option 2</option>
<option value="three">Option 3</option>
<option value="four">Option 4</option>
<option value="five">Option 5</option>
</select>
<input type="submit" name="submit" value="Send"/>
</form>
</body>
</html>
</code>
Check with:
if ( isset($_POST['name']) ) {
// Do stuff
} else {
// Do other stuff
}

PHP multi select quote script

I did something like this :
devis2.php
<script type="text/javascript" src="quote_validation2.js"></script>
<?php
$ddcount = 1;
foreach ($_POST as $key => $value) {
$$key = $value;
}
If (isset($_POST['garment_type']) && $ddcount == 1) {
$post_to = "devis2-resultats.php";
$ddcount = $ddcount + 1;
} Else {
$post_to = "devis2.php";
}
?>
<div style="float:right; display: block;"><img style="border: none;" border="0" src="images/quote-again.gif" width="120" height="16" /></div><br/><br/>
<form name="instantquote" onSubmit="return validator(this)" method="post" action="<?php echo $post_to; ?>">
Au bac:<br/><select multiple size="3" name="garment_type" class="quoteFields" tabindex="1">
<?php
If (isset($_POST['garment_type'])) {
echo "<option value=\"$garment_type\" selected>$garment_type</option> \n";
} Else {
echo "<option value=\"0\" selected>Choisir un soin</option> \n";
}
?>
<option value="soin1">soin1</option>
<option value="soin2">soin2</option>
</select><br/>Longueur des cheveux*:<br/>
<select name="colors_back" class="quoteFields" tabindex="5" size="1" onChange="Javascript:document.instantquote.submit()" >
<?php
If (isset($_POST['colors_back'])) {
echo "<option value=\"$colors_back\" selected>$colors_back</option> \n";
} Else {
echo "<option value=\"0\" selected>Choisir une longueur</option> \n";
}
?>
<option value="+ de 25 cm">+ de 25 cm</option>
<option value="- de 25 cm">- de 25 cm</option>
</select>
<input type="hidden" name="ddcount" value="<?php echo $ddcount; ?>">
<br/><br/><input type="submit" value="Obtenir votre devis" name="quote" class="quoteSubmit" tabindex="20">
</form>
devis2-resultats.php
<?php
foreach ($_POST as $key => $value) {
$$key = $value;
}
include("quote-prices2.php");
?>
<br/><br/><b>Au bac:</b> <?php echo $garment_type; ?><br/><br/>
<?php
If (!isset($_POST['ind_numbers'])) {
} Else {
?>
Individual Numbers:<?php echo $ind_numbers; ?><br/><br/>
<?php
}
If (!isset($_POST['ind_names'])) {
} Else {
?>
Individual Names: <?php echo $ind_names; ?><br/><br/>
<?php
}
If (!empty($email)) { ?>
<br/><br/><b>Envoyé à:</b> <?php echo $email; ?><br/><br/>
<?php
}
?>
<br/><br/><div style="font-size:16px; background-color:#fff; color:#bf3d71;"> <b>Total:</b> <?php $dectotal = sprintf('%0.2f', $total); echo $dectotal; ?> $</div><br/><br/><br>
quote-prices2.php
<?php
// INCOMING VARIABLES FOR PROCESSING: garment_type, garment_color, quantity, colors_front, colors_back, ind_numbers, ind_names
// GARMENT TYPE PRICE
$flash_charge = 0;
switch ($garment_type) {
case "soin1":
$price_garment_type = 6.00;
break;
case "soin2":
$price_garment_typea = 7.00;
break;
}
if ($_POST['garment_type'] === 'soin2') {
switch ($colors_back) {
case "+ de 25 cm":
$price_garment_typea = $price_garment_typea + 50;
break;
}}
$total_garment_pricing = $price_garment_type + $price_garment_typea;
$total_additionals = $total_garment_pricing;
$total = $total_garment_pricing + $total_additionals;
$per_garment_price = $total/2;
$total = $total/2;
?>
**quote_validation2.js**
function validator(form) {
if (form.colors_back.value == 0) {
alert("Vous devez sélectionner une longueur de cheveux.");
return false;
}
return true;
}
function CalcKeyCode(aChar) {
var character = aChar.substring(0,1);
var code = aChar.charCodeAt(0);
return code;
}
function checkNumber(val) {
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
var cCode = CalcKeyCode(lchar);
/* Check if the keyed in character is a number
do you want alphabetic UPPERCASE only ?
or lower case only just check their respective
codes and replace the 48 and 57 */
if (cCode < 48 || cCode > 57 ) {
var myNumber = val.value.substring(0, (strLength) - 1);
val.value = myNumber;
}
return false;
}
I'am very bad at php developement. At this point if I select soin1 & soin2 it display one price only. soin1 = 6 & soin2 = 7 if they are both selected I'd like to obtain 13 as a result. And of course if soin1,soin2 & + de 25 cm are selected I'd like to obtain 63 as a result. The question is how can I make multi select work to obtain the addition of soin1 & soin2. See the script here.

Categories