-2

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

?>
Emmanuel Gamor
  • 69
  • 1
  • 10

4 Answers4

2

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,

  1. 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.

  2. I outlined reason 2 in the above edit. You're using a variable which isn't in the scope of the queries.

Script47
  • 14,230
  • 4
  • 45
  • 66
1

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'";

RhapX
  • 1,663
  • 2
  • 10
  • 17
1

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.

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:

<a href="display.php?region_name=your_value" target="main">Greater Accra Region</a>

which in turn will echo "your_value".

You will need to modify its value respectively.

In your case, that would read as:

<a href="display.php?region_name=Greater Accra Region" target="main">Greater Accra Region</a>

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.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

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

<a href="display.php?selection=Greater%20Accra%20Region">Greater Accra Region</a>

Then on your display file you can use the variable $_GET['selection']; to use your selection :)

bashleigh
  • 8,813
  • 5
  • 29
  • 49