This form takes in data only in the "inp" field. To multiply two numbers you'd have to input the first number then hit "=" to send the number to the "out" field then type in the second number and hit "*". I am attempting to post the results($result) of calculations into the "out" field and not sure how to do this using php. Any help would be greatly appreciated.
php:
$input = $_POST['inp'];
$output= $_POST['out'];
if($_POST['submit'] == 'add') {
$result = $input + $output;
//set result to "out"
}
else if($_POST['submit'] == 'sub') {
$result = $input - $output;
//set result to "out"
}
else if($_POST['submit'] == 'mul') {
$result = $input * $output;
//set result to "out"
}
else if($_POST['submit'] == 'div') {
$result = $input / $output;
//set result to "out"
}
else if($_POST['submit'] == 'equ') {
$result = $input;
//set result to "out"
}
form:
<body>
<form action = "calc.php" method = "post">
<input type="text" value="0.0" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>
If you php and html code in the same file you can write simple write to out input value calculated result. Try something like this:
<body>
<form action = "calc.php" method = "post">
<input type="text"
value="<?=!empty($result)?$result:"0.0"?>" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>
I modified your code.
$result= '';
if ($_POST) {
$input = (int) $_POST['inp'];
$output= (int) $_POST['out'];
if(isset($_POST['add']) && !empty($input)) {
$result = $output + $input;
//set result to "out"
}
else if(isset($_POST['sub']) && !empty($input)) {
$result = $output - $input;
//set result to "out"
}
else if(isset($_POST['mul']) && !empty($input)) {
$result = $output * $input;
//set result to "out"
}
else if(isset($_POST['div']) && !empty($input)) {
$result = $output / $input;
//set result to "out"
}
else if(isset($_POST['equ']) && !empty($input)) {
$result = $input;
//set result to "out"
} else {
$error = "Please enter a number";
}
}
<body>
<form action="cal.php" method = "post">
<input type="text" value="<?=!empty($result)?$result:"0.0"?>" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
<p><?=isset($error)?$error:''?></p>
Related
i made a form where you can put a number and show which one is the highest.
i want to make sure you can put any words in this form.
that there show a notification you cant put words in the form
<?php
function maxGetal($getal1, $getal2)
{
if ($getal1 > $getal2) {
return ($getal1);
} elseif ($getal2 > $getal1) {
return ($getal2);
} else {
return ("gelijk");
}
}
?>
<form action="" method="post">
<input type="text" name="eerstegetal" placeholder="Eerste getal"><br>
<input type="text" name="tweedegetal" placeholder="Tweede getal"><br>
<input type="submit" name="submit" value="Bereken hoogste getal">
<p> -----------------------------------------------------------------------</p>
</form>
<?php
if (isset($_POST["submit"])) {
$maxgetal = maxGetal($_POST["eerstegetal"], $_POST["tweedegetal"]);
echo $maxgetal;
}
$input1 = doubleval($_POST["eerstegetal"]);
$input2 = doubleval($_POST["tweedegetal"]);
?>
The following code shows a notification when a letter is typed.
<?php
function maxGetal($getal1, $getal2)
{
if ($getal1 > $getal2) {
return ($getal1);
} elseif ($getal2 > $getal1) {
return ($getal2);
} else {
return ("gelijk");
}
}
?>
<form action="" method="post">
<input type="text" id="eerstegetal" name="eerstegetal" oninput="Eerste_check()" placeholder="Eerste getal"><br>
<input type="text" id="tweedegetal" name="tweedegetal" oninput="Tweede_check()" placeholder="Tweede getal"><br>
<input type="submit" name="submit" value="Bereken hoogste getal">
<p> -----------------------------------------------------------------------</p>
</form>
<?php
if (isset($_POST["submit"])) {
$maxgetal = maxGetal($_POST["eerstegetal"], $_POST["tweedegetal"]);
echo $maxgetal;
}
$input1 = doubleval($_POST["eerstegetal"]);
$input2 = doubleval($_POST["tweedegetal"]);
?>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script>
function Eerste_check(){
var eerstegetal = $('#eerstegetal').val()
var pattern = /^\d+\.?\d*$/;
if(!pattern.test(eerstegetal)){
alert('Eerstegetal should contain only numbers.')
}
}
function Tweede_check(){
var tweedegetal = $('#eerstegetal').val()
var pattern = /^\d+\.?\d*$/;
if(!pattern.test(tweedegetal)){
alert('Tweedegetal should contain only numbers.')
}
}
</script>
Try the following:
<input type="number" name="eerstegetal" placeholder="Eerste getal"><br>
<input type="number" name="tweedegetal" placeholder="Tweede getal"><br>
I hope it helps
I am trying to create a simple tax calcualtor and need to validate the both textfields so they are not empty and it doesn't appear to be working any tips?
if(isset($_GET['taxSubmit'])) {
$afterPrice = $_GET['taxPrice'];
$taxRate = $_GET['taxRate'];
$beforeTax = ($afterPrice * 100) / ($taxRate + 100);
if(!empty($_GET['taxPrice'.'taxRate'])){
echo "<h1>Price before tax = £".$beforeTax."<h1>";
} else {
echo 'All Fields Required';
}
}
Somebody asked for the markup so here it is:
<form method="get" action="watWk5.php">
<fieldset>
<legend>Without tax calculator</legend>
<label for="">After Tax Price</label>
<input type="text" name="taxPrice"/ value=<?php
if(isset($_GET['taxPrice'])){
echo $_GET['taxPrice'];
}
?>>
<label for="">Tax Rate</label>
<input type="text" name="taxRate"/ value=<?php
if(isset($_GET['taxRate'])){
echo $_GET['taxRate'];
}
?>>
<input type="submit" value="Submit" name="taxSubmit"/>
<button type="reset" value="Reset" name="clrButton">Clear</button>
</fieldset>
Each variable should have its own !empty() check
if(!empty($_GET['taxPrice']) && !empty($_GET['taxRate'])) {
//proceed
} else {
//throw error
}
Like in topic. How can I call the overrider controller from html form like this below:
<form method="post" action="index.php?controller=CmsController">
<input name="test" type="text" />
<input name="action" value="getStatus" type="hidden" />
<input value="test" type="submit" />
</form>
This code above I have in my tpl file.
File: /override/controllers/front/CmsController.php
---edit
{$link->getModuleLink('blockcms', 'CmsController')|escape:'html'}
doesn’t work.
---edit
public function initContent() {
parent::initContent();
if (Tools::getValue('action') && Tools::getValue('action') == 'getStatus') {
$id = $this->getIdByCode(Tools::getValue('voucher'));
$obj = new CartRuleCore($id);
$context = Context::getContext();
$response = $obj->checkValidity($context);
if ($response == NULL or $response == 'Koszyk jest pusty'){
$response = 'Kod ';
}
}
if (isset($this->cms)) {
$this->cms->content = str_replace("<!--response-->", $response, "{$this->cms->content}");
}
$this->setTemplate(_PS_THEME_DIR_ . 'cms.tpl');
This is part of initContent function in \override\controllers\front\CmsController.php And in my file tpl I have
<form method="post" action="index.php?controller=CmsController">
<input name="test" type="text" />
<input name="action" value="getStatus" type="hidden" />
<input value="test" type="submit" />
I'm having issues trying to output data which a user has inputted into a form.. It's a 2 block form, I'm just trying to save the data into 2 variables and then echo the variables. But I don't understand where I'm going wrong. Any help appreciated.
<?php
echo $problem = "";
if(isset($_POST['submit']) && $_POST['submit']=="submit"){
if(!empty($_POST['eWeight']) && $_POST['eWeight']!=''){
$eWeight = mysqli_real_escape_string($conn, trim($_POST['eWeight']));
} else {
$problem .= "Please enter a weight. <br/>";
}
if(!empty($_POST['gym']) && $_POST['gym']!=''){
$gym = mysqli_real_escape_string($conn, trim($_POST['gym']));
} else {
$problem .= "Please enter time at gym. <br/>";
}
echo $eWeight, $gym;
}
?>
<?php
if($conn){
echo "connected";
}
echo $problem;
echo $eWeight;
?>
<form class="pure-form pure-form-stacked" name="contact_weight">
<label for="eWeight">Enter Weight: </label> <input type="number" id="eWeight" name="eWeight" placeholder="88" required/>
<label for="gym">Enter Time at Gym: </label> <input type="number" id="gym" name="gym" placeholder="60" required/>
<button class="submit" type="submit">Submit Form</button>
</form>
You can do something like this
<?php
echo $problem = "";
if(isset($_POST['submit'])){
if(!empty($_POST['eWeight']) && $_POST['eWeight']!=''){
$eWeight = mysqli_real_escape_string($conn, trim($_POST['eWeight']));
} else {
$problem .= "Please enter a weight. <br/>";
}
if(!empty($_POST['gym']) && $_POST['gym']!=''){
$gym = mysqli_real_escape_string($conn, trim($_POST['gym']));
} else {
$problem .= "Please enter time at gym. <br/>";
}
echo $eWeight, $gym;
}
if($conn){
echo "connected";
}
echo $problem;
echo $eWeight;
?>
<form class="pure-form pure-form-stacked" name="contact_weight" action="" method="post">
<label for="eWeight">Enter Weight: </label> <input type="number" id="eWeight" name="eWeight" placeholder="88" required/>
<label for="gym">Enter Time at Gym: </label> <input type="number" id="gym" name="gym" placeholder="60" required/>
<button name="submit" class="submit" type="submit">Submit Form</button>
</form>
Can someone explain or show me why my form is only posting in 1 column when it should be posting in 2 columns for example.
Here is my form
<form action="{$baseurl}/redirect" method="post" enctype="multipart/form-data" id="details_change">
<textarea name="about_me" cols="53" rows="5" class="submit_form_textfield">{$profile_user.about_me}</textarea>
<input type="text" name="profile_message" />
<input type="hidden" name="action" value="user_details" />
<input type="submit" class="submit_form_button" value="Update Details" id="details_change">
</form>
And here is my PHP
if (isset($action) && $action=='user_details' && isset($_SESSION['loggeduser_id'])) {
$user = new User();
if (isset($_POST['about_me']) && isset($_POST['about_me'])) {
$_POST['$about_me'] = preg_replace("/[^a-z]/i","",$_POST['about_me']);
} else {
$about_me = '';
}
if (isset($_POST['profile_message']) && isset($_POST['profile_message'])) {
$_POST['$profile_message'] = preg_replace("/[^a-z]/i","",$_POST['profile_message']);
} else {
$profile_message = '';
}
$user->update($_SESSION['loggeduser_id'],array("about_me" => $about_me,"profile_message" => $profile_message));
}
And I have columns in my user table called about_me and profile_message
And what happens is it will only post about_me and not profile_message any reason why?
You need to use $_POST['user_details'] and $_POST['profile_message']
In HTML:
<form action="{$baseurl}/redirect" method="post" enctype="multipart/form-data" id="details_change">
<textarea name="about_me" cols="53" rows="5" class="submit_form_textfield">{$profile_user.about_me}</textarea>
<input type="text" name="profile_message" />
<input type="hidden" name="user_details" value="" />
<input type="submit" class="submit_form_button" value="Update Details" name="action" >
</form>
PHP:
if (isset($_POST['action']) && isset($_SESSION['loggeduser_id'])) {
$user = new User();
if (isset($_POST['profile_message'])) {
$profile_message = preg_replace("/[^a-z]/i","",$_POST['profile_message']);
} else {
$profile_message = '';
}
if (isset($_POST['user_details']) && isset($_POST['user_details'])) {
$user_details = preg_replace("/[^a-z]/i","",$_POST['user_details']);
} else {
$user_details = '';
}
$about_me = $_POST['about_me'];
$user->update($_SESSION['loggeduser_id'],array("about_me" => $about_me,"profile_message" => $profile_message));
}