Jump to content

Archived

This topic is now archived and is closed to further replies.

stuart6521

php form help needed!!

Recommended Posts

Hi I am creating a grade caculation website and am having trouble with submitting a form on my page.
my code gives option to user to select his course of study. From this selection my code pulls all modules from that course from my database and displays it on the screen. This all works ok.

after i display all modules and module weight(which i need to calc the grade average) i have put in a drop down list where the user can select his grade value.
My problem is how to submit these values. I have tried using action=$_SERVER[PHP_SELF] but this just reloads my origianl page where the user can select his course so then i have value of grade but lose info i have got from database.
I have also tried to post form to new php doc but then i also dont have values i have taken from database.

So i would like to know is there a way to submit the form to a php function where i can do calculation or a way to submit form without reloading page???

The last 20 or so lines of code are where i am havng diffficulty. I have posted my code below.

hope this makes some sense!! any help will be much appreciated.






//connect to database
$conn = mysql_connect("localhost", "stuart", "stuart") or die(mysql_error());
mysql_select_db("stuart",$conn) or die(mysql_error());

if ($_POST[op] != "view") {
//haven't seen the selection form, so show it
$display_block = "<h1>Select Your course</h1>";

//get parts of records
$get_list = "select course_id, concat_ws(', ', course_name) as display_name from course";

$get_list_res = mysql_query($get_list) or die(mysql_error());

if (mysql_num_rows($get_list_res) < 1) {
//no records
$display_block .= "<p><em>Sorry, no records to select!</em></p>";

} else {
//has records, so get results and print in a form

$display_block .= "

<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
<P><strong>Select a Record to View:</strong><br>
<select name=\"sel_id\">
<option value=\"\">-- Select One --</option>";


while ($recs = mysql_fetch_array($get_list_res)) {
$id = $recs['course_id'];
$display_name = stripslashes($recs['display_name']);

$display_block .= "<option value=\"$id\">$display_name</option>";
}
$display_block .= "
</select>
<input type=\"hidden\" name=\"op\" value=\"view\">
<p><input type=\"submit\" name=\"submit\" value=\"View Modules\"></p>
</FORM>";


}


} else if ($_POST[op] == "view") {

//check for required fields
if ($_POST[sel_id] == "") {
header("Location: selcourse.php");
exit;
}


//get master_info
$get_master = "select course_name as display_name from course where course_id = $_POST[sel_id]";
$get_master_res = mysql_query($get_master);
$display_name = stripslashes(mysql_result($get_master_res, 0,'display_name'));
$display_block = "<h1>Showing Modules for $display_name</h1>";



//get all module for semester 1
$get_modules = "select mod_code, mod_weight from module where course_id = $_POST[sel_id] && sem_num = 1";
$get_modules_res = mysql_query($get_modules);

if (mysql_num_rows($get_modules_res) > 0) {



$display_block .= "
<table celpadding=3 cellspacing=2 border=1 width=98%>
<tr>
<th>Semester 1</th>
<tr>
<tr>
<th>Course code</th>
<th>Weight</th>
<th>Result</th>
</tr>";


$sem1=0;
$modres=1;

while ($add_info = mysql_fetch_array($get_modules_res)) {



$modname = $add_info[mod_code];
$modweight = $add_info[mod_weight];
$sumcred1 = $sumcred1+$modweight;
$numvalue[$sem1] = $modweight;


$display_block .= "<form method=\"post\"action=\"$_SERVER[PHP_SELF]\" >";



$display_block .= "<tr>


<td align=left>$modname <br></td>
<td align=left>$modweight <br></td>


<td align=left>

<select name=\"$modres\">
<option value=\"\">-- Select Result --</option>
<option value=\"4.00\">A1</option>
<option value=\"3.60\">A2</option>
<option value=\"3.20\">B1</option>
<option value=\"3.00\">B2</option>
<option value=\"2.80\">B3</option>
<option value=\"2.60\">C1</option>
<option value=\"2.40\">C2</option>
<option value=\"2.00\">C3</option>
<option value=\"1.60\">D1</option>
<option value=\"1.20\">D2</option>
<option value=\"0.00\">F</option>
<option value=\"0.00\">NG</option>
</select>

<br></td>



";
$sem1++;
$modres++;
}



$display_block .= "<td><input type=\"submit\" name=\"submit\" value=\"calculate\"></td></tr>
</form>";

}

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.