1

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;
?>
2
  • $_GET. Also use POST more secure Commented Aug 2, 2016 at 7:32
  • If you want, you can also use $_POST instead because it's more secure since the user can't see it in the url. $_GET will be transmitted in the url. Commented Aug 2, 2016 at 7:43

3 Answers 3

2

That's because $_GET and $_get are two different variables. You must use capital letters. So PHP thinks you're referring to another variable.

This will work :

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

If you're new to PHP, these two pages should help : Variable basics (php.net) and $_GET

Sign up to request clarification or add additional context in comments.

Comments

1

GET Variable name should be all in CAPS,

So your code might look something like this,

<?PHP

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

Reference: http://php.net/manual/en/reserved.variables.get.php

$_GET is predefined reserved variable.


Also it's advisable to use POST method (as @Anant mentioned) to send sensitive data to server, You can access those data which is sent using POST method by $_POST variable.

Comments

0

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;
?>

3 Comments

Define "more secure". In what respect? What if security is irrelevant? The GET form submission method has its uses.
@deceze added that explanation
It's still unclear why data not shown in the URL is "more secure", or in what scenarios it's "more secure". You can't make blanket statements like that, there's a lot more nuance to things relating to "security".

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.