views:

101

answers:

4

Hi I'm trying to make some form send back to the person who fill up these forms as a references. However I keep getting error in line 5. Which i don't see any problem. Can anyone guide me? Thanks!

<?php session_start(); 
require_once 'opendb.php'

$subject = "Refrence";
$refemail = "Here is the list for your refrence";
$class_name = $_POST['course_name'];
$class_venue = $_POST['class_venue'];
$date_of_lesson =$_POST['date_of_lesson'];
$time_of_lesson =$_POST['time_of_lesson'];
$class_duration =$_POST['class_duration'];
$date_of_exam =$_POST['date_of_exam'];
$class_in_charge =$_POST['class_in_charge'];    
$contact =$_POST['contact'];
$email =$_POST['email'];
$zone =$_POST['zone_no'];
$cl_name =$_POST['cl_name'];
$ce_name =$_POST['ce_name'];

$todayis = date("l, F j, Y, g:i a") ;

$message = " $todayis \n
From: $cl_name ($email)\n
Class name: $class_name \n
Class Venue: $class_venue \n
Date of lesson: $date_of_lesson \n
Time of lesson: $time_of_lesson \n
Class duration: $class_duration \n
Date of exam: $date_of_exam \n
Class in charge: $class_in_charge \n
Contact: $contact \n
Zone: $zone \n
Cl Name: $cl_name \n
Ce Name: $ce_name \n
";

$from = "From: $email\r\n";

mail($email, $subject, $refemail, $message, $from);
?>

<img style="width: 190px; height: 184px;" alt=""
src="images/Picture1.jpg">
<form enctype="multipart/form-data" action = 'function.php' method='post' />
</head>
<body>
<span
 style="text-decoration: underline; font-weight: bold;"> </span>

 <head><title>Success!</title></head>
<h1>Class Successfully Created !</h1>
<h3>Thank you. An email has been sent to your account for futher reference.</h3>

<?
// Read variables from the $_POST array


$i = 0;
while ($i < $_SESSION['numberOfStudents'])
{
$student_name = $_POST['student_name'.$i.''];
$classId = $_SESSION['class_id'];
$nric = $_POST['nric'.$i.''];
$age = $_POST['age'.$i.''];
$sql = "INSERT INTO b_student (bs_nric_no,student_name,age,b_class_id) VALUES 
      ('$nric','$student_name','$age','$classId')";

mysql_query($sql) or die(mysql_error());

$i++;
}


?>

<tr>
      <td></td>
      <table style="text-align: left;"
      <td><input type = 'submit' value = 'Back to Function List!'></td>

     </tr>
    </table>
</form>
</html>
</body>
</html>
+1  A: 

; is missing after require_once() in second line. Don't know why is says error at line 5, maybe you cleared out some comments?

usoban
I guess tat was the problem and now i get a new error. "undefine index for all variables"
Check how your POST looks like, just type var_dump($_POST);
usoban
Care to elaborate more? Sorry I'm not good in this..
$_POST is array and with var_dump() you will see if array actually has indexes you are trying to get, because obviously they don't exist or they have different names.
usoban
Because these post are stored on the database from the first site and 2nd site was to key in other data. While in 2nd page I load the Post from the first site. So in the 3rd site I can't get the post data I had previously hmm?I have posted 3 site and their code below this post.
A: 

I see lots of problems in the code snippet you posted, but the only one that comes to mind that could be around line 5 is that $_POST['course_name'] isn't set.

However, without knowing what the form that posted the values through looks like, it's impossible to say.

Fake51
Hmm.. means i missing out something?
A: 

The site comes in 3 set.

First site:

<?
session_start();
require_once 'opendb.php';
?>
<html>
<form action="student_details.php" method="post">
<img style="width: 190px; height: 184px;" alt=""
src="images/Picture1.jpg">

<body>
<span
 style="text-decoration: underline; font-weight: bold;"> </span><big
 style="font-weight: bold;"><big><big><big>Create New Class</big></big></big></big>
 </head>

<?


      echo "<td><h3>Application for Registration of
      <select name='course_name' id='drop_down'></td><h3>";
      $sql= "SELECT DISTINCT course_name FROM c_course";
            $result=mysql_query($sql);
      echo "</tr>";

      while  ($row = mysql_fetch_assoc($result))
      {
      echo "<option>";
      echo $row['course_name'];
      echo "</option>";

      }
      echo "</td>";
      echo "</tr>";


          ?>



    <table>

     <tr>
           <td>Venue of Class:</td>
      <td><input type = 'text' name = 'class_venue'></td>
      </tr>
     <tr>
      <td>Date of Commencement:</td>
      <td><input type = 'text' name = 'date_of_lesson'></td>
     </tr>
     <tr>
     <td>Other Dates of Lessons:</td>
     <td><input type = 'text' name = 'other_lessons'></td>
     <td><input type = 'text'  name = 'other_lessons1'></td>

     <tr>
         <td>Time:</td><td><select name="time_of_lesson" id="drop_down">
       <option value="0700">0700</option> 
             <option value="0800">0800</option>
             <option value="0900">0900</option>
      </tr>
      <tr>
       <td> Class Duration:</td><td><select name="class_duration" id="drop_down">
       <option value="1">1</option> 
             <option value="2">2</option>
             <option value="3">3</option>
       <option value="4">4</option> 
             <option value="5">5</option>
             <option value="6">6</option>

       </tr>
       <tr>
       <td>Tentative Date of Exam:</td>
      <td><input type = 'text'  name = 'date_of_exam'></td>
         <td>(DD-MM-YYYY)</td>
     </tr>

     <tr>
      <td>Class-In-Charge:</td>
      <td><input type = 'text' name = 'class_in_charge'></td>
      </td>
      <tr>
      <td>Contact No:</td>
      <td><input type = 'text' name = 'contact'></td>
      </tr>
      <tr>
      <td>Email:</td>
      <td><input type = 'text' name = 'email'></td>
      </tr>
