Hey all,
I got a design question:
I want to design a script that can add details to a database, but the details are all going to be different:
For instance the user will enter details such as title, surname ect ect ect....
Once that is done, the user can add another set of details by clicking a button and another set of form with the same HTML elements embedded will appear...
So basically is there a way of doing this is PHP, or another design technique i can use to implement this?
Thanks in advance...
Design Suggestions
Moderator: General Moderators
One thing you can do is use javascript to have more form elements appear. So you would have one form setup, then if they clicked a button you would have javascript dynamically create another set of form elements.
To pass these to PHP you can use array naming in HTML like this
For the next set of inputs you create, you would name it mystuff[3]. When this form is posted to PHP you can now loop through it like this
That part is pretty simple. The javascript is going to be the difficult part but I am sure you can dig up a script.[/syntax]
To pass these to PHP you can use array naming in HTML like this
Code: Select all
<input name="mystuff[0][name]">
<input name="mystuff[0][address]">
<input name="mystuff[1][name]">
<input name="mystuff[1][address]">
<input name="mystuff[2][name]">
<input name="mystuff[2][address]">
Code: Select all
foreach ($_POST['mystuff'] as $person)
{
echo $person['name'].'<br />' ;
echo $person['address'].'<br />' ;
}Thanks for the suggestion:
This is what i have done so far:
what it doess is output the number of form elements based on the number that is entered, but my only problem is it cant tell the difference between the different form elements: Any suggestions???
This is what i have done so far:
what it doess is output the number of form elements based on the number that is entered, but my only problem is it cant tell the difference between the different form elements: Any suggestions???
Code: Select all
<?PHP include 'header.php'; ?>
<?PHP include 'opendb.php'; ?>
<?PHP
$ClientDetailID = $_GET['ClientDetailID'];
$numofpass = $_GET['numofpass'];
$seatsnumber = $_GET['seatsnumber'];
$FlightID = $_GET['FlightID'];
$status = "OK"; // setting the flag for form validation
$msg=""; // error message string is blank
// Now let us check if name is entered or not
if(strlen($seatsnumber) < 1 ){ // if name is less than two char length
$msg .="<center>Please select a return flight number</center><BR>";
$status="NOT OK";
}
// Now let us check if name is entered or not
if(strlen($FlightID) < 1 ){ // if name is less than two char length
$msg .="<center>Please select a flight</center><BR>";
$status="NOT OK";
}
if($status<>"OK"){ // if form validation is not passed
echo "<BR><BR>";
echo $msg. "<br><center><input type='button' value='Retry' onClick='history.go(-1)'></center><br><br><br>";
}else{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="JavaScript" src="javascript/ts_picker.js">
</script>
<script type="text/javascript" src="javascript/calendarDateInput.js">
</script>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?PHP
if($numofpass > $seatsnumber) {
echo "
<font size=+2>The number of seats left(<font color=#FF0000>$seatsnumber</font>)
is less than the seats requested (<font color=#FF0000>$numofpass</font>). Please
go back enter a number that equals or is less than the number of seats left.</font><br><br>
<input type='button' value='Back' onClick='history.go(-1)'> ";
} else {
?>
<form action="process_hotel.php" method="get" name="add_hotel">
<table width="85%" border="0" align="center">
<tr>
<td colspan="2"><strong>Client flight confirmation</strong></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
<?PHP
while ($numofpass>0) {
echo" <tr>
<td><strong>Title:</strong></td>
<td><select name=title>
<option value=></option>
<option value=Mr>Mr</option>
<option value=Mrs>Mrs</option>
<option value=Ms>Ms</option>
<option value=Master>Master</option>
</select></td>
</tr>
<tr>
<td><strong>First name:</strong></td>
<td><input type=text name=firstname></td>
</tr>
<tr>
<td height=32><strong>Surname:</strong></td>
<td><input type=text name=surname></td>
</tr>
<tr>
<td><strong>Child/Adult:</strong></td>
<td><select name=ChildAdult>
<option value=></option>
<option value=1>Child</option>
<option value=2>Adult</option>
</select></td>
</tr>
<tr>
<td><strong>Cost:</strong></td>
<td><input type=text name=cost></td>
</tr>
<tr>
<td width=17><strong>Notes:</strong></td>
<td width=83><textarea name=notes></textarea></td>
</tr>
<tr>
<td></td></tr>"
;
$numofpass = $numofpass-1;
}
?>
<tr>
<td colspan="2"> <input type="submit" name="submit" value="Confirm flight">
<input type="reset" name="Reset" value="Reset"> <INPUT type="button" value="Cancel" onClick="location.href='http://pb-sql/admin.php'">
</td>
</tr>
</table>
</form>
<?php }}?>
</body>
</html>
<?PHP include 'footer.php'; ?>