0

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'; 
    }
?>
0

3 Answers 3

3

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'];
Sign up to request clarification or add additional context in comments.

7 Comments

Vwahahaha. I was here to suck the easy rep without explaining anything.. but you beat me to it and gave a very good explanation. Thus... I must upvote you instead. =P
So I tried this method then ran my .php?btn1Pressed=1 but it's still not echoing Button One Pressed?
Are you running it on a webserver @MattHutch?
And the code is in server1.php? If so, add error reporting to the top of your page right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1);
Added this line, no changes?
|
0

You can try use GET method:

if ($_GET['btn1Pressed'] == 1) {
    echo 'Button One Pressed';
}
else{
    echo 'Button Two Pressed'; 
}

Comments

-1
<?php

$btn_pressed = filter_input(INPUT_GET, 'btn1Pressed',  FILTER_SANITIZE_NUMBER_INT);

if ($btn_pressed == 1) {
    echo 'Button One Pressed';
} else {
echo 'anything';
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.