我繼續寫一個迷宮生成器來教自己的PHP ...(php)在初始表單之後獲得額外的輸入?
我已經設置了一個會話,並且收集一個名稱和一個迷宮大小(5-20)的表單。
在第一次運行時,工作正常,迷宮生成。
我想進一步的會議是名字被記住,但大小是重新輸入。
我試過使用另一種(不同)的形式,如果會話不是新的,但它不承認/運行它和錯誤,告訴我$ mazesize是一個無法識別的索引。
代碼:
<?php
session_start();
error_reporting(-1);
?>
<html>
<head>
</head>
<body>
<?php
include 'ClassFunc1.php';
//program functionality
if (!isset($_SESSION['name']) && !isset($_POST['name'])){
//if no data, print form
?><table><tr>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<td>Name: </td><td><input type="text" name="name"></td></tr>
<tr><td>Maze size (min 5 max 20): </td><td><input type="text" name="mazesize"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td></tr>
</form></table>
<?php
}else if (!isset($_SESSION['name']) && isset($_POST['name']) && isset($_POST['mazesize'])){
//if a session doesn't exist but the form has been submitted
//check to see if the form has all required values
//create a new session
if(!empty($_POST['name'])){
$_SESSION['name']=$_POST['name'];
$_SESSION['start']=time();
echo "Welcome, ".$_POST['name'].". A new session has been activated for you. Click <a href=".$_SERVER['PHP_SELF'].">here</a> to refresh the page.";
if($_POST['mazesize']>20){
$m_size=20;
}elseif($_POST['mazesize']<5){
$m_size=5;
}else{
$m_size=$_POST['mazesize'];
}
define("COL_MAX",$m_size);
define("ROW_MAX",$m_size);
run_maze($_POST['name']);
close_down();
}else{
echo "ERROR. Please enter your name and a maze size";
}
}else if (isset($_SESSION['name'])) {
//if a previous session exists
//calculate elapsed time since session start and now
echo "Welcome back, ".$_SESSION['name'].". This session was activated ".round((time()-$_SESSION['start'])/60)." minute(s) ago. Click <a href=".$_SERVER['PHP_SELF'].">here</a> to refresh the page.<br />";
echo "Current session ID: ".session_id();
//Ask for a new maze size
?><table><tr>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<td>Maze size (min 5 max 20): </td><td><input type="text" name="mazesize"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td></tr>
</form></table>
<?php
if($_POST['mazesize']>20){
$m_size=20;
}elseif($_POST['mazesize']<5){
$m_size=5;
}else{
$m_size=$_POST['mazesize'];
}
define("COL_MAX",$m_size);
define("ROW_MAX",$m_size);
run_maze($_POST['name']);
close_down();
}
//session_destroy();
?>
「包括」 文件是類,方法&功能。
我確定我做錯了什麼,任何人都可以幫忙嗎?
非常感謝
ETA:
(代碼回聲「錯誤修正後...行)
}else if (isset($_SESSION['name']) && !isset($_POST['n_mazesize'])) {
//if a previous session exists but n_mazesize not set
//calculate elapsed time since session start and now
echo "Welcome back, ".$_SESSION['name'].". This session was activated ".round((time()-$_SESSION['start'])/60)." minute(s) ago. Click <a href=".$_SERVER['PHP_SELF'].">here</a> to refresh the page.<br />";
echo "Current session ID: ".session_id();
//Ask for a new maze size
?><table><tr>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<td>Maze size (min 5 max 20): </td><td><input type="text" name="n_mazesize"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td></tr>
</form></table>
<?php
}else if (!isset($_SESSION['name']) && isset($_POST['n_mazesize'])){
//if a previous session exists and n_mazesize is set
//calculate elapsed time since session start and now
echo "Welcome back, ".$_SESSION['name'].". This session was activated ".round((time()-$_SESSION['start'])/60)." minute(s) ago. Click <a href=".$_SERVER['PHP_SELF'].">here</a> to refresh the page.<br />";
echo "Current session ID: ".session_id();
if(!empty($_POST['n_mazesize'])){
if($_POST['n_mazesize']>20){
$m_size=20;
}elseif($_POST['n_mazesize']<5){
$m_size=5;
}else{
$m_size=$_POST['n_mazesize'];
}
run_maze($_SESSION['name'], $m_size);
close_down();
}
//session_destroy();
}
?>
</body>
</html>
這要求進行規模但提交返回一個空白屏幕: - (
你在調用'session_start()'的地方嗎? –
是的,這只是代碼的摘錄;如果沒有設置會話名稱,會出現一個表單詢問名稱和大小,如果會話名稱已設置,則會嘗試詢問新的大小。此代碼之前的行是:'} else if(isset($ _ SESSION ['name'])){' – horsethorn
我會將整個代碼添加到我的問題中... – horsethorn