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;
?>
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
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.
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;
?>
Related
On the first page (index.php) I wrote this:
<?php
echo '<form action="test.php" method="get">';
$x=0;
while($x<5){
$x++;
$test='tester'.$x;
echo '<input type="text" name="$test"><br>';
};
echo '<input type="submit" value="submit">';
?>
on the second page (test.php) I wrote this:
<?php
echo $_POST['tester1'];
echo $_POST['tester2'];
echo $_POST['tester3'];
echo $_POST['tester4'];
echo $_POST['tester5'];
?>
and when I tested it I got these errors
Notice: Undefined index: tester1 in C:\xampp\htdocs\test.php on line 2
Notice: Undefined index: tester2 in C:\xampp\htdocs\test.php on line 3
Notice: Undefined index: tester3 in C:\xampp\htdocs\test.php on line 4
Notice: Undefined index: tester4 in C:\xampp\htdocs\test.php on line 5
Notice: Undefined index: tester5 in C:\xampp\htdocs\test.php on line 6
The code below is an example for the real code being meant for users to fill in a number. I want to use these numbers later for calculations so they all need an unique name. The <input> objects are generated via a loop, the amount of times the loop runs is specified by the amount of rows in a database.
The two main issues are that you are using PHP variables inside single quotes, this doesn't pass the actual variable, but the actual name. As an example
$foo = "bar";
echo 'This is $foo';
would print
This is $foo
If you use double-quotes, the variable's content would be passed,
$foo = "bar";
echo "This is $foo"; // You can also do the following: echo 'This is '.$foo;
would print
This is bar
Secondly, you are using method="get" in your form, but trying to retrieve them as POST variables. This means that you have to change it to method="POST".
Another alternative, is to create an array of elements, and use a loop in PHP to retrieve the values. An example given below. The first snippet generates a form with 5 input-fields as an array.
<form action="test.php" method="POST">
<?php for ($x=0; $x<5; $x++) { ?>
<input type="text" name="tester[]" />
<?php } ?>
<input type="submit" name="submit" />
</form>
And in PHP, loop through that array.
<?php
if (isset($_POST['tester']) {
// If the values are set, to avoid Undefined Index notices
foreach ($_POST['tester'] as $value) {
echo $value."<br />";
}
}
?>
This is how it works (index.php):
<?php
echo '<form action="test.php" method="post">';
$x=0;
while($x<5){
$x++;
$test='tester'.$x;
echo '<input type="text" name="'.$test.'"><br>';
};
echo '<input type="submit" value="submit">';
?>
simply append this code to the top of test.php
if(!isset($_POST['tester1']) || !isset($_POST['tester2']) ... etc){
exit(0); //or do something to signal index.php of missing values
}
/* (the rest of the code */
When writing a dynamic page like this, you should always expect there to be a missing variable and code an escape so you never run into an error such as that.
I have two pages where one links one to the other. How can i pass the value of the variable(region_name) in my anchor(select.php) to the next page(display.php)? i used the following codes and im getting Notice: Undefined variable: region_name.how do i fix it?
select.php
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a name="region_name" method="POST" value="Greater Accra Region" type="submit" href="display.php" target="main">Greater Accra Region</a>
</h4>
</div>
</div>
display.php
<?php
ob_start();
session_start();
if(isset($_POST['region_name'])){
$region_name = $_POST['region_name'];
}
$con=mysqli_connect("localhost","root","root","findings");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM approved_reservation WHERE region_name = '$region_name'";
$records=mysqli_query($con,$sql);
?>
We pass the variable $myVar through the URL and use $_GET in the next page to use it. We use htmlspecialchars(); to clean user input.
We use the if (isset($_GET['myVar'])) so it doesn't throw Notice: Undefined index error.
pageOne.php
<?php
$myVar = "My variable one.";
echo "<a href='pageTwo.php?myVar=" . $myVar . "'>Page Two</a>"
?>
pageTwo.php
<?php
if (isset($_GET['myVar'])) {
$myVar = htmlspecialchars($_GET['myVar'], ENT_QUOTES);
// Now you can use $myVar.
}
?>
Now I'm not sure what variable you want to pass through so I used a simple skeleton which I made yesterday to show you how it is done. If you supply more code, I'll edit it to suit your scenario.
Edit 1
Your code below is incorrect because you're declaring your variable $region_name inside the scope of the if statement your queries and other code which is dependent on the variable has to be in that if statement.
Another solution would be to define an empty variable $region_name outside of the if statement then after the if statement you won't get the error Undefined Index.
if(isset($_POST['region_name'])){
$region_name = $_POST['region_name'];
}
Edit 2
That error is being thrown for two reasons,
Your not actually passing the region name through the URL - though even if you were it wouldn't work as you're using $_POST instead of $_GET.
I outlined reason 2 in the above edit. You're using a variable which isn't in the scope of the queries.
You're getting an undefined variable because of the following:
href does not use methods, nor values, nor type, <form>...</form> does.
Forms use inputs with name attributes set for its method.
Your code is expecting it to be passed via a form and using a POST method.
More on forms here http://php.net/manual/en/tutorial.forms.php
What you want to use here is a GET in your conditional statement:
if(isset($_GET['region_name']) && !empty($_GET['region_name']) ){
$region_name = $_GET['region_name'];
echo $region_name;
}
Sidenote: I used !empty() should someone want to modify the URL as ?region_name=
and using a ? parameter after the filename and a = followed by the "value" you wish it to be:
Greater Accra Region
which in turn will echo "your_value".
You will need to modify its value respectively.
In your case, that would read as:
Greater Accra Region
You can also add to the above and inside the conditional statement:
if($_GET['region_name'] == "Greater Accra Region"){
echo "Region is Greater Accra Region";
}
Which can be modified as:
Sidenote: The following example checks if a region equals to something, and if the value was modified in the URL such as ?region_name=.
if(isset($_GET['region_name'])){
$region_name = $_GET['region_name'];
echo $region_name;
if($_GET['region_name'] == "Greater Accra Region"){
echo "Region is Greater Accra Region"; }
if(empty($_GET['region_name'])){
echo "EMPTY"; }
}
Now, seeing your query, that is unknown as to what the ultimate goal is.
Only you know and goes beyond the scope of your original question.
If you have a form (with a POST method) that you haven't posted, then you can use an input:
I.e.:
Greater Accra Region:
<input name="region_name" value="Greater Accra Region" type="submit">
and using your present POST arrays.
Your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements.
The simplest form of passing along the data to display.php would be to add the region_name to the URL like so:
href="displayApprovedReservation.php?region_name=<?php echo $region_name; ?>"
In the display.php, you would then use $_GET['region_name'] to retrieve the data from the URL. Doing it this way, you would need to make sure you sanitize the input as it can be changed very easily from the URL by a user.
The reason you are getting the error Notice: Undefined variable: region_name is because the data is not being posted to the page, therefore $_POST['region_name'] is not set, thus not setting $region_name in the process -- yet you are still calling it in the SQL query regardless of the variable being set:
$sql="SELECT * FROM approved_reservation WHERE region_name = '$region_name'";
There are several ways to do what you're trying to do. The method that you're trying to achieve would require a form like:
<form name="selection" method="POST" action="display.php">
<input name="region" type="radio" value="Greater Accra Region">
</form>
But it doesn't look great and don't work the way you want to.
Another method would be just to use a get variable like this
Greater Accra Region
Then on your display file you can use the variable $_GET['selection']; to use your selection :)
Hello i have problem with isset id like someone to help me fixin this problem. Its this problem Notice: Undefined variable: v in C:\xampp\htdocs\something\something2.php on line 17 and my code is :
<form id="priklad" method="post">
<label>Obvod kruhu:</label>
<?php IF (isset($_POST['v'])) {
$_POST['v'];
}
?>
<label>r:</label><input type="text" name="r"/>
<input type="submit" value="Vypočti"/></br>
<label><?php echo 2*$v;?> cm</label>
</form>
EDIT:
<form id="priklad" method="post">
<label>Obvod kruhu:</label>
<?php If (is_numeric($_POST['v'])) {
$v = $_POST['v'];
}
?>
<label>v:</label><input type="text" name="v"/>
<input type="submit" value="Vypočti"/></br>
<label><?php echo 2*$v;?> cm</label>
</form>
and im getting these errors
Notice: Undefined index: v in C:\xampp\htdocs\something\something2.php on line 11 and Notice: Undefined variable: v in C:\xampp\htdocs\something\something2.php on line 17
What's actually happening with that error is that your variable $v isn't being initialized because the code has not enter the if condition.
if(is_numeric($_POST['v'])){
$v = $_POST['v'];
}
You're assuming that the condition is_numeric($_POST..) is true but it might not be, which means that the variable $v is never initialized. Therefore is pretty normal the error you are having.
You can continue with your condition but you must check for the variable after it.
if(is_numeric($_POST['v'])){
$v = $_POST['v'];
}
echo 2 * (isset($v) ? $v : 1);
The above code might be improved. A better code/logic would be you to set up the default value of the variable in case the is_numeric returns false.
$v = 1; // default value
if(is_numeric($_POST['v'])){
$v = $_POST['v'];
}
echo 2 * $v;
What is happening is that your value v is not the same as the implied value of $_POST['v'] . As stated by Tim, there is no $v = statement so in effect your echo output is outputting nothing multiplied by 2.
This throws a warning which you have picked up.
So a solution could be:
<form id="priklad" method="post">
<label>Obvod kruhu:</label>
<?php If (is_numeric($_POST['v'])) {
$v = $_POST['v'];
}
?>
<label>r:</label><input type="text" name="r"/>
<input type="submit" value="Vypočti"/></br>
<label><?php echo 2*$v;?> cm</label>
</form>
I have used is_numeric as you are using the echo to produce a value x 2 so makes more sense to check V is a number rather than a string.
UPDATE
Ok, first stop then is to see what is on line 11? is it $_POST['v'] ? If so then you can work back to the source of the cause - replace the line 11 (comment it out, don't delete it) with something like:
print_r($_POST);
This will output all values associated with the array $_POST.
If you do not see a value for 'v' (and I guess you probably won't) then that means this value wasn't set in the previous HTML page so go back and check that your form submits an input such as:
<input name='v' value="something">
Undefined index means the array ( $_POST ) does not contain any information stored under $_POST['v'].
Can you confirm your POST is as intended, and that you do want post which means you need to submit the form rather than GET or another method, such as index.php?v=10 ?
The Notice:
PHP does have notice statements but they're only notices, they are not errors or even warnings, it's just PHP telling you what's going on. This will not break your code, but it could be nice to add this line above your IF statement:
$v = 0;
This establishes the variable $v and gives it a value. Which if $_POST['v'] exists is then updated to reflect that.
Your code must be like this.
<form id="priklad" method="post">
<label>Obvod kruhu:</label>
<?php
$v = '';
If (is_numeric($_POST['v'])) {
$v = $_POST['v'];
}
?>
<label>v:</label><input type="text" name="v"/>
<input type="submit" value="Vypočti"/></br>
<label><?php echo (($v)? 2*$v : 'Its not numeric value');?> cm</label>
</form>
I wrote a simple php script that basically echos the values put into a form on the page, later, I will have this write to a DB but I was working on troubleshooting it before I did that since I keep getting this warning.
Does anyone know why I am getting it? If I just fill in the fields and submit the form, the script works fine and the warning disappears.
PHP Function:
function quickEntry()
{
$subTitle = $_POST['subTitle'];
$subDetails = $_POST['details']; //This is line 13 in my code
echo "$subTitle";
echo "<br>$subDetails";
}
HTML / PHP Code:
<form method="post" action="">
<hr>
<h1>Quick Entry:<p></h1>
Subject Title:<br> <input type="text" name="subTitle"><br><br>
Subject Details: <p><textarea name="details" placeholder="Enter Details here..."></textarea><p><br>
<input type="submit" name="QuickEntrySubmit" value="Submit Quick Entry" /><br>
</form>
<?php
if (isset($_POST['QuickEntrySubmit']))
{
quickEntry();
}
?>
I know that I could disable warnings and I wouldn't see this, but I really just want to know why php is throwing the warning so I can fix the syntax appropriately and keep my code clean going forward. Full warning is:
Notice: Undefined index: details in C:\xampp\htdocs\test1.php on line 13
Thanks!
The reason why you are getting that error is because you are not checking whether the 'subTitle' and 'details' inputs have values in them.
Your code should work well like this:
function quickEntry(){
$subTitle = isset($_POST['subTitle'])? $_POST['subTitle']: null;
$subDetails = isset($_POST['details'])? $_POST['details']: null ; //This is line 13 in my code
if(!is_null($subTitle) && !is_null($subDetails)){
echo "$subTitle";
echo "<br>$subDetails";
} else{
//blah blah blah
}
You get the warnings because the $_POST variables with the indexes that you're checking for ($_POST['subTitle'] & $_POST['details']) aren't set, so the variables are empty as you reference something that isn't there.
You should do a check to ensure they are set first before trying to assign them to a variable:
$subTitle = (isset($_POST['subTitle']) && !empty($_POST['subTitle'])) ? $_POST['subTitle'] : null;
$subDetails = (isset($_POST['details']) && !empty($_POST['details'])) ? $_POST['details'] : null;
The above code will check to ensure the post index isset() and isn't empty() before assigning it to the variables.
NOTE
The variables are set when you submit the form because you are actually posting the data to the script.
You see the input attribute name? When you submit the form, they are the relevant $_POST indexes.
Just remember to ensure that the values aren't empty if you intend to print them to the markup.
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 9 years ago.
In a html form I have a variable $var = "some value";.
I want to call this variable after the form posted. The form is posted on the same page.
I want to call here
if (isset($_POST['save_exit']))
{
echo $var;
}
But the variable is not printing. Where I have to use the code GLOBAL ??
EDIT: After your comments, I understand that you want to pass variable through your form.
You can do this using hidden field:
<input type='hidden' name='var' value='<?php echo "$var";?>'/>
In PHP action File:
<?php
if(isset($_POST['var'])) $var=$_POST['var'];
?>
Or using sessions:
In your first page:
$_SESSION['var']=$var;
start_session(); should be placed at the beginning of your php page.
In PHP action File:
if(isset($_SESSION['var'])) $var=$_SESSION['var'];
First Answer:
You can also use $GLOBALS :
if (isset($_POST['save_exit']))
{
echo $GLOBALS['var'];
}
Check this documentation for more informations.
Try that
First place
global $var;
$var = 'value';
Second place
global $var;
if (isset($_POST['save_exit']))
{
echo $var;
}
Or if you want to be more explicit you can use the globals array:
$GLOBALS['var'] = 'test';
// after that
echo $GLOBALS['var'];
And here is third options which has nothing to do with PHP global that is due to the lack of clarity and information in the question. So if you have form in HTML and you want to pass "variable"/value to another PHP script you have to do the following:
HTML form
<form action="script.php" method="post">
<input type="text" value="<?php echo $var?>" name="var" />
<input type="submit" value="Send" />
</form>
PHP script ("script.php")
<?php
$var = $_POST['var'];
echo $var;
?>