Right I have a big issue with a form that I am trying to use. I initially created a php form which works fine apart from one thing....due to me having a drop down menu that uses a reload function, this means that the page is refreshing all the time and thus any values that I have stored in the text fields are disappearing. Is there some way of keeping the text fields values even when the menu changes.My code is as below and its the name, email, login etc fields that are causing the problem.
<?php
// username, password and database name for making connection
$username="root";
$password="michael1";
$database="learningcentres";
//makes connection or outputs error message if connection failes
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
// Execute a SQL query against the database, fetch all the result rows into an
// array and return the array.
//
// It takes one parameter, the SQL query string to be executed
//
// It returns an array of query result rows. The returned array is numerically
// indexed, the first result row being at 0, the second result at 1 etc. Each
// element in the array is also an array, the result of the call to
// mysql_fetch_array(), which is indexed both numerically (0 being first column,
// 1 being second etc.) and also by column name.
//
// If the query returns no result, this function returns an empty array.
// If the query fails, the script will die() with an error message.
//
// Example code:
// $results = select_into_array( "SELECT name, description FROM table" );
// foreach($results as $row) {
// echo "Name: $row[name]\n";
// echo "Description: $row[1]\n";
// }
function select_into_array( $query ) {
// start with an empty results array
$results = array();
// attempt to execute the query
$result_handle = mysql_query( $query )
or die( "<p>SQL Error<br/>$query<br/>" . mysql_error() . "</p>" );
// the query executed successfully, so fetch all the result rows and
// append to the results array
while( $result = mysql_fetch_array( $result_handle ) )
$results[] = $result;
// Although not strictly necessary, be nice to the server and free the
// resources used by the query. It will definately not be needed again.
@mysql_free_result( $result_handle ); // @ means "fail silently"
// return the results array
return $results;
}
?>
<html>
<head>
<title>course input form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.sector.options[form.sector.options.selectedIndex].value;
self.location='?sector=' + val;
}
function reload1(form)
{
var val=form.sector.options[form.sector.options.selectedIndex].value;
var val2=form.course.options[form.course.options.selectedIndex].value;
self.location='?sector=' + val + '&course=' + val2 ;
}
</script>
</head>
<body style="text-align:center">
<?php
// checks to see if the values have been set for all text fields
//delcare all posts as variables
$forename = $_POST['firstname'];
$surname = $_POST['surname'];
$tel = $_POST['telephone'];
$email = $_POST['email'];
$vlelogin = $_POST['vlelogin'];
if(isset ($_POST['firstname']) && isset($_POST['surname']) && isset($_POST['telephone']) && isset($_POST['email']) && isset($_POST['vlelogin']))
{
// sql insert query for the variables into the delegate table
$query = "INSERT INTO delegate (forename, surname, telephone, email, vlelogin ) VALUES ('$forename', '$surname', '$tel', '$email', '$vlelogin')";
// sql query either processes or fails and outputs an error message
mysql_query($query) or die ('Error, insert into query failed');
}
?>
<!-- here is the form that will be output to the users -->
<form method="post" name="form1" action = "">
<table style="width=:800px" border="1" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><image src="clcs_logo.jpg" alt=""/></td>
</tr>
<tr>
<td colspan="2"><h4>Sheffield City Learning Centres Online Course Booking Form</h3></td>
</tr>
<!-- delegate firstname detaisl go here -->
<tr>
<td style="width:150px">Firstname</td>
<td width="100"><input name="firstname" type="text" id="first" size="30"</td>
</tr>
<!-- delegate surname details go here -->
<tr>
<td width="100">Surname</td>
<td><input name="surname" type="text" id="last" size="30"></td>
</tr>
<!-- delegate temephone details go here -->
<tr>
<td witdh="100">Telephone</td>
<td><input name="telephone" type="text" id="tel" size="30"></td>
</tr>
<!-- delegate e-mail address goes here -->
<tr>
<td width="100">Email</td>
<td><input name="email" type="text" id="email" size="30"></td>
</tr>
<!-- delegate vle login name goes here -->
<tr>
<td width="100">VLE Login</td>
<td><input name="vlelogin" type="text" id="vle" size="30"></td>
</tr>
<!-- delegate gender goes here -->
<tr>
<td width="100">Gender</td>
<td>
<?php
$genderresults = mysql_query("SELECT GenderType, GenderID FROM gender");
echo "<select name ='gender'>";
while ($row = mysql_fetch_assoc($genderresults)) {
echo '<option value="'.$row['GenderID'].'">'.$row['GenderType'].'</option>';
}
echo "</select>";
?>
</td>
</tr>
<!-- delegates role in the schol goes here -->
<tr>
<td width="100">Role in school</td>
<td>
<?php
$roleresults = mysql_query("SELECT RoleType, RoleID FROM role");
echo "<select name ='roles'>";
while ($row = mysql_fetch_assoc($roleresults)) {
echo '<option value="'.$row['RoleID'].'">'.$row['RoleType'].'</option>';
}
echo "</select>";
?>
</td>
</tr>
<!-- any dietary requirements go here -->
<tr>
<td width="100">Dietary Requirements</td>
<td>
<?php
$dietaryresults = mysql_query("SELECT DietaryType, DietaryID FROM dietary");
echo "<select name ='diet'>";
while ($row = mysql_fetch_assoc($dietaryresults)) {
echo '<option value="'.$row['DietaryID'].'">'.$row['DietaryType'].'</option>';
}
echo "</select>";
?>
</td>
</tr>
<!-- details from which institution the deelgate comes from are selected here -->
<tr>
<td>Institution</td>
<td>
<?php
// Is this script being run for; the first time, the second time after the
// user selects from the first menu, or the third time after the user has
// clicked on the first, then the second menus.
// set some variables from the user-supplied parameters
// first is coursecoee second is eventid
if( isset($_REQUEST['sector']) ) $sectorname = $_REQUEST['sector'];
if( isset($_REQUEST['institution']) ) $instname = $_REQUEST['institution'];
?>
<?php
// get items for first menu
$menuitems = select_into_array( "SELECT SectorType, SectorID FROM sector ORDER BY sector.SectorID ASC" );
// output the first menu
echo "<h5>From which sector do they belong to?</h5>";
echo "<select id='sector' name='sector' onchange=\"reload(this.form)\"><option value=''>Select One</option>";
foreach($menuitems as $item) {
$selected = ($item['SectorID']==$sectorname) ? ' selected' : '';
echo "<option value='$item[SectorID]'$selected>$item[SectorType]</option>";
}
echo "</select>";
?>
<?php
if( $sectorname && !$instname ) {
// get items for second menu
$menuitems = select_into_array( "SELECT institution.InstitutionName, institution.InstitutionID "
."from sector, institution where sector.SectorID = institution.SectorID and sector.SectorID = $sectorname "
."order by institution.InstitutionName ASC");
// output the second menu
echo "<h5>Which institution within this sector?</h5>";
echo "<select id='institution' name ='institution'><option value=''>Select One</option>";
foreach($menuitems as $item) {
echo "<option value='$item[InstitutionID]'>$item[InstitutionName]</option>";
}
echo "</select>";
}
?>
</td>
</tr>
<tr>
<td>Couse Details</td>
<td>
<?php
// Is this script being run for; the first time, the second time after the
// user selects from the first menu, or the third time after the user has
// clicked on the first, then the second menus.
// set some variables from the user-supplied parameters
// first is coursecoee second is eventid
if( isset($_REQUEST['course']) ) $coursename = $_REQUEST['course'];
if( isset($_REQUEST['fourth']) ) $event = $_REQUEST['fourth'];
?>
<?php
// get items for first menu
$menuitems = select_into_array( "SELECT CourseName AS name, CourseCode AS code FROM course ORDER BY name ASC" );
// output the first menu
echo "<h5>Please select the desired course</h5>";
echo "<select id='course' name='course' onchange=\"reload1(this.form)\"><option value=''>Select One</option>";
foreach($menuitems as $item) {
$selected = ($item['code']==$coursename) ? ' selected' : '';
echo "<option value='$item[code]'$selected>$item[name]</option>";
}
echo "</select>";
?>
<?php
if( $coursename && !$event ) {
// get items for second menu
$menuitems = select_into_array( "select e.EventID, DATE_FORMAT(e.EventDate,'%d-%b-%Y') as date , l.ClcName as name, e.ClcCode AS code "
."from event as e, location as l where e.ClcCode = l.ClcCode and e.CourseCode = $coursename "
."order by e.EventDate, l.ClcName");
// output the second menu
echo "<h5>Please select the desired date and location</h5>";
echo "<select id='date' name ='date'><option value=''>Select One</option>";
foreach($menuitems as $item) {
echo "<option value='$item[EventID]'>$item[date] at $item[name]</option>";
}
echo "</select>";
}
?>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="add" value="Add New User"></td>
</tr>
</table>
</form>
</body>
</html>
I also have this code below which partly precvents the problem but because it uses javascript I cant proces the form and then have another page showing me the results i.e. the action of ction='javascript_newcode_mastercopy.php'> always has to be the same file due to onchange='this.form.submit()'
I really dont know how to bring both functions together so that the value in each text field is restored but the form will also submit.
but then there is this code which stored the value but I can't submit the form
<?php
error_reporting(E_ALL);
include("db.inc");
function select_into_array( $query ) {
// start with an empty results array
$results = array();
// attempt to execute the query
$result_handle = mysql_query( $query )
or die( "<p>SQL Error<br/>$query<br/>" . mysql_error() . "</p>" );
// the query executed successfully, so fetch all the result rows and
// append to the results array
while( $result = mysql_fetch_array( $result_handle ) )
$results[] = $result;
// Although not strictly necessary, be nice to the server and free the
// resources used by the query. It will definately not be needed again.
@mysql_free_result( $result_handle ); // @ means "fail silently"
// return the results array
return $results;
}
?>
<html>
<head>
<title>course input form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT language="JavaScript">
function reload(form)
{
document.form1.action = "?action=update";
document.form1.submit();
}
function submitForm(form)
{
document.form1.action = "?action=submit";
document.form1.submit();
}
</script>
</head>
<body>
<?php
//delcare all posts as variables
$forename = $_POST['firstname'];
$surname = $_POST['surname'];
$tel = $_POST['telephone'];
$email = $_POST['email'];
$vlelogin = $_POST['vlelogin'];
if ($action == "submit") {
if(isset ($_POST['firstname']) && isset($_POST['surname']) && isset($_POST['telephone']) && isset($_POST['email']) && isset($_POST['vlelogin']))
{
// sql insert query for the variables into the delegate table
$query = "INSERT INTO delegate (forename, surname, telephone, email, vlelogin ) VALUES ('$forename', '$surname', '$tel', '$email', '$vlelogin')";
// sql query either processes or fails and outputs an error message
mysql_query($query) or die ('Error, insert into query failed');
}
header("Location: http://127.0.0.1/allononepage_test_results.php");
}
?>
<!-- here is the form that will be output to the users -->
<form method="post" name="form1" action = "">
<table width="400" border="1" cellpadding="0" cellspacing="0">
<!-- delegate firstname detaisl go here -->
<tr>
<td width="100">Firstname</td>
<td width="100"><input name="firstname" type="text" id="first" size="30" value="<?=$forename;?>"></td>
</tr>
<!-- delegate surname details go here -->
<tr>
<td width="100">Surname</td>
<td><input name="surname" type="text" id="last" size="30" value="<?=$surname;?>"></td>
</tr>
<!-- delegate temephone details go here -->
<tr>
<td witdh="100">Telephone</td>
<td><input name="telephone" type="text" id="tel" size="30" value="<?=$telephone;?>"></td>
</tr>
<!-- delegate e-mail address goes here -->
<tr>
<td width="100">Email</td>
<td><input name="email" type="text" id="email" size="30" value="<?=$email;?>"></td>
</tr>
<!-- delegate vle login name goes here -->
<tr>
<td width="100">VLE Login</td>
<td><input name="vlelogin" type="text" id="vle" size="30" value="<?=$vlelogin;?>"></td>
</tr>
<!-- delegate gender goes here -->
<tr>
<td width="100">Gender</td>
<td>
<?php
$genderresults = mysql_query("SELECT GenderType, GenderID FROM gender");
echo "<select name ='gender'>";
while ($row = mysql_fetch_assoc($genderresults)) {
$selected = ($row['GenderID']==$gender) ? ' selected' : '';
echo '<option value="'.$row['GenderID'].'" $selected>'.$row['GenderType'].'</option>';
}
echo "</select>";
?>
</td>
</tr>
<!-- delegates role in the schol goes here -->
<tr>
<td width="100">Role in school</td>
<td>
<?php
$roleresults = mysql_query("SELECT RoleType, RoleID FROM role");
echo "<select name ='roles'>";
while ($row = mysql_fetch_assoc($roleresults)) {
$selected = ($row['RoleID']==$roles) ? ' selected' : '';
echo '<option value="'.$row['RoleID'].'" $selected>'.$row['RoleType'].'</option>';
}
echo "</select>";
?>
</td>
</tr>
<!-- any dietary requirements go here -->
<tr>
<td width="100">Dietary Requirements</td>
<td>
<?php
$dietaryresults = mysql_query("SELECT DietaryType, DietaryID FROM dietary");
echo "<select name ='diet'>";
while ($row = mysql_fetch_assoc($dietaryresults)) {
$selected = ($row['DietoryID']==$diet) ? ' selected' : '';
echo '<option value="'.$row['DietaryID'].' $selected">'.$row['DietaryType'].'</option>';
}
echo "</select>";
?>
</td>
</tr>
<!-- details from which institution the deelgate comes from are selected here -->
<tr>
<td>Institution</td>
<td>
<?php
// Is this script being run for; the first time, the second time after the
// user selects from the first menu, or the third time after the user has
// clicked on the first, then the second menus.
// set some variables from the user-supplied parameters
// first is coursecoee second is eventid
if( isset($_REQUEST['sector']) ) $sectorname = $_REQUEST['sector'];
if( isset($_REQUEST['institution']) ) $instname = $_REQUEST['institution'];
?>
<?php
// get items for first menu
$menuitems = select_into_array( "SELECT SectorType, SectorID FROM sector ORDER BY sector.SectorID ASC" );
// output the first menu
echo "<h5>From which sector do they belong to?</h5>";
echo "<select id='sector' name='sector' onchange=\"reload(this.form)\"><option>Select One</option>";
foreach($menuitems as $item) {
$selected = ($item['SectorID']==$sectorname) ? ' selected' : '';
echo "<option value='$item[SectorID]'$selected>$item[SectorType]</option>";
}
echo "</select>";
?>
<?php
if( $sectorname && !$instname || $action == "update" ) {
// get items for second menu
$menuitems = select_into_array( "SELECT institution.InstitutionName, institution.InstitutionID "
."from sector, institution where sector.SectorID = institution.SectorID and sector.SectorID = $sectorname "
."order by institution.InstitutionName ASC");
// output the second menu
echo "<h5>Which institution within this sector?</h5>";
echo "<select id='institution' name ='institution'>";
foreach($menuitems as $item) {
$selected = ($item['InstitutionID']==$institution) ? 'selected' : '';
echo "<option value='$item[InstitutionID]' $selected>$item[InstitutionName]</option>";
}
echo "</select>";
}
?>
</td>
</tr>
<tr>
<td>Couse Details</td>
<td>
<?php
// Is this script being run for; the first time, the second time after the
// user selects from the first menu, or the third time after the user has
// clicked on the first, then the second menus.
// set some variables from the user-supplied parameters
// first is coursecoee second is eventid
if( isset($_REQUEST['course']) ) $coursename = $_REQUEST['course'];
if( isset($_REQUEST['fourth']) ) $event = $_REQUEST['fourth'];
?>
<?php
// get items for first menu
$menuitems = select_into_array( "SELECT CourseName AS name, CourseCode AS code FROM course ORDER BY name ASC" );
// output the first menu
echo "<h5>Please select the desired course</h5>";
echo "<select id='course' name='course' onchange=\"reload(this.form)\"><option>Select One</option>";
foreach($menuitems as $item) {
$selected = ($item['code']==$coursename) ? ' selected' : '';
echo "<option value='$item[code]'$selected>$item[name]</option>";
}
echo "</select>";
?>
<?php
if( $coursename) {
// get items for second menu
$menuitems = select_into_array( "select e.EventID, DATE_FORMAT(e.EventDate,'%d-%b-%Y') as date , l.ClcName as name, e.ClcCode AS code "
."from event as e, location as l where e.ClcCode = l.ClcCode and e.CourseCode = $coursename "
."order by e.EventDate, l.ClcName");
// output the second menu
echo "<h5>Please select the desired date and location</h5>";
echo "<select name ='date'><option value=''>";
foreach($menuitems as $item) {
$selected = ($item['EventID']==$date) ? 'selected' : '';
echo "<option value='$item[EventID]' $selected>$item[date] at $item[name]</option>";
}
echo "</select>";
}
?>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="add" value="Add New User" onClick="submitForm(this.form)"></td>
</tr>
</table>
</form>
</body>
</html>
I would appreciate any help/input/advice on offer please all most options have nearly been exhausted!