Search code examples
phphtmlgetxampp

Undefined variable: _get XAMPP PHP


I'm just trying to add two integers using PHP and XAMP.

I've placed my client.html file and service.php (which add numbers) in C:\xampp\htdocs

and I get

"Notice: Undefined variable: _get in C:\xampp\htdocs\service.php on line 7

Notice:

Undefined variable: _get in C:\xampp\htdocs\service.php on line 8" error.

Before posting this error to Stack Overflow. Let me tell you that I double checked my file names, variable names case-sensitive etc everything. but still having the same error. Any help will be really appreciated.

This is my client.html

form action="service.php" method="get">
    input type="text" name="txt1"> <br />
    input type="text" name="txt2"> <br />
    input type="submit" value="add"><br />

and here is service.php

<?PHP

echo "This is my first program in php";
$a= $_get['txt1'];
$b= $_get['txt2'];
echo $a + $b;
?>

Solution

  • GET is a SUPER GLOBAL VARIABLE and to access it you have to use $_GET.

    So do like below:-

    <?PHP
    
    echo "This is my first program in php";
    $a= $_GET['txt1'];
    $b= $_GET['txt2'];
    echo $a + $b;
    ?>
    

    Note:-

    using POST is more secure than GET (in the sense that data shown into the url in get request, but not in post)

    So just use post instead of get in <form method>

    and $_POST instead of $_GET.

    like:-

    form action="service.php" method="POST">
        input type="text" name="txt1"> <br />
        input type="text" name="txt2"> <br />
        input type="submit" value="add"><br />
    

    AND

    <?PHP
    
    echo "This is my first program in php";
    $a= $_POST['txt1'];
    $b= $_POST['txt2'];
    echo $a + $b;
    ?>