<?

$i=1;
      echo "<tr>";
      echo "<td><u>Choose Lecturer:</u></td>";
      echo "</tr>";



      echo "<tr>";
      echo "<td>Zone:</td>";
      echo "<td><select name='zone_no' id='drop_down'>";
      $sql= "SELECT DISTINCT zone_no FROM c_lecturer";
            $result=mysql_query($sql);

      while  ($row = mysql_fetch_assoc($result))
      {
      echo "<option>";
      echo $row['zone_no'];
      echo "</option>";

      }
      echo "</td>";
      echo "</tr>";


      echo "<tr>";
      echo "<td>Lecturer:</td>";
      echo "<td><select name='cl_name' id='drop_down'>";
      $sql= "SELECT cl_name FROM c_lecturer";
            $result=mysql_query($sql);

      while  ($row = mysql_fetch_assoc($result))
      {
      echo "<option>";
      echo $row['cl_name'];
      echo "</option>";

      }
      echo "</td>";
      echo "</tr>";



            echo "<tr>";
      echo "<td>Examiner:</td>";
      echo "<td><select name='ce_name' id='drop_down'>";
      $sql= "SELECT ce_name FROM c_examiner";
            $result=mysql_query($sql);

      while  ($row = mysql_fetch_assoc($result))
      {
      echo "<option>";
      echo $row['ce_name'];
      echo "</option>";

      }
      echo "</td>";
      echo "</tr>";
?>
<br>
<br>
      <tr>
     <td><input type = 'submit' name="StudentDetails" value = "Continue"></td>
     </tr>
    </table>
</form>
</body>
</html>

Second one

<?php
error_reporting(E_ALL ^ E_NOTICE);

session_start();
require_once 'opendb.php';

if (isset($_POST['AddStudent'])) {
// If user click on Add new student button 
    $temp = $_SESSION['numberOfStudents'];
    $temp = $temp + 1;
    $_SESSION['numberOfStudents'] = $temp;
}
/*else if (isset($_POST['FinalCheckBtn'])) {
// If user click on Final Check button go to Final check page
header('Location: http://localhost/SJAA/final_check.php') ;
}*/
else {
// if User enter this page the first time from "create new class page " 
    // the variable that hold the credentials to connect to the database

    // Read variables from the $_POST array
    $class_name = $_POST['course_name'];
    $class_venue = $_POST['class_venue'];
    $date_of_lesson =$_POST['date_of_lesson'];
    $time_of_lesson =$_POST['time_of_lesson'];
    $class_duration =$_POST['class_duration'];
    $date_of_exam =$_POST['date_of_exam'];
    $class_in_charge =$_POST['class_in_charge']; 
    $contact =$_POST['contact'];
    $email =$_POST['email'];
    $zone =$_POST['zone_no'];
    $cl_name =$_POST['cl_name'];
    $ce_name =$_POST['ce_name'];

    // Prepare the query

    $sql = "INSERT INTO b_class (class_name,class_venue,date_of_lesson,time_of_lesson,class_duration,date_of_exam,class_in_charge,contact,email,zone_no, cl_name,ce_name) VALUES 
       ('$class_name','$class_venue','$date_of_lesson','$time_of_lesson','$class_duration','$date_of_exam','$class_in_charge','$contact','$email','$zone_no','$cl_name','$ce_name')";
    // Execute the query
    mysql_query($sql) or die(mysql_error());
    $_SESSION['numberOfStudents'] = 0;
    $_SESSION['class_name']=$_POST['course_name'];
    $_SESSION['class_venue'] = $_POST['class_venue'];
    $_SESSION['date_of_lesson'] = $_POST['date_of_lesson'];
    $_SESSION['time_of_lesson'] = $_POST['time_of_lesson'];
    $_SESSION['class_duration'] = $_POST['class_duration'];
    $_SESSION['date_of_exam'] = $_POST['date_of_exam'];
    $_SESSION['class_in_charge'] = $_POST['class_in_charge'];
    $_SESSION['contact'] = $_POST['contact'];
    $_SESSION['email'] = $_POST['email'];
    $_SESSION['zone_no'] = $_POST['zone_no'];
    $_SESSION['cl_name'] = $_POST['cl_name'];
    $_SESSION['ce_name'] = $_POST['ce_name'];



    // Prepare query to get the new class id 
    $sql = "select max(b_class_id) as ClassId from b_class";
    $result = mysql_query($sql) or die(mysql_error());
    if (mysql_num_rows($result) > 0) {
     $row = mysql_fetch_row($result);
     $_SESSION['class_id'] = $row[0]; 
    }


}
?>
<html>
<form enctype="multipart/form-data" action = 'student_details.php' method='post'>
<img style="width: 190px; height: 184px;" alt="" src="images/Picture1.jpg">

