1

I am still a student and new to PHP and web development in general. I wanted to update the $f1 variable via a button but i could not seem to do it. Is there something that I have been missing?

I have tried the following (simplified) code:

<?php

$f1="s";

//user presses a button to change the variable
if(isset($_POST['emp_sig']) && !empty($_POST['emp_sig'])){

$upload_dir1 = "signatures/EmployeeSignatures/";
$file1= $upload_dir1 . mktime() . ".png";
$GLOBALS['f1'] = $file1;
$success = file_put_contents($file1, $data1);
print $success ? $file1 : 'Unable to save the file.';


}


//user now presses another button to echo the updated variable
if(isset($_POST['submit'])){


echo $GLOBALS['f1'];

}

?>

It does not echo an updated variable but it echoes "s". Any help would be greatly appreciated. Thanks.

CBroe
  • 91,630
  • 14
  • 92
  • 150
GenesisTepait
  • 63
  • 1
  • 10
  • 2
    Looks like you are under the impression that $GLOBALS would hold values across different script instances …? Nope, it doesn’t. Most likely you want to look into _sessions_. – CBroe May 28 '18 at 08:32
  • 1
    The most obvious reason is that it doesn't enter the code block where modification happens. – Álvaro González May 28 '18 at 08:33
  • To begin with, no need to do `if(isset($_POST['emp_sig']) && !empty($_POST['emp_sig'])){`, you can only do `if(!empty($_POST['emp_sig'])){` because `empty` test if it's isset + if not null, empty, etc. – Mickaël Leger May 28 '18 at 08:33
  • Actually, not using $GLOBALS and directly updating the $f1 variable was my first solution, it did not solve the problem. Actually, the first if{} block uploads a .png file to my server, and updates the $f1 variable. No problem with uploading, but it fails to update the variable. It really is puzzling me. – GenesisTepait May 28 '18 at 08:48
  • [Why check both isset() and !empty()](https://stackoverflow.com/q/4559925/476) – deceze May 28 '18 at 09:00
  • Here's a link to the manual for [PHP Sessions](http://php.net/manual/en/book.session.php). You need to read it and use them to solve your problem. – Nick May 28 '18 at 09:12

2 Answers2

1

You don't have to use globals, you can directly access the variable.

$f1="s";

//user presses a button to change the variable
if(!empty($_POST['emp_sig'])){
    $upload_dir1 = "signatures/EmployeeSignatures/";
    $file1= $upload_dir1 . mktime() . ".png";
    $f1 = $file1;
    $success = file_put_contents($file1, $data1);
    print $success ? $file1 : 'Unable to save the file.';
}

//user now presses another button to echo the updated variable
if(isset($_POST['submit'])){
    echo $f1;
}
Kishen Nagaraju
  • 2,062
  • 9
  • 17
0

Using $_SESSION solved my problem.

<?php

session_start();

//user presses a button to change the variable
if(!empty($_POST['emp_sig'])){

$upload_dir1 = "signatures/EmployeeSignatures/";
$file1= $upload_dir1 . mktime() . ".png";
$_SESSION["path1"] = $file1;
$success = file_put_contents($file1, $data1);
print $success ? $file1 : 'Unable to save the file.';


}


//user now presses another button to echo the updated variable
if(isset($_POST['submit'])){


echo $f1 = $_SESSION["path1"];

}

?>

Thank you for all those who helped

GenesisTepait
  • 63
  • 1
  • 10