Hello ,
Could somebody help me with these 2 codes. I am very new in PHP. I intend to have a form where visitor could enter their data and click submit button which opens another page where the information submitted are displayed. Visitor could have opportunity of returning back to the form page and make correction if there is need. He could as well decides to continue with the registration by uploading his picture and submit the data. This would in turn diplays his information and the uploaded picture. The problem I encountered was the inability of the picture to display instead I get X. All other information however displayed. The picture also failed to move into the image directory created for that purpose. The photo field in the database is being populated with random number any time the transaction is carried out. I also observed that the "id" number increases anytime the displayed page is refreshed . The implication of this is that a visitor could be assigned 2 or more id numbers. This is not what I intend to achieve with the code. i am looking forward for any workable solution to this problem. The codes are below:
index4.php (form)
<?php
session_start();
if(!isset($_SESSION['Fname'])) {
echo "
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method=\"post\" action=\"http://localhost/army/preview4.php\">
First Name:<input type=\"text\" size=\"12\" maxlength=\"12\" name=\"Fname\"><br />
Last Name:<input type=\"text\" size=\"12\" maxlength=\"36\" name=\"Lname\"><br />
Gender:<br />
Male:<input type=\"radio\" value=\"Male\" name=\"gender\"><br />
Female:<input type=\"radio\" value=\"Female\" name=\"gender\"><br />
Select a Level of Education:<br />
<select name=\"education\">
<option value=\"Jr.High\">Jr.High</option>
<option value=\"HighSchool\">HighSchool</option>
<option value=\"College\">College</option></select><br />
Select your favorite time of day:<br />
<select name=\"TofD\" size=\"3\">
<option value=\"Morning\">Morning</option>
<option value=\"Day\">Day</option>
<option value=\"Night\">Night</option></select><br />
<input type=\"submit\" value=\"submit\" name=\"submit\">
</form>
</body></html>";
} else {
$Fname = $_SESSION['Fname'];
$Lname = $_SESSION['Lname'];
$gender = $_SESSION['gender'];
$quote = $_SESSION['quote'];
$education = $_SESSION['education'];
$TofD = $_SESSION['TofD'];
echo "
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method=\"post\" action=\"http://localhost/army/preview4.php\">
First Name:<input type=\"text\" size=\"12\" maxlength=\"12\" name=\"Fname\" value=\"$Fname\"><br />
Last Name:<input type=\"text\" size=\"12\" maxlength=\"36\" name=\"Lname\" value=\"$Lname\"><br />
Gender:<br /> ";
if($gender=="Male") {
echo "Male:<input type=\"radio\" value=\"Male\" name=\"gender\" checked><br /> ";
} else {
echo "Male:<input type=\"radio\" value=\"Male\" name=\"gender\"><br />";
}
if($gender=="Female") {
echo "Female:<input type=\"radio\" value=\"Female\" name=\"gender\" checked><br />";
} else {
echo "Female:<input type=\"radio\" value=\"Female\" name=\"gender\"><br />";
}
echo "
Select a Level of Education:<br />
<select name=\"education\">";
if($education=="Jr.High") {
echo "<option selected value=\"Jr.High\">Jr.High</option>";
} else {
echo "<option value=\"Jr.High\">Jr.High</option>";
}
if($education=="HighSchool") {
echo "<option selected value=\"HighSchool\">HighSchool</option>";
} else {
echo " <option value=\"HighSchool\">HighSchool</option>";
}
if($education=="College") {
echo "<option selected value=\"College\">College</option></select><br />";
} else {
echo "<option value=\"College\">College</option></select><br />";
}
echo "</select><p>
Select your favorite time of day:<br />
<select name=\"TofD\" size=\"3\"> ";
if($TofD=="Morning") {
echo "<option selected value=\"Morning\">Morning</option> ";
} else {
echo "<option value=\"Morning\">Morning</option> ";
}
if($TofD=="Day") {
echo "<option selected value=\"Day\">Day</option>";
} else {
echo "<option value=\"Day\">Day</option>";
}
if($TofD=="Night") {
echo "<option selected value=\"Night\">Night</option>";
} else {
echo "<option value=\"Night\">Night</option>";
}
echo "
</select><br />
<input type=\"submit\" value=\"submit\" name=\"submit\">
</form>
</body></html>";
}
?>
preview4.php
<?php
session_start();
if(!isset($_POST['insert'])) {
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$gender = $_POST["gender"];
$quote = $_POST["quote"];
$education = $_POST["education"];
$TofD = $_POST["TofD"];
//set the session values
$_SESSION['Fname'] = $Fname;
$_SESSION['Lname'] = $Lname;
$_SESSION['gender'] = $gender;
$_SESSION['education'] = $education;
$_SESSION['TofD'] = $TofD;
echo "Hello, $Fname $Lname<br />
You are $gender.<br />";
echo "<p><i>$quote</i><br />
You're favorite time is $TofD, and you passed $education!<br /><br />
If this information is incorrect, click the back button to make changes.
<form action=\"http://localhost/army/index4.php\" method=\"post\">
<input type=\"submit\" name=\"back\" value=\"Back\">
</form>
<form enctype= \"multipart/form-data\" action=\"preview4.php\" method=\"POST\">
Photo: <input type=\"file\" name=\"photo\"><br>
</form>
<form action=\"http://localhost/army/preview4.php\" method=\"post\">
<input type=\"submit\" name=\"insert\" value=\"Submit To Database\">
</form>";
} else {
//This is the directory where images will be saved
$ran = rand () ;
$ran2 = $ran.".";
$target = "images/";
$target = $target .$ran2. basename( $_FILES['photo']['name']);
$ok=1;
$Fname = $_SESSION['Fname'];
$Lname = $_SESSION['Lname'];
$gender = $_SESSION['gender'];
$quote = $_SESSION['quote'];
$education = $_SESSION['education'];
$TofD = $_SESSION['TofD'];
$photo=$ran2.($_FILES['photo']['name']);
include('connect.php');
mysql_query("INSERT INTO `favor`
(`Fname`, `Lname`, `gender`,`education`, `TofD`,`photo`) VALUES('$Fname', '$Lname', '$gender',
'$education', '$TofD','$photo') ")
or die(mysql_error());
$id= mysql_insert_id();
$query = "SELECT * FROM favor WHERE id=$id";
$result = mysql_query($query)or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<p>FORM NUMBER :62RRI/ <b>".$id."</b><br><br>";
echo "<img src=\"http://localhost/army/images/$photo\"> <br>";
echo "<p>Fame: <b>".$row['Fname']."</b><br>";
echo "<p>Lname: <b>".$row['Lname']."</b><br>";
echo "<p>gender: <b>".$row['gender']."</b><p>";
//echo"<a href=\"javascript:void(0);\" onclick=\"window.print();\">Print this Page</a>";
}
}
?>