<head><title>Students' Particulars</title></head>
<h1>Students' Particulars</h1>
<h3> Please enter student's particulars. All fields are to be filled. </h3>
<table style="text-align: left; width: 333px; height: 144px;"
 border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td><?php 
echo "Class Name: "; 
echo $_SESSION['class_name'];?> </td>
</tr>
<tr>
      <td><?php 
echo "Class Id: "; 
echo $_SESSION['class_id'];?> </td>
</tr>
    <tr>
      <td><?php 
echo "Class Venue: "; 
echo $_SESSION['class_venue'];?> </td>
</tr>
<tr>
      <td><?php 
echo "Date of Commencement: "; 
echo $_SESSION['date_of_lesson'];?> </td>
</tr>
<tr>
      <td><?php 
echo "Time: "; 
echo $_SESSION['time_of_lesson'];?> </td>
</tr>
<tr>
      <td><?php 
echo "Class Duration: "; 
echo $_SESSION['class_duration'];?> </td>
</tr>
<tr>
      <td><?php 
echo "Tentative Date of Exam: "; 
echo $_SESSION['date_of_exam'];?> </td>
</tr>
<tr>
      <td><?php 
echo "Class-In-Charge: "; 
echo $_SESSION['class_in_charge'];?> </td>
</tr>
<tr>
      <td><?php 
echo "Contact: "; 
echo $_SESSION['contact'];?> </td>
</tr>
<tr>
      <td><?php 
echo "Email: "; 
echo $_SESSION['email'];?> </td>
</tr>
<tr>
   <td><?php 
echo "Zone: "; 
echo $_SESSION['zone_no'];?> </td>
</tr>
<tr>
   <td><?php 
echo "Lecturer: "; 
echo $_SESSION['cl_name'];?> </td>
</tr>
<tr>
   <td><?php 
echo "Examiner: ";
echo $_SESSION['ce_name'];?> </td>
</tr>

</tr>
<body>
<table border="1">
<tr>
<th>Name</th>
<th>NRIC no.</th>
<th>Age</th>
</tr>
<br>
<input type="submit" value="Add new Student" name="AddStudent">
</form>
<?php 
$cnt = 0;
echo "<form enctype='multipart/form-data' action = 'final_check.php' method='post'>";
while ($cnt < $_SESSION['numberOfStudents']) {


    $NameCnt = "Name".$cnt;
    $NRICcnt = "NRICno".$cnt;
    $AgeCnt = "Age".$cnt;

    if (isset($_POST[$NameCnt])) {
?> 
     <tr>
     <td><input type = 'text' name = 'student_name<?php echo $cnt ?>' value='<?php echo $_POST[$NameCnt] ?>'></td>
     <td><input type = 'text' name = 'nric<?php echo $cnt ?>' value='<?php echo $_POST[$NRICcnt] ?>'></td>
     <td><input type = 'text' name = 'age<?php echo $cnt ?>' value='<?php echo $_POST[$AgeCnt] ?>'></td>
     </tr> 
<?php
    }
    else {
?> 
     <tr>
     <td><input type = 'text' name = 'student_name<?php echo $cnt ?>'></td>
     <td><input type = 'text' name = 'nric<?php echo $cnt ?>'></td>
     <td><input type = 'text' name = 'age<?php echo $cnt ?>'></td>
     </tr>
<?php
    }

$cnt = $cnt +1;
}
?>

</table>
<tr>
      <td></td>
      <table style="text-align: left;"
      <td><input type = 'submit' value = 'Submit' name='FinalCheckBtn'></td>

     </tr>
    </table>
</form>
</html>

Final one is the one on my first post.

A: 

I'm quite sure the form tag must come after the body tag, but that's minor issue. Your second set of code appends a number at the end of each post variable, but your last piece of code don't seems to reflect that.

Instead of using numbers at the end of each form variable, use array instead, like this

<input type='text' name='nric[]'>

It will become an which you can access via

$nric = $_POST["nric"];
echo "<pre>".print_r($nric, true)."</pre>";
Extrakun
I see... But what if I just want to mail the first table back to the user who key these information? $class_name and etc.. Not the student form.Because my feedback code doesn't work. It say I have undefine index for all variables.