ollie101
03-10-2009, 02:01 PM
I'm learning PHP at the moment and have created a few pages on my hosting account to test out things.
I am trying to load a value from a randomly generated postion in an array in an external PHP file and echo it to the screen inside a generated div.
The text doesn't appear on the page and is missing in the source of the final page. I have managed to do exactly the same thing in a different file but not inside a div and these files are in the same directory with the code being identical.
The online example is here:
http://www.olliesilviotti.co.cc/php_stuff
(the second submit button (next to the combo box) executes the script I'm talking about)
Here's the relevant source's:
index.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include("txt.php"); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Stuff</title>
</head>
<body>
<form method="get" action="phpscript.php">
Name: <input type="text" name="nameBox" />
<input type="submit" name="btn1" value="Submit"/>
</form>
<br /><br />
<hr>
<form method="post" action="phpscript2.php">
<select name="combo_no">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" name="submitbtn" value="Submit" />
</form>
<hr>
<?php
$randval = rand(0, 2);
$imglist = array("red", "blue", "green");
echo "<img src='images/".$imglist[$randval].".jpg' />";
?>
<hr>
<?php $randmessval = rand(0,2); echo $mess[$randmessval]; ?>
</body>
</html>
phpscript2.php:
<?php
$no_divs = $_POST['combo_no'];
echo "<html>"."\n"."<head>"."\n"."<link rel='text/css' type='stylesheet' src='script2style.css'/>"."\n"."<title>PHP Stuff - Script 2 - DIVs</title>"."\n"."<?php include(\"txt.php\"); ?>"."\n"."</head>"."\n"."<body>"."\n";
for($i = 0; $i < $no_divs; $i++)
{
echo "<div id='div".$i."'".">"."\n";
echo "<?php echo $mess[0]; ?>";
echo "\n"."</div>"."\n";
}
echo "</body>"."\n"."</html>";
?>
txt.php:
<?php
$mess = array("This is Div 1. I have filled it with content to check that this all works and I'm not an idiot. Have a nice day. Div 1 over and out.", "This is Div 2. I have filled it with content to check that this all works and I'm not an idiot. Have a nice day. Div 2 over and out.", "This is Div 3. I have filled it with content to check that this all works and I'm not an idiot. Have a nice day. Div 3 over and out.");
?>
I'm sure the answer is stupidly simple but I can't figure it out
Cheers
ollie
I am trying to load a value from a randomly generated postion in an array in an external PHP file and echo it to the screen inside a generated div.
The text doesn't appear on the page and is missing in the source of the final page. I have managed to do exactly the same thing in a different file but not inside a div and these files are in the same directory with the code being identical.
The online example is here:
http://www.olliesilviotti.co.cc/php_stuff
(the second submit button (next to the combo box) executes the script I'm talking about)
Here's the relevant source's:
index.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include("txt.php"); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Stuff</title>
</head>
<body>
<form method="get" action="phpscript.php">
Name: <input type="text" name="nameBox" />
<input type="submit" name="btn1" value="Submit"/>
</form>
<br /><br />
<hr>
<form method="post" action="phpscript2.php">
<select name="combo_no">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" name="submitbtn" value="Submit" />
</form>
<hr>
<?php
$randval = rand(0, 2);
$imglist = array("red", "blue", "green");
echo "<img src='images/".$imglist[$randval].".jpg' />";
?>
<hr>
<?php $randmessval = rand(0,2); echo $mess[$randmessval]; ?>
</body>
</html>
phpscript2.php:
<?php
$no_divs = $_POST['combo_no'];
echo "<html>"."\n"."<head>"."\n"."<link rel='text/css' type='stylesheet' src='script2style.css'/>"."\n"."<title>PHP Stuff - Script 2 - DIVs</title>"."\n"."<?php include(\"txt.php\"); ?>"."\n"."</head>"."\n"."<body>"."\n";
for($i = 0; $i < $no_divs; $i++)
{
echo "<div id='div".$i."'".">"."\n";
echo "<?php echo $mess[0]; ?>";
echo "\n"."</div>"."\n";
}
echo "</body>"."\n"."</html>";
?>
txt.php:
<?php
$mess = array("This is Div 1. I have filled it with content to check that this all works and I'm not an idiot. Have a nice day. Div 1 over and out.", "This is Div 2. I have filled it with content to check that this all works and I'm not an idiot. Have a nice day. Div 2 over and out.", "This is Div 3. I have filled it with content to check that this all works and I'm not an idiot. Have a nice day. Div 3 over and out.");
?>
I'm sure the answer is stupidly simple but I can't figure it out
Cheers
ollie