PHP unable to write a variable to XML file - php

I am unable to get my php code to write my $result variable into the XML file. It does write the $number1 variable that I just added for testing but it doesnt want to add the $result. I need this, so that I can make a memory and memory recall button for my calculator. This is for a school project and I have to use XML. Thanks.
<?php
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];;
$operator = $_POST['operator'];
if ($operator == 'subtract'){
subtract();
}
if ($operator == 'add'){
add();
}
if ($operator == 'multiply'){
multiply();
}
if ($operator == 'divide'){
divide();
}
function add() {
global $number1;
global $number2;
$result = $number1 + $number2;
echo $result;
}
function subtract() {
global $number1;
global $number2;
$result = $number1-$number2;
echo $result;
}
function multiply() {
global $number1;
global $number2;
$result = $number1*$number2;
echo $result;
}
function divide() {
global $number1;
global $number2;
$result = $number1/$number2;
echo $result;
}
$handle = fopen('Data.xml', 'w');
fwrite($handle, "<inputs>\n\t<input>\n\t\t<answer>".$result."</answer>\n\t\t<number1>".$number1."</number1>\n\t</input>\n</inputs>");
fclose($handle);
echo "Write to input.xml successfully";
?>
<html>
<center>
<h1 style="color:blue;">Rohit's Math Machine</h1>
<form action="Code.php" method="POST">
Enter Number 1: <input type="text" name="number1" method="POST"><br><br>
<select name="operator" method="POST">
<option value="subtract">subtract</option>
<option value="add">add</option>
<option value="multiply">multiply</option>
<option value="divide">divide</option>
</select><br><br>
Enter Number 2: <input type="text" name="number2" method="POST"><br><br>
<input type="submit" value="Submit">
</form>
</center>
</html>

Related

PHP Currency Converter Not Working

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.

if statement error showing different values

I am newbie to PHP and I am trying to study "if statement" by making calculator and I end up with an error.the result showing is not correct and also please help to suggest how to clear input after each submission.
<html !doctype>
<head>
<title>
</title>
</head>
<body>
<?php
error_reporting();
if(isset($_POST['submit'])) {
$num1 = $_POST['number1'];
$num2 = $_POST['number1'];
$action = $_POST['action1'];
if($action == "addition"){
$add = $num1 + $num2;
echo "your value is";
echo $add;
}
if($action == "subtraction"){
$sub = $num1 - $num2;
echo "your value is";
echo $sub;
}
if($action =="multipilcation"){
$multi = $num1 * $num2;
echo "your value is";
echo $multi;
}
if($action =="division"){
$divi = $num1 / $num2;
echo "your value is";
echo $divi;
}
}
?>
<form method='post' name='myform'>
Enter Number 1:<br>
<input type="number" name="number1" >
<br>
Enter Number 2:<br>
<input type="number" name="number2" >
<br>
choose operation<br>
<select name='action1'>
<option>addition</option>>
<option>subtraction</option>
<option>multipilcation</option>
<option>division</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
</body>
<footer>
</footer>
</html>
You used same number1 for two variables, change it to number2
$num1 = $_POST['number1'];
$num2 = $_POST['number2'];
^
$action = $_POST['action1'];
1) You are not passing any value in <option></option> tag.
<select name='action1'>
<option value="addition">addition</option>>
<option value="subtraction">subtraction</option>
<option value="multipilcation">multipilcation</option>
<option value="division">division</option>
</select>
2) Change $num2 = $_POST['number1']; to $num2 = $_POST['number2'];
<?
if(isset($_POST['submit'])) {
$num1 = $_POST['number1'];
$num2 = $_POST['number2'];
$action = $_POST['action1'];
$num2 = $_POST['number1'];
chnage with:
$num2 = $_POST['number2'];
Also add value attribute for option tag inside select.
<option value="addition">addition</option>>
<option value="subtraction">subtraction</option>
<option value="multipilcation">multipilcation</option>
<option value="division">division</option>

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.

I have the following string in a text file:

I have a code but is not working, I'm trying use explode but still not working.
My code have two strings:
^#^b = (on)
^A^b = (off)
I try to get only 2 characters from a .txt file in a variable like:
$on=^#
$off=^A
then I can use the result to verify the state of light using a dimmer.
Code
<?php
if(isset($_POST['estado'])) {
exec('/var/www/www.rfteste.com/htdocs/estado.sh');
}
if(isset($_POST['ligar'])) {
exec('/var/www/www.rfteste.com/htdocs/liga.sh');
}
if(isset($_POST['desligar'])) {
exec('/var/www/www.rfteste.com/htdocs/desliga.sh');
}
echo "<H3>CONTROL PANEL</H3>";
$str = file_get_contents("/var/www/www.rfteste.com/htdocs/estado.txt");
$vals = explode("^", $str);
$num1 = "^".$vals[0];
$num2 = "^".$vals[1];
$onoff= "^A";
if($num2 == $onoff)
echo "<b>on</b>";
else
echo "<b>off</b>";
?>
<html>
<body>
<form method="post" action="">
<p>
<center><input type="submit" value="Ligar" name="ligar""';" /></center>
<center><input type="submit" value="desligar" name="desligar""';" /></center>
<center><input type="submit" value="atualizar" name="estado""';" /></center>
i think this can help you solve your problem
function checktext($text){
if(preg_match("'#'", $text)){
return 'on';
}else{
return 'off';
}
}
echo checktext('^A^b');
Use php substr()
if(substr($str, 0, 1) == "A")
{
echo "<b>on</b>";
} else {
echo "<b>off</b>";
}

Simple PHP calculator

I'm creating a basic PHP calculator that lets you enter two values and chose your operator then displays the answer. Everything is working fine except it's not outputting the answer to the browser.
Here are the codes for my html and PHP files:
<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<form method="post" attribute="post" action="disp_form.php">
<p>First Value:<br/>
<input type="text" id="first" name="first"></p>
<p>Second Value:<br/>
<input type="text" id="second" name="second"></p>
<input type="radio" name="group1" id="add" value="add" checked="true"><p>+</p><br/>
<input type="radio" name="group1" id="subtract" value="subtract"><p>-</p><br/>
<input type="radio" name="group1" id="times" value="times"><p>x</p><br/>
<input type="radio" name="group1" id="divide" value="divide"><p>/</p><br/>
<p></p>
<button type="submit" name="answer" id="answer" value="answer">Calculate</button>
</form>
</body>
</html>
PHP file:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
if($_POST['group1'] == add) {
echo "$first + $second";
}
else if($_POST['group1'] == subtract) {
echo "$first - $second";
}
else if($_POST['group1'] == times) {
echo "$first * $second";
}
else($_POST['group1'] == divide) {
echo "$first / $second";
}
?>
</p>
</body>
</html>
<?php
$result = "";
class calculator
{
var $a;
var $b;
function checkopration($oprator)
{
switch($oprator)
{
case '+':
return $this->a + $this->b;
break;
case '-':
return $this->a - $this->b;
break;
case '*':
return $this->a * $this->b;
break;
case '/':
return $this->a / $this->b;
break;
default:
return "Sorry No command found";
}
}
function getresult($a, $b, $c)
{
$this->a = $a;
$this->b = $b;
return $this->checkopration($c);
}
}
$cal = new calculator();
if(isset($_POST['submit']))
{
$result = $cal->getresult($_POST['n1'],$_POST['n2'],$_POST['op']);
}
?>
<form method="post">
<table align="center">
<tr>
<td><strong><?php echo $result; ?><strong></td>
</tr>
<tr>
<td>Enter 1st Number</td>
<td><input type="text" name="n1"></td>
</tr>
<tr>
<td>Enter 2nd Number</td>
<td><input type="text" name="n2"></td>
</tr>
<tr>
<td>Select Oprator</td>
<td><select name="op">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value=" = "></td>
</tr>
</table>
</form>
Personally I would do a switch instead of all this if, else if, else
$first = $_POST['first'] + 0;//a small "hack" to make sure its an int but allow negs!!
$second= $_POST['second'] + 0;
$operator = $_POST["group1"];
switch($operator)
{
case "add"
echo "Answer is: " .$first + $second;
break;
case "subtract"
echo "Answer is: " .$first - $second;
break;
case "times"
echo "Answer is: " .$first * $second;
break;
case "divide"
echo "Answer is: " .$first / $second;
break;
}
You need to assign $first and $second
$first = $_POST['first'];
$second= $_POST['second'];
Also, As Travesty3 said, you need to do your arithmetic outside of the quotes:
echo $first + $second;
You also need to put the [== 'add'] math operation into quotes
if($_POST['group1'] == 'add') {
echo $first + $second;
}
complete code schould look like that :
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == 'add') {
echo $first + $second;
}
else if($_POST['group1'] == 'subtract') {
echo $first - $second;
}
else if($_POST['group1'] == 'times') {
echo $first * $second;
}
else if($_POST['group1'] == 'divide') {
echo $first / $second;
}
?>
You need to get the values the same way to get the calculator operation which looks like:
<?php
if($_POST['group1'] == add) {
echo "$_POST['first']+ $_POST['second'];
}
... and so on
?>
Or, to make it easier, just do:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == add) {
echo "$first + $second";
}
else if($_POST['group1'] == subtract) {
echo "$first - $second";
}
else if($_POST['group1'] == times) {
echo "$first * $second";
}
else($_POST['group1'] == divide) {
echo "$first / $second";
}
?>
</p>
</body>
</html>
$first = doubleval($_POST['first']);
$second = doubleval($_POST['second']);
if($_POST['group1'] == 'add') {
echo "$first + $second = ".($first + $second);
}
// etc
Check string using single quotes
Ex. $_POST['group1'] == 'add'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
HTML Code is here:
<form method="post">
<input type="text" name="numb1">
<input type="text" name="numb2">
<select name="operator" id="">
<option>None</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
<option>Square</option>
</select>
<button type="submit" name="submit" value="submit">Calculate</button>
</form>
PHP Code:
<?php
if (isset($_POST['submit'])) {
$result1 = $_POST['numb1'];
$result2 = $_POST['numb2'];
$operator = $_POST['operator'];
switch ($operator) {
case 'None':
echo "You need to select any operator";
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;
case 'Square':
echo $result1 ** $result2;
break;
}
}
?>
enter code here
</body>
</html>
Make this changes in php file
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == 'add') {
echo $first + $second;
}
else if($_POST['group1'] == 'subtract') {
echo $first - $second;
}
else if($_POST['group1'] == 'times') {
echo $first * $second;
}
else if($_POST['group1'] == 'divide') {
echo $first / $second;
}
else {
echo "Enter the numbers properly";
}
?>
</p>
</body>
</html>
<?php
$cal1= $_GET['cal1'];
$cal2= $_GET['cal2'];
$symbol =$_GET['symbol'];
if($symbol == '+')
{
$add = $cal1 + $cal2;
echo "Addition is:".$add;
}
else if($symbol == '-')
{
$subs = $cal1 - $cal2;
echo "Substraction is:".$subs;
}
else if($symbol == '*')
{
$mul = $cal1 * $cal2;
echo "Multiply is:".$mul;
}
else if($symbol == '/')
{
$div = $cal1 / $cal2;
echo "Division is:".$div;
}
else
{
echo "Oops ,something wrong in your code son";
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<form method="GET">
<h1>Calculator</h1>
<p>This is a calculator made by Hau Teen Yee Fabrice and is free to use.</p>
<ul>
<ol> Multiplication: * </ol>
<ol> Substraction: - </ol>
<ol> Division: / </ol>
<ol> Addition: + </ol>
</ul>
<p>Enter first number:</p>
<input type="text" name="cal1">
<p>Enter second number:</p>
<input type="text" name="cal2">
<p>Enter the calculator symbol</p>
<input type="text" name="symbol">
<button>Calculate</button>
</form>
<?php
$cal1= $_GET['cal1'];
$cal2= $_GET['cal2'];
$symbol =$_GET['symbol'];
if($symbol == '+')
{
$add = $cal1 + $cal2;
echo "Addition is:".$add;
}
else if($symbol == '-')
{
$subs = $cal1 - $cal2;
echo "Substraction is:".$subs;
}
else if($symbol == '*')
{
$mul = $cal1 * $cal2;
echo "Multiply is:".$mul;
}
else if($symbol == '/')
{
$div = $cal1 / $cal2;
echo "Division is:".$div;
}
else
{
echo "Oops ,something wrong in your code son";
}
?>
</body>
</html>

Categories