The $GLOBALS do not get the expected data - php

I have the bellow php code:
<form method="get">
<input type="text" name="num1" placeholder="num1">
<input type="text" name="num2" placeholder="num2">
<button type="submit" name="submit" value="func1">Submit</button>
</form>
The result is: <?php
if (isset($GLOBALS['result'])) {
echo $GLOBALS['result']; // there do not get the add result.
}
?>
<?php
$GLOBALS['result'] = 0;
if(isset($_GET['submit']) && $_GET['submit'] == 'func1'){
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$GLOBALS['result'] = $num1 + $num2;
}
?>
The issue is if (isset($GLOBALS['result'])) {...} the code did not walk through.
Where is my issue?
Or how can I use a variables in different php tags?

Try below code
It is working.
<?php
session_start();
?>
<form method="get">
<input type="text" name="num1" placeholder="num1">
<input type="text" name="num2" placeholder="num2">
<button type="submit" name="submit" value="func1">Submit</button>
</form>
<?php
if(isset($_GET['submit']) && $_GET['submit'] == 'func1'){
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$_SESSION['result'] = ($num1 + $num2);
}
?>
The result is: <?php
if (isset($_SESSION['result'])) {
echo $_SESSION['result']; // there do not get the add result.
}
?>

The page gets refreshed after submitting the form.
So the value get reset in GLOBALS.
If you can echo GLOBALS inside the submit condition then you will get the values.
Note : Use SESSION instead of GLOBALS.
<?php
if(isset($_GET['submit']) && $_GET['submit'] == 'func1'){
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$GLOBALS['result'] = $num1 + $num2;
echo $GLOBALS['result'];
}
?>

Related

How can I echo a statement from a form?

I'm a student doing a challenge where I would have to make a Calculator-type form where you can input two values and click an operation. It's a simple concept using only 4 operations.
I was able to make the format of the form: title, input text and buttons. But I can't find a way to take the input information and manipulating the information. My initial goal was to take the two values and adding/subtracting/multiplying/dividing the values and printing the statement above.
For example:
1 + 2 = 3 //statement printed above
First Value: 1
Second Value: 2 //insert values
+ - * / //click operation buttons
Any suggestions?
<!DOCTYPE HTML>
<?php
print_r($_POST);
if ( isset($_POST["+"]) ) {
$sum = $var1 + $var2;
echo "$var1 + $var2 = $sum";
}
elseif( isset($_POST["-"]) ) {
$sum = $var1 - $var2;
echo "$var1 - $var2 = $sum";
}
elseif ( isset($_POST["*"]) ) {
$sum = $var1 * $var2;
echo "$var1 * $var2 = $sum";
}
elseif ( isset($_POST["/"]) ) {
$sum = $var1 / $var2;
echo "$var1 / $var2 = $sum";
}
?>
<html>
<body>
<form action="" method="POST">
First Value: <input type="text" name="First Value"><br><br>
Second Value: <input type="text" name="Second Value"><br><br>
Operations: <button type="submit" name "+">+</button>
<button type="submit" name "-">-</button>
<button type="submit" name "*">*</button>
<button type="submit" name "/">/</button>
</form>
</body>
</html>
This seems to fix the errors I mentioned in comments
<!DOCTYPE HTML>
<html>
<body>
<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST'){
if ( isset($_POST["+"]) ) {
$sum = $_POST['var1'] + $_POST['var2'];
echo "$_POST[var1] + $_POST[var2] = $sum";
}
elseif( isset($_POST["-"]) ) {
$sum = $_POST['var1'] - $_POST['var2'];
echo "$_POST[var1] - $_POST[var2] = $sum";
}
elseif ( isset($_POST["*"]) ) {
$sum = $_POST['var1'] * $_POST['var2'];
echo "$_POST[var1] * $_POST[var2] = $sum";
}
elseif ( isset($_POST["/"]) ) {
$sum = $_POST['var1'] / $_POST['var2'];
echo "$_POST[var1] / $_POST[var2] = $sum";
}
}
?>
<form action="" method="POST">
First Value: <input type="text" name="var1"><br><br>
Second Value: <input type="text" name="var2"><br><br>
Operations: <button type="submit" name="+">+</button>
<button type="submit" name="-">-</button>
<button type="submit" name="*">*</button>
<button type="submit" name="/">/</button>
</form>
</body>
</html>
I don't know if it's the right way to do things but you can use jquery in the script section to do so, let's say you have this form
<form id="target" action="destination.html">
<input type="text" value="Hello there">
<input type="submit" value="Go">
</form>
so on the submit you do the math
$( "#target" ).submit(function( event ) {
//do the math//
document.write(//your answer//);
event.preventDefault();
});
<?php
$first_num = $_POST['first_num'];
$second_num = $_POST['second_num'];
$operator = $_POST['operator'];
$result = '';
if (is_numeric($first_num) && is_numeric($second_num)) {
switch ($operator) {
case "Add":
$result = $first_num + $second_num;
break;
case "Subtract":
$result = $first_num - $second_num;
break;
case "Multiply":
$result = $first_num * $second_num;
break;
case "Divide":
$result = $first_num / $second_num;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="page-wrap">
<h1>Simple Calculator Program</h1>
<form action="" method="post" id="quiz-form">
<p>
<label>First Number</label>
<input type="number" name="first_num" id="first_num" required="required" value="<?php echo $first_num; ?>" />
</p>
<p>
<label>Second Number</label>
<input type="number" name="second_num" id="second_num" required="required" value="<?php echo $second_num; ?>" />
</p>
<p>
<label>Result</label>
<input readonly="readonly" name="result" value="<?php echo $result; ?>">
</p>
<input type="submit" name="operator" value="Add" />
<input type="submit" name="operator" value="Subtract" />
<input type="submit" name="operator" value="Multiply" />
<input type="submit" name="operator" value="Divide" />
</form>
</div>
</body>
</html>

how to make result of multiple choice in side of the answer

i want to make a multiple choice that if we choose wrong answer, then a text 'incorrect' will be displayed in side of the answer that we choose and if we choose right answer, then a text 'correct' will be displayed in side of the answer that we choose. i using radio button. please help.
this is my index.php:
<?php
include 'koneksi.php';
session_start();
$query = mysql_query("select * from t_soal") or die (mysql_error());
//$_SESSION['soal'] = mysql_fetch_array($query);
$_SESSION['soal'] = array();
$_SESSION['no'] = 1;
$_SESSION['score'] = 0;
$_SESSION['option'] = array();
$_SESSION['jawab'] = array();
$i=0;
while($row = mysql_fetch_assoc($query)){
$_SESSION['soal'][] = $row;
$_SESSION['option'][] = array($_SESSION['soal'][$i]['a'], $_SESSION['soal'][$i]['b'], $_SESSION['soal'][$i]['c'], $_SESSION['soal'][$i]['d']);
$i++;
}
if(isset($_SESSION['soal'])){
header("location:test.php");
}
this is test.php:
<?php
session_start();
$soal = $_SESSION['soal'];
$no = $_SESSION['no'];
if(isset($_POST['next'])){
$_SESSION['jawab'][] = $_POST['option'];
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
}
}
if(isset($soal[$no-1])){
?>
<!DOCTYPE html>
<html>
<head>
<title>Latihan Soal</title>
</head>
<body>
Kembali ke soal 1
<form action="" method="POST">
<p>
<?php
echo $no.". "; $_SESSION['no']++;
echo $soal[$no-1]['soal'];
$jawaban = $_SESSION['option'][$no-1];
shuffle($jawaban);
?>
</p>
<?php
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
}
?>
<input type="submit" name="next" value="next">
</form>
</body>
</html>
<?php
}else{
header("location:result.php");
}
?>
Assuming all your script is working already, then:
In PHP:
You will need to assign a new parameter on this script:
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
}
to
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
$_SESSION['correctAnswer'] = $soal[$no-2]['kunci']; // add sthing like this
}
and then, from
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
}
to
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
echo $jawaban[$i] == $_SESSION['correctAnswer'] ? " correct" : " incorrect";
}
In JS: (much easier)
HTML files will looks like this:
<div class="correctAnswer">
<input type="radio" name="theanswer" id="choice1" />
<label for="choice1">Choice 1</label>
</div>
<div class="incorrectAnswer">
<input type="radio" name="theanswer" id="choice2" />
<label for="choice2">Choice 2</label>
</div>
and the JS will looks like this:
$(".correctAnswer").click(function() {
$(".checks").remove();
$(this).append("<span class='checks'> correct</span>");
});
$(".incorrectAnswer").click(function() {
$(".checks").remove();
$(this).append("<span class='checks'> incorrect</span>");
});
If you want a working example, please check this fiddle link here.
Hope it helps !

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>

Why is it when i ad 0 + 0 in my php, I receive undefined variable?

Hi guys I am doing this user calculation form and for some reason when I addition button 0 + 0 it gives me this error Notice: Undefined variable: add in /var/www/html/1018/5356684c357e7ff13ae21450a17d877d/Test-YT/index.php on line 44
other than that, it works good for all other numbers this is my php code
<html>
<body>
<style><?php include "style.css" ?></style>
<form action="index.php" method="POST">
<table border="1">
<td>
<p>insert value one: <input type="text" name="num1"> <br>
<p>insert value two: <input type="text" name="num2"> <br>
</td>
<td>
<input type="submit" name="add" value="Addition">
<input type="submit" name="sub" value="Subtraction">
<input type="submit" name="mult" value="Multiplication">
<input type="submit" name="div" value="Division">
<input type="submit" name="all" value="Display All">
</td>
</table>
</form>
<div id="answers">
<?php
if (isset($_POST['num1']) && ($_POST['num2'])){
$val1 = $_POST['num1'];
$val2 = $_POST['num2'];
$add = $val1+$val2;
$sub = $val1-$val2;
$mult = $val1*$val2;
$div = $val1/$val2;
}
if (isset($_POST['add'])){
echo $add;
}
else{
echo "zero";
}
if (isset($_POST['sub'])){
echo $sub;
}
if (isset($_POST['mult'])){
echo $mult;
}
if (isset($_POST['div'])){
echo $div;
}
if (isset($_POST['all'])){
echo $add . "<br>" . $sub . "<br>" . $mult . "<br>" . $div . "<br>";
}
?>
</div>
</body>
</html>
if (isset($_POST['num1']) && ($_POST['num2'])){
if $_POST['num2'] is 0 this expression calculated to false and variable $add is not initialized, as other variables as well.
edit:
As suggested below, you probably wanted something like:
if (isset($_POST['num1']) && isset($_POST['num2'])) {
But also looks like you tried to prevent division by zero error. As a better solution, you can define all vars at first and also check non-zero value only before division.
$add = $sub = $mult = $div = 0;
if (isset($_POST['num1']) && isset($_POST['num2'])){
$val1 = $_POST['num1'];
$val2 = $_POST['num2'];
$add = $val1+$val2;
$sub = $val1-$val2;
$mult = $val1*$val2;
if ($val2) {
$div = $val1/$val2;
}
}
So you can be sure script will not die even if num1 and num2 were not submitted.

How to count input that have value?

in my code, when we click ok button . It's will echo 3
I want to apply to count only input that have value only
How to do ?
<?php
if(isset($_POST["submit"]))
{
echo count($_POST["to_more"]);
}
?>
<form name="f1" method="post">
<input type="text" name="to_more[]">
<input type="text" name="to_more[]">
<input type="text" name="to_more[]">
<input type="submit" name="submit" value="OK">
</form>
try this
<?php
if(isset($_POST["submit"]))
{
echo count(array_filter($_POST["to_more"]));
}
?>
How about
if(isset($_POST["submit"])){
$count = 0 ;
foreach($_POST["to_more"] as $data){
if($data != '') $count++;
}
if($count > 0)
echo $count;
}
array_filter should help
<?php
$ar = isset($_POST["to_more"]) ? $_POST["to_more"] : array();
$ar = array_filter($ar, function($el){ return !empty($el);});
echo count($ar);
?>
Should be quicker than foreach, btw.
UPD: Oh, seems, NLSaini posted the precise solution, check it.

Categories