Simple Calculator that isn't working - php

I want to develop an ez breezy calculator to upload to a page on my site. I have written both the HTML and PHP code to the best of my understanding but still fail to produce an answer to my screen. After I click the "button", I get redirected to a blank screen with no code. When I try it in my site, it takes me back to my home page...every time! Below is all the code...I need help!
<?php
$a=$_POST["'a'"];
$b=$_POST["'b'"];
$c=0;
?>
<meta charset="utf-8">
<html>
<head>
<title>Proposal Sheet Calculator</title>
</head>
<body>
<form method="post" action="/Merchant Calc 1.php">
<h1>How Much Money Are You Losing?</h1>
<p> </p>
<p> </p>
<h2>How many clients walk out your business each month when finding out they don't have the cash or credit to pay?<br/>
<input type="text" name="'a'"><br/></h2>
<h2>What is your average ticket price?<br/>
<input type="text" name="'b'"><br/></h2>
<p> </p>
<h2><input name="calc" type="Submit" value="Show me the numbers" /></h2>
<p> </p>
<p> </p>
<br/>
<p>
<?php
$a = $_POST[‘a’];
$b = $_POST[‘b’];
$c=$a*$b
// should output $a*$b
if(isset($_POST[‘submit’])){
echo "$a * $b";
}
more?<br/>
<input type="text" name="'Y/N'"><br/>
<input name="calc" type="Submit" />
if(isset($_POST[‘submit’]))<br/>
</p>
?>
</form>
</body>
</html>
I don't even get to the "more?" input...it dumps me out before that so I have not done anything below the "more?" lines. And the crazy output of the last if statement...WebMatrix shows no errors.?.?.?.?
Thanks in advance!

<?php
error_reporting(0);
?>
<html>
<head>
<title>Proposal Sheet Calculator</title>
</head>
<body>
<form method="post" action="">
<h1>How Much MONEY Are You Losing?</h1>
<p> </p>
<p> </p>
<h2>How many clients walk out your BUSINESS each month when finding out they don't have the cash or credit to pay?<br/>
<input type="text" name="a"><br/></h2>
<h2>What is your average ticket price?<br/>
<input type="text" name="b"><br/></h2>
<p> </p>
<h2><input name="calc" type="Submit" value="Show me the numbers" /></h2>
<p> </p>
<p> </p>
<br/>
<p>
<?php
$a = $_POST["a"];
$b = $_POST["b"];
$c=$a*$b;
// should output $a*$b
if(isset($_POST["calc"])){
echo $c;
}
?><br/>
<input type="text" name="'Y/N'"><br/>
<input name="calc" type="Submit" />
<br/>
</p>
</form>
</body>
</html>

You have few syntax errors, fix them it will work.
Firstly, add semicolon ; in this line
$c=$a*$b;
^
Next, you are enclosing multiplication operation inside double quotes, so multiplication will now work, it will be considered as string. so remove double quotes.
echo "$a * $b";// will echo number * number
change to
echo $a * $b;// will echo multiplied value.
Next remove action to your form. I mean make action as action=""
Next access either usnf single quotes or double quotes not both.
$a=$_POST["'a'"];
this should be
$a=$_POST["a"]; or
$a=$_POST['a'];
and also
<input type="text" name="'Y/N'"><br/>
this also
<input type="text" name="Y/N"><br/> or
<input type="text" name='Y/N'><br/>

Related

PHP Form Post/Redirect/Get Header Location Error

