I'm trying to cause a variable $btn1Pressed
to be set via a URL load. For example, loading http://mywebsite.com/myphp.php?btn1Pressed=1
would set the variable to 1
. The below test code doesn't seem to be doing anything:
<?php
if ($btn1Pressed == 1) {
echo 'Button One Pressed';
}
else{
echo 'Button Two Pressed';
}
?>
Before the vampires arrive...
<?php
if (isset($_GET['btn1Pressed']) && $_GET['btn1Pressed'] == 1) {
echo 'Button One Pressed';
}
else{
echo 'Button Two Pressed';
}
?>
Anything in the query string will be in PHP's $_GET
array. To see the entire array you can print_r($_GET);
in your PHP code. In the example I am also testing to make sure the variable has been set, for safeties sake. You should never accept user input without sanitizing, which I have not done here.
You can also set a variable with the array item:
$btn1Pressed = $_GET['btn1Pressed'];
You can try use GET method:
if ($_GET['btn1Pressed'] == 1) {
echo 'Button One Pressed';
}
else{
echo 'Button Two Pressed';
}
<?php
$btn_pressed = filter_input(INPUT_GET, 'btn1Pressed', FILTER_SANITIZE_NUMBER_INT);
if ($btn_pressed == 1) {
echo 'Button One Pressed';
} else {
echo 'anything';
}
?>