Sumeet Gohel
Sumeet Gohel

Reputation: 39

How can I fetch POST Variable value into another php file by include that file in another php file?

I want to get variable value in my another php file

suppose there is three files

1. category.html

<form name='my _form' method='post' action='db_store.php'>
<select name="category">
<option value='yahoo'>Yahoo</option>
</select>
<input type="submit.php" value="submit" name="submit">

2. db_store.php

if(isset($_POST['submit']))
{
    $my_cat=$_POST['category'];
}

3. another.php

<?php include('db_store.php');?>
echo "SELECT * FROM tabl_name where category_id='".$my_cat."';

OUTPUT:

SELECT * FROM tabl_name where category_id='';

How to get value in this query with exact value. I used var_dump and also tried through SESSION variable.

Upvotes: 0

Views: 4358

Answers (3)

user1850534
user1850534

Reputation:

Try this, without using session variable

db_store.php

<?php
if(isset($_POST['submit']))
{
    $my_cat=$_POST['category'];
    include('db_store.php');
    echo "SELECT * FROM tabl_name where category_id='".$my_cat."';
}
?>

Generally, session variable is available to all pages in one application. If you want to use session variable, try this

db_store.php

<?php
session_start(); //starting session at the top of the page
if(isset($_POST['submit']))
{
    $my_cat=$_POST['category'];
    $_SESSION['category']=$my_cat;
}
?>

another.php

    <?php
       session_start();  //starting session at the top of the page
     include('db_store.php');
    if(isset($_SESSION['category']){
         $category = $_SESSION['category'];
    echo "SELECT * FROM tabl_name where category_id='".$category."';
   unset($_SESSION['category']); //to unset session variable  $_SESSION['category'] 
    }else { die("No session variable found");}
     ?>

Upvotes: 1

Dipesh Parmar
Dipesh Parmar

Reputation: 27382

You need to include file inside submit check if condition.

if(isset($_POST['submit']))
{
    $my_cat=$_POST['category'];
    echo "SELECT * FROM tabl_name where category_id='".$my_cat."';
}

Upvotes: 0

Snik
Snik

Reputation: 171

The obvious solution is to ditch the include and action directly to another.php

Assuming there is a good reason for the include, then I can only think you should use a session variable to store the $_POST. Remember to put session_start() at the VERY top of both php files

Upvotes: 1

Related Questions