Ciao ho un problema che non riesco a risolvere, sto seguendo un manuale on-line per imparare php, ma sono bloccato da un po' perchè succede che non riesco a far passare una variabile di un radio input alla pagina successiva.
Ecco il codice di pagina 1, dove creo la variabile $roomNumb prendendo i dati ("id") da una tabella ("adventure") mysql. La inserisco poi con un ciclo WHILE nel campo "value" dal form radio. Il quale dovrebbe poi inviarla a pagina2 con method="post".
Codice PHP:
<html>
<head>
<title> PAGINA1</title>
<style type="text/css"> body {color:red} td{color:white; background-color:blue}
th {color:white; background-color:red} </style>
</head>
<body>
<?
$connect=mysql_connect("localhost", "root", "root") or die ("no connection");
mysql_select_db("adv1", $connect);
$sql="SELECT * FROM adventure ORDER BY id";
$result=mysql_query($sql, $connect);
print <<<HERE
<form method="post"
action="PAGINA2.php">
HERE;
while ($row=mysql_fetch_assoc($result)) {
print "<table border= 1 width=80%>\n";
foreach ($row as $key=>$value) {
$roomNumb=$row["id"];
print <<<HERE
<tr>
<th width=10%>$key</th>
<td>$value</td>
</tr>
HERE;
}
print <<<HERE
<tr>
<td colspan=2 align="center">
<input type"radio"
name="room"
value="$roomNumb"> Modifica questa room //QUESTO VALORE NON PASSA
<input type="submit"
value="edit">
</td></tr></table>
HERE;
}
?>
<center> <input type = "submit"
value = "edit indicated room">
</center>
</form>
</body>
</html>
pagina2 (in teoria dovrebbe ricevere la variabile $room del form radio di pag1 , ma in realtà mi tiene sempre l'ultimo valore che viene creato dal ciclo WHILE di pag1)..
codice:
Codice PHP:
<html>
<head>
<title> PAGINA2</title>
<style type="text/css"> body {color:red} td{color:white; background-color:blue; width:20%; height:5em; text-align:center} </style>
</head>
<body>
<?
$room=$_POST['room']; //LA VARIABILE PASSA MA COL VALORE SBAGLIATO
if(empty($room)) {
$room="0";
}
$connect=mysql_connect("localhost", "root", "root") or die ("no connection");
mysql_select_db("adv1", $connect);
$sql="SELECT * FROM adventure WHERE id='$room'"; ////ECCOLA QUI
// IL RESTO NON E' IMPORTANTE
$result=mysql_query($sql, $connect);
$mainRow=mysql_fetch_assoc($result);
$theText=$mainRow["description"];
$roomName=$mainRow["name"];
$northList=makeList("north", $mainRow["north"]);
$southList=makeList("south", $mainRow["south"]);
$westList=makeList("west", $mainRow["west"]);
$eastList=makeList("east", $mainRow["east"]);
$roomNum=$mainRow["id"];
print <<<HERE
<form method="post"
action="saveRoom.php">
<table border=1>
<tr>
<td colspan=3>
Room # $roomNum:
<input type="text"
name="name"
value="$roomName">
<input type="hidden"
name="id"
value="$roomNum">
</td>
</tr>
<tr>
<td></td>
<td>$northList</td>
<td></td>
</tr>
<tr>
<td>$westList</td>
<td><textarea rows=5 cols=30 name="description">$theText</textarea>
</td>
<td>$eastList</td>
</tr>
<tr>
<td></td>
<td>$southList</td>
<td></td>
</tr>
<tr>
<td colspan=3>
<input type="submit"
value="save this room">
</td>
</tr>
</table>
</form>
HERE;
function makeList($dir, $current) {
global $connect;
$listCode="<select name=$dir>";
$sql="SELECT id, name FROM adventure";
$result=mysql_query($sql, $connect);
$rowNum=0;
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$placeName=$row["name"];
$listCode .="<option value = $id";
if ($rowNum == $current) {
$listCode .=" selected\n";
}
$listCode .=">$placeName</option>";
$rowNum++;
}
return $listCode;
}
print "done";
?>
</center>
</body>
</html>
Ho iniziato da poco a studiare php, ho comunque visto che nel manuale che seguo (della serie absolute beginner) non c'è bisogno ogni volta di richiamare le variabili a inizio pagina con $_POST etc. (una roba che chiamano register_global settato su on o off che non ho capito bene cos'è) quindi probabilmente credo sia lì il problema perchè il resto del codice è uguale al loro..
ciao grazie per l'aiuto