我想在另一个php文件中获得变量值
假设有三个文件
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.另一个.php
<?php include('db_store.php');?>
echo "SELECT * FROM tabl_name where category_id='".$my_cat."';
输出:
SELECT * FROM tabl_name where category_id='';
如何在此查询中获取具有精确值的值。我使用了var_dump,还尝试了SESSION变量。
显而易见的解决方案是将include和action直接丢弃到另一个。php
假设include有充分的理由,那么我只能认为您应该使用会话变量来存储$_POST。请记住将session_start()放在两个php文件
尝试此操作,不使用会话变量
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."';
}
?>
通常,session
变量可用于一个应用程序中的所有页面。如果你想使用会话变量,试试这个
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;
}
?>
另一个.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");}
?>
您需要在提交检查条件中包含文件。
if(isset($_POST['submit']))
{
$my_cat=$_POST['category'];
echo "SELECT * FROM tabl_name where category_id='".$my_cat."';
}