我正在尝试使用php的$ _POST功能或$ _SESSION功能来获取基于前一页面的表单数据的数组。每当我尝试使用$ _SESSION时,我都会收到错误:“脚本试图执行方法或访问不完整对象的属性。”这是我想要做的事情的想法。任何帮助都会很棒,并提前感谢!
<?php
// initialize a session
include 'hoteldetails.php';
session_start();
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<center><h1>Room Reserves</h1>
<h2>Search</h2></center>
<h3>Login <br/> Sign up</h3>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
Please enter City, State or Zipcode:<input type="text" name="location">
<input type="date" name="startdate" value="startdate">
to <input type="date" name="enddate" value="enddate">
<br/>
Minimum price:<select name="minimumprice">
<option value="0">No Minimum</option>
<option value="49">$50</option>
<option value="99">$100</option>
<option value="199">$200</option>
</select>
Maximum price:<select name="maximumprice">
<option value="9999">No Maximum</option>
<option value="101">$100</option>
<option value="201">$200</option>
<option value="301">$300</option>
</select>
Beds:<select name="beds">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
Bathrooms:<select name="baths">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br/>
<input type="submit" name="Search" value="Search">
</form>
<?php
try{
$dbconn= pg_connect("host=localhost port=5432 dbname=roomReserves user=superUser password=F3Poriginal");
}
catch(PDOException $e){
echo $e->getMessage();
}
if(isset($_POST['Search'])){
if($_POST['startdate']==null || $_POST['enddate']==null || $_POST['location']==null){
echo "One or more fields are empty, please try again.";
}
else{
$query = "SELECT * FROM hotellist WHERE zip=75007";
$rs = pg_query($dbconn, $query) or die("Cannot execute query: $query\n");
// $hotelsdetail=array();
$_SESSION['hotelsdeets']=array();
$hotelpos=0;
while ($row = pg_fetch_row($rs)) {
$serverInfo= explode(" ",$row[2]);
$hotelsdb= pg_connect("host=$serverInfo[0] port=$serverInfo[1] dbname=$serverInfo[2] user=$serverInfo[3] password=$serverInfo[4]");
$queryresult = "SELECT * FROM hotelinfo where beds= CAST('".$_POST['beds']."' AS INT) AND baths= CAST(baths='".$_POST['baths']."' AS INT) AND price BETWEEN CAST('".$_POST['minimumprice']."' AS INT) AND CAST('".$_POST['maximumprice']."' AS INT)";
$hotelrs = pg_query($hotelsdb, $queryresult) or die("Cannot query hotel info: $queryresult\n");
$matchesquery=false;
while($hotelrow=pg_fetch_row($hotelrs) && $matchesquery==false){
$matchesquery=true;
$date = $_POST['startdate'];
$endDate= $_POST['enddate'];
$hotelroomrs=pg_fetch_row($hotelrs);
$hotelroom=$hotelroomrs[0];
$hotelprice=$hotelroomrs[1];
while ($date < $endDate && $matchesquery==true && $hotelroomrs[0]!=null) {
$isavail="SELECT * FROM hotelinfo where roomnumber= CAST('".$hotelroom."' AS INT) AND date= CAST('".$date."' AS date)";
$roomexists=pg_query($hotelsdb, $isavail);
$roomdets=pg_fetch_row($roomexists);
if($roomdets[0]==null){
$matchesquery=false;
}
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
if($matchesquery==true && $hotelroomrs[0]!=null){
$_SESSION['hotelsdeets'][$hotelpos]=new hoteldetails();
$_SESSION['hotelsdeets'][$hotelpos]->sethotelinfo($row);
$_SESSION['hotelsdeets'][$hotelpos]->setprice($hotelprice);
$hotelpos++;
}
}
pg_close($hotelsdb);
}
header("Location: ./searchresults.php");
}
}
pg_close($dbconn);
?>
<?php
session_start();
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<center><h1>Room Reserves</h1>
<h2>Results</h2></center>
<h3>Login<br/>Sign up</h3>
</head>
<body>
<?PHP
//$searchinfo->$hotelsdetail[0]->sethotelinfo(1);
echo $_SESSION['hotelsdeets'][0]->gethotelinfo()[1];
?>
</body>
</html>
class hoteldetails {
private $hotelinfo;
private $price;
public function sethotelinfo($hotelinfo){
$this->hotelinfo=$hotelinfo;
}
public function setprice($price){
$this->price=$price;
}
public function gethotelinfo(){
return $this->hotelinfo;
}
public function get($var){
return $this->{$var};
}
}
再一次,谢谢!
答案 0 :(得分:0)
作为起点,您需要打开和关闭php标签:
<?PHP
session_start();
?>
<html>
<head>
</head>
<body>
<?PHP
$colors=array();
array_push($colors, "green");
array_push($colors, "white");
array_push($colors, "yellow");
array_push($colors, "black");
$_SESSION['color']=$colors;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="Search" value="Search">
</form>
<?php
if(isset($_POST['Search'])){
header("Location: page2.php");
}
?>