I have a simple form that I've made to help me learn php. I'm using my local host server to host the php file (form.php). However, when I refresh the page it resubmits the form, I know I can use the post/redirect/get method to negate this problem. Except implementing the header('Location: form.php'); hasn't been working, if you could take a look at the code- and tell me what I'm doing wrong, that would be much appreciated.
Code example i.e without header('Location: form.php');
<?php
if (empty($_POST) === false) {
echo '<pre>', print_r($_POST, true), '</pre>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<form action="form.php" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name"><br>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email"><br>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"></textarea>
</p>
<p>
<input type="submit" value="submit">
</p>
</form>
This is the result...
Screen Shot without header location
then I add:
header('Location: form.php');
And I get this...
To prevent a form from resubmitting on page refresh, two methods are used:
Method 1: Use AJAX + Redirect
Submit your form using AJAX and then redirect to another page using JQuery.
Method 2: Reload the page
Refresh the page using Javascript.
Your code should be like this:
<?php
if (!empty($_POST)) {
echo '<pre>', print_r($_POST, true), '</pre>';
echo '<script type="text/javascript"> location.reload();</script>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<form action="form.php" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name"><br>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email"><br>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"></textarea>
</p>
<p>
<input type="submit" value="submit">
</p>
</form>
</body>
</html>

Passing hidden value to a php variable in input tag

I am new to coding and pardon me if it a silly thing I am missing. I have searched through the forum & did not find an answer that suits my need. I have 2 files: jobs.php & jobprocess.php
Jobs.php goes as
<?php session_start();
include('dbConnect.php');
$q1="abc";
$q2="pqr";
$q3="xyz";
$opportunity=29;
echo "Opportunity is". $opportunity;
?>
<html>
<head>
<div align="center">
<form method="post" method="post" action="jobprocess.php">
<input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
<input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
<input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
<input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">
<ul class="actions">
<li><input type="submit" name="submit" value="I would like to join!! "></li>
</ul>
</form>
</div>
</head>
<body>
</body>
</html>
jobprocess.php goes with the code
<?php session_start();
include('dbConnect.php');
$opportunity = $_GET['opportunity'];
echo "opportunity is " . $opportunity;
?>
Unfortunately, the above code is not defining value="29" for opportunity on 2nd page. Thanks in advance
If you echo anything before the html tag it would effectively make the html invalid. Also, the head of the document MUST not have presentational html elements such as forms, divs etc
<?php
session_start();
include('dbConnect.php');
$q1="abc";
$q2="pqr";
$q3="xyz";
$opportunity=29;
?>
<html>
<head>
<title>must have a title</title>
</head>
<body>
<?php
echo "Opportunity is". $opportunity;
?>
<div align="center">
<form method="post" method="post" action="jobprocess.php">
<input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
<input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
<input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
<input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">
<ul class="actions">
<li><input type="submit" name="submit" value="I would like to join!! "></li>
</ul>
</form>
</div>
</body>
</html>
And because the form is set to POST you should probably check and use the POSTed variable rather than a GET variable
<?php
session_start();
include('dbConnect.php');
$opportunity = $_POST['opportunity'];
echo "opportunity is " . $opportunity;
?>
Surprisingly my answer that suggested using session variable instead of hidden form field was deleted?! I guess session variables are illegal now?
The answer was chosen for the best answer even.

Unable to get jQuery validation to work

I am using php and CodeIgniter. I am a novice at both of these (career VB.Net and C# developer). However, trying to create a basic registration form, I'm having a hard time getting jQuery validation to work.
header.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?><!DOCTYPE html>
<html>
<head>
<?php if(isset($title)) {?>
<title>Discuss Cards - <?php echo $title ?></title>
<?php } else { ?>
<title>Discuss Cards</title>
<?php } ?>
<link rel="stylesheet" href="<?php echo base_url();?>styles/forum.css" type="text/css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<script src="<?php echo base_url();?>js/jquery.validate.min.js"></script>
</head>
<body>
<?php if(isset($title)) {?>
<h1><?php echo $title?></h1>
<?php } ?>
<?php if (isset($page_description)) {?>
<p id="page_description"><?php echo $page_description?></p>
<?php } ?>
create.php
<script>
$("#createaccount").validate();
</script>
<?php $attributes = array('id'=>'createaccount');
echo form_open('user/create_account',$attributes); ?>
<?php echo form_label('Email','txtEmail');?>
<br />
<?php $data = array('type'=>'email','name'=>'txtEmail','id'=>'txtEmail','maxlength'=>'50','minlength'=>'2');
echo form_input($data); ?>
<br /><br />
<?php echo form_label('Username','txtUsername');?>
<br />
<?php $data = array('name'=>'txtUsername','id'=>'txtUsername','maxlength'=>'50','minlength'=>'5');
echo form_input($data); ?>
<br /><br />
<?php echo form_label('Password','pswPassword');?>
<br />
<?php $data = array('name'=>'pswPassword','id'=>'pswPassword');
echo form_password($data); ?>
<br /><br />
<?php echo form_label('Confirm Password','pswConfirmPassword');?>
<br />
<?php $data = array('name'=>'pswConfirmPassword','id'=>'pswConfirmPassword');
echo form_password($data); ?>
<br /><br />
<input type="submit" value="Register" name="Register" />
<?php echo form_close();?>
footer.php
<strong>© 2016</strong>
</body>
</html>
result...
<!DOCTYPE html>
<html>
<head>
<title>Discuss Cards - Create New Account</title>
<link rel="stylesheet" href="http://[::1]/forum/styles/forum.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://[::1]/forum/js/jquery.validate.min.js"></script>
</head>
<body>
<h1>Create New Account</h1>
<script>
$("#createaccount").validate();
</script>
<form action="http://[::1]/forum/index.php/user/create_account" id="createaccount" method="post" accept-charset="utf-8">
<label for="txtEmail">Email</label>
<br>
<input type="email" name="txtEmail" value id="txtEmail" maxlength="50" minlength="2">
<br>
<br>
<label for="txtUsername"></label>
<br>
<input type="text" name="txtUsername" value id="txtUsername" maxlength="50" minlength="5">
<br>
<br>
<label for="pswPassword">Password</label>
<br>
<input type="password" name="pswPassword" value id="pswPassword">
<br>
<br>
<label for="pswConfirmPassword">Confirm Password</label>
<br>
<input type="password" name="pswConfirmPassword" value id="pswConfirmPassword">
<br>
<br>
<input type="submit" value="Register" name="Register">
</form>
<strong>© 2016</strong>
</body>
</html>
Now, the links to the js and css files are correct. I used the example from http://jqueryvalidation.org/documentation/:
<form class="cmxform" id="commentForm" method="get" action="">
<fieldset>
<legend>Please provide your name, email address (won't be published) and a comment</legend>
<p>
<label for="cname">Name (required, at least 2 characters)</label>
<input id="cname" name="name" minlength="2" type="text" required>
</p>
<p>
<label for="cemail">E-Mail (required)</label>
<input id="cemail" type="email" name="email" required>
</p>
<p>
<label for="curl">URL (optional)</label>
<input id="curl" type="url" name="url">
</p>
<p>
<label for="ccomment">Your comment (required)</label>
<textarea id="ccomment" name="comment" required></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit">
</p>
</fieldset>
</form>
<script>
$("#commentForm").validate();
</script>
However, I feel that I'm missing something because it's not working. When I use their demo (http://jqueryvalidation.org/files/demo/), a new label is created under the input area which displays the error. However, when I test my code (Notepadd++, latest version of Google Chrome, WAMPServer 2.5), I only get Chrome validation. Could someone point out to me what I'm doing incorrect? Thanks.
A bit too long for a comment, but not sure if it is the only problem. The first thing I see is that the
<script>
$("#createaccount").validate();
</script>
is located before the form with id="createaccount". This means this javascript code is parsed and run before the element exists inside the DOM.
In order to run this JS when the DOM is loaded, you then need to wrap it into this specific jquery part :
$( document ).ready( function( ) {
$("#createaccount").validate();
}
(And ideally you put all JS at the end of the page, before the closing </body> tag)

How do I multiply values in PHP?

I am trying to find a basic input where user enters one number and the second number and then multiplies it.
I got it to work without the isset function, but now I am trying to echo out the error line when the page first starts up. If you see the input it is named, name and name2 so I call them in PHP.
My original code did not use isset and it worked but I got error before any input. This is my PHP code:
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name'])) && (isset($_POST['name2'])){
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>
You have closed your IF parentheses too soon. The line should be like this:
if (isset($_POST['name']) && isset($_POST['name2'])) {
This is working code you have some extra parenthesis. If you are multiplying integer values from user always use intval function so that you always have integer value. If user enters string or characters it intval will change to zero
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name']) && isset($_POST['name2'])){
$num = intval($_POST['name']);
$num2 = intval($_POST['name2']);
echo $num*$num2;
}
else{
echo '';
}
?>
Try this I think it is helpful to you:
<form method="POST">
<input type="text" name="value1" placeholder="Enter 1st Value" required>
<input type="text" name="multiply" value="*" readonly>
<input type="text" name="value2" placeholder="Enter 2nd Value" required>
<input type="submit" name="submit" value="Calculate">
</form>
<?php
if(isset($_POST['submit'])){
$value1 = $_POST['value1'];
$multiply = $_POST['multiply'];
$value2 = $_POST['value2'];
if($multiply == "*"){
echo $value1*$value2;
}
}
?>
The main problem is paranthesis are not closed properly it is
if(condition1)&& (condition2){
}
it should be
if((condition1)&&(condition2)){
}
you can use single condition for this also as shown in below code
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send" name="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
//if (isset($_POST['name'])) && (isset($_POST['name2'])){ problem is here your paranthesis are not closed properly
if (isset($_POST['send'])){ //use this as this will ensure that your send button is clicked for submitting form
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>

Finding a page created by a php function

I am trying to generate an HTML page using data gathered in a form on another page. I have this tag on the html page with the form:
<html>
<body>
<meta http-equiv="cache-control" content="private" >
<link rel="stylesheet" type="type/css" href="vytran_css.css" />
<head> New Product Introduction </head>
<p> In order to begin the process of introducing a new product, please complete
the following form. Once you are satisfied with your responses to the various
prompts, please click on the submit button at the bottom of the page. If you
would like to start over, click the Reset button. If you have any questions,
Please follow the link that says "Help".
<form action="html_data.php" id=from1 method="post">
Product Name:
<input name="Name" size="20" type="text">
<br><br>
Project Lead Name:
<input name="PLname" size="20" type="text"> <br><br>
Team-members: <br>
<textarea name="Team-members" rows=10 cols=40 type="text"> </textarea> <br><br>
Product Type: <br>
<input name="Product Type" size="20" type="text"> <br><br>
Description: <br>
<textarea name="Description" rows=10 cols=40 type="text"> </textarea>
<br>
<br> <br>
<input value="Submit" type="submit" name="formSubmit">
<input value="Reset" type="reset">
<input value="Help" type="button" onclick="window.location.href='problems.html'">
</form>
</p>
</body>
</html>
and on the html_data.php page I have the following:
<?php
ob_start(); // start trapping output
$name = #$_POST['Name'];
?>
<html>
<body>
<p>
Product Name: <?php echo $Name; ?><br>
Project Lead: <?php echo $PLname; ?><br>
Team Members: <?php echo $Team-members; ?><br>
Description: <?php echo $Description; ?>
</p>
</body>
</html>
<?php
$output = ob_get_contents();
$newfile="output.txt";
$file = fopen ($newfile, "w");
fwrite($file, $output);
fclose ($file);
ob_end_clean();
?>
This should do what I am asking it to do. The form submits with no problem, but I cannot find the page once it submits. Any idea what I need to change?
In your form
Change from
<textarea name="Team-members" rows=10 cols=40 type="text">
to
<textarea name="Team_members" rows=10 cols=40 type="text">
In your html_data.php
Change from
<?php echo $Team-members; ?>
to
<?php echo $Team_members; ?>
There is obviously an issue with the dash between Team and members.
PHP is treating the hyphen as a mathematical operation, being a minus.
I.e.: Team minus members
Then, if you wish to echo the data to screen after submission, add
echo $output;
underneath ob_end_clean();
This worked for me.

Categories