The $GLOBALS do not get the expected data

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



The $GLOBALS do not get the expected data



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.


if (isset($GLOBALS['result'])) ...



Where is my issue?
Or how can I use a variables in different php tags?




2 Answers
2



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.

?>





thank you, bro, but there still a issue for me, if the The result is: <?php if (isset($_SESSION['result'])) echo $_SESSION['result']; // there do not get the add result. ?> is before the if(isset($_GET['submit']) && $_GET['submit'] == 'func1') php code, there will not update as my requirement.
– qg_java_17137
Aug 13 at 8:36


The result is: <?php if (isset($_SESSION['result'])) echo $_SESSION['result']; // there do not get the add result. ?>


if(isset($_GET['submit']) && $_GET['submit'] == 'func1')





If you place that block above the submit condition then you will not get desired result as it will then need a page refresh to update your session variable.
– vishal shah
Aug 13 at 8:41





or use header location for same page in that if condition below $_SESSION['result'] = ($num1 + $num2); to get that value
– vishal shah
Aug 13 at 8:45



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'];


?>





I know I can put the php echo code in there, but I want to realize the different php tag place for using the variable, I tried use $_SESSION['result'] to store it, but still can not get it.
– qg_java_17137
Aug 13 at 8:23



$_SESSION['result']






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard