0

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Sumeet Gohel
  • 39
  • 1
  • 7
  • You might be interested to read: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) – hakre Aug 21 '13 at 06:39
  • The script you're submitting the POST data to is not the script running the database query. Which puzzles me because your OUTPUT line of the question suggests that you're actually fire-ring a database query which technically is not possible. So it looks like you not only have some mistake in your code, but you also made one formulating the question. I'd say let's fix one problem before the other and start with fixing your question. – hakre Aug 21 '13 at 06:42
  • @hakre:You are right i tried to solve that using a temporary variable but still is not working... – Sumeet Gohel Aug 21 '13 at 06:48

3 Answers3

1

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

Snik
  • 171
  • 2
  • 10
1

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");}
     ?>
raduns
  • 765
  • 7
  • 18
0

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."';
}
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90