Several values for one field

I need to have a form selection with the following process:

  1. Open box
  2. Select choices with the following
<select style='height:40px;width:45px;font-size:11pt' name=\"code1\" value=\"$code1\" class=\"select\">
            <option value=\"0\">0</option>                      
            <option value=\"1\">1</option>   
            <option value=\"2\">2</option>
            <option value=\"3\">3</option>

            <select style='height:40px;width:120px;font-size:11pt' name=\"code1\" value=\"$code40\" class=\"select\">
            <option value=\"Adult(s)\">Adult(s)</option>                    
            <option value=\"Children\">Children (2-11yrs)</option>   
            <option value=\"Infant(s)\">Infant(s) (0-2yrs)</option>
Most likely will be a combination box of the two above. You can see a perfect example of the function on under Traveller and travel class

A family for example he can select 2 adult(s) / 2 Children / 1 Infant
3. selection madem it shows in the field with the result
4. Enter, it goes to the database $code1 field.

thank you for any assistance and help with this. DAvid

Your User Interface (UI) should be simple and general-purpose, allowing the user to enter/select data easily and for all likely situations (I know of families with more than 3 adults/children.)

What data do you expect? A quantity for each of the three categories? How about the following -

<form method='post'>
<label>Adult(s): <input type='number' name='qty[a]' value='0'></label><br>
<label>Children (2-11Yrs): <input type='number' name='qty[c]' value='0'></label><br>
<label>Infant(s) (Less than 2Yrs): <input type='number' name='qty[i]' value='0'></label><br>
<input type='submit'>

If you mean storing the three values in one database table column, don’t. Whatever this data represents (itinerary?), you should insert a separate row for each category that has a non-zero quantity and depending on how you need to track each person, you may want to insert a separate row for each person.

Thank you very much, the is not bad, and getting close. but yes it would go into my field $code1 as it is for now only a collector of info. Being it is part of many fields, it was easy for the passenger to select the field traveller, it opens the box, you select the number of passenegrs and then ok it displays in the field $code1 (1 adult / 1 child / 2 infants) as an exmaple. This way when it is uploaded, I will see exactly the details in one field.

I have compromised with what you listed but a result like 2 adults 1 child 3 infants does not go into my $code1 field

Cannot help with your code without having all the code needed to reproduce the problem.

Thank you for the message:
Joined the code and the data needs to go into $code40 when a passenger inserts the different choices for the flight:

	   echo "<form method=\"post\" action=\"addvariant.php\" ENCTYPE=\"multipart/form-data\" NAME=\"Add\" onSubmit=\"return verify();\">";

	   echo "<table width='900px' border='0' cellpadding='0' cellspacing='0' align='center'><tr><td class='bg2'>
	         <table width='100%' border='0' cellpadding='10' cellspacing='1' align='center'>
	         <td align='left' class='bg1'><font color='#027ECD' size='2' face='Verdana, Arial, Helvetica, sans-serif'>DEPART</font></td>
	         <td align='left' class='bg1'><font color='#027ECD' size='2' face='Verdana, Arial, Helvetica, sans-serif'>DATE</font></td>
	         <td align='left' class='bg1'><font color='#027ECD' size='2' face='Verdana, Arial, Helvetica, sans-serif'>PASSENGERS</font></td>
			 <tr class='bg1'>";

               $requete = $db->query("select title, affprix from ".$db->prefix("variant_categories")." where  cid=".$cid."");
               list($title, $affprix) = $db->fetch_row($requete);
               if($xoopsUser) {
               $iddd =$xoopsUser->getVar("uid", "E");
               $idd =$xoopsUser->getVar("name", "E");
               $idde =$xoopsUser->getVar("email", "E");
               $time = time();
	     echo "<td height='90'>
			   <input style='height:38px;width:120px;font-size:11pt' type=\"text\" class=\"text\" name=\"region\" size=\"10\" value=\"$regionfrom\" required><br />
			   <input style='height:38px;width:120px;font-size:11pt' type=\"text\" class=\"text\" name=\"town\" size=\"10\" value=\"$regionto\">
		       <td width='60'>
		       <input style='height:38px;width:135px;font-size:11pt' type=\"date\" class=\"text\" name=\"code13\" size=\"7\" value=\"$code13\" required><br />
			   <input style='height:38px;width:135px;font-size:11pt' type=\"date\" class=\"text\" name=\"code38\" size=\"7\" value=\"$code38\">


			   <select style='height:40px;width:100px;font-size:11pt' name=\"code8\" value=\"$code8\" class=\"select\">
               <option value=\"economy\">Economy</option>	
               <option value=\"ecopremium\">Ecopremium</option>
               <option value=\"Business\">Business</option>
               <option value=\"first\">First</option>
			   <label>Adults <input style='height:30px;width:43px;font-size:11pt' type='number' name='$code40' value='1' required></label>
               <label> Kids(-11yrs) <input style='height:30px;width:43px;font-size:11pt' type='number' name='$code40' value=''></label>
               <label>Infants (-2yrs) <input style='height:30px;width:43px;font-size:11pt' type='number' name='$code40' value=''></label>
			        <tr class='bg1'>
			        <td colspan='3' valign='top'>
			   <table border='0' cellpadding='5' cellspacing='0'>
               <td colspan='0' align='left' class='bg1'><font color='#027ECD' size='2' face='Verdana, Arial, Helvetica, sans-serif'>OPTIONS</font></td>
			   <td><input style='height:15px;width:15px;font-size:6pt' type=\"checkbox\" class=\"text\" name=\"code78\" value=\"+ Export\"> Export Assist</td>
			   <td><input style='height:15px;width:15px;font-size:6pt' type=\"checkbox\" class=\"text\" name=\"code79\" value=\"+ Customs\"> Customs</td>
			   <td><input style='height:15px;width:15px;font-size:6pt' type=\"checkbox\" class=\"text\" name=\"code80\" value=\"+ Taxi\"> Taxi</td>
			   <td><input style='height:15px;width:15px;font-size:6pt' type=\"checkbox\" class=\"text\" name=\"code81\" value=\"+ Hotel\"> Hotel</td>
			  echo "<br />";		

	   echo "<table width='900px' border='0' cellpadding='0' cellspacing='0' align='center'><tr><td valign='top'>
	                <table width='100%' border='0' cellpadding='0' cellspacing='0' align='center'><tr><td class='bg2'>
	                <table width='100%' border='0' cellpadding='10' cellspacing='1' align='center'>	
                    <td colspan='2' align='left' class='bg1'><font color='#027ECD' size='2' face='Verdana, Arial, Helvetica, sans-serif'>RECEIVE REFERENCE BY:</font></td>
			        <tr class='bg1'>
					<td width='40'></td>
					<td><input style='height:40px;width:140px;font-size:11pt' type='text' class='text' name='code16' value='$code16' placeholder='Email' required></td>
			        <tr class='bg1'>
					<td><input style='height:40px;width:140px;font-size:11pt' type='text' class='text' name='code10' value='$code10' placeholder='Whatsapp/SMS'></td>
	         <input type=\"hidden\" class=\"text\" name=\"submitter\" size=\"20\" value=\"$idd\"><br />
		     <input type=\"hidden\" class=\"text\" name=\"email\" size=\"20\" value=\"$idde\">
			 <input type=\"hidden\" name=\"cid\" value=\"$cid\">
			 <input type=\"hidden\" name=\"code11\" value=\"air-airline\">
			 <input type=\"hidden\" name=\"code27\" value=\"activate\">";
     	echo "</td><td width='150' align='center' valign='top'>";
			##########  ENTER  ########	
	   echo "<table width='100%' border='0' cellpadding='0' cellspacing='0' align='center'><tr><td class='bg2'>
	         <table width='100%' border='0' cellpadding='10' cellspacing='1' align='center'>				 
			 <tr class='bg1'><td align='center'><font color='#027ECD' size='2' face='Verdana, Arial, Helvetica, sans-serif'>DATE & TIME</font></td></tr>
			 <tr class='bg1'><td height='112' align='center'>
			 <input type=\"datetime-local\" class=\"text\" name=\"code15\" style='height:40px;width:145px;font-size:11pt' value=\"$code15\" required><br /><br />
	         <input type=\"hidden\" name=\"usid\" value=\"$iddd\">
	         <input type=\"hidden\" name=\"op\" value=\"AddVariantOk\">";
         if ($moderated == '1') {
         if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) {
       echo "<input type=\"hidden\" name=\"valid\" value=\"Yes\">"; }
	else {
       echo "<input type=\"hidden\" name=\"valid\" value=\"No\">"; }}
	else {
	   echo "<input type=\"hidden\" name=\"valid\" value=\"Yes\">"; }
	   echo "<input type=\"hidden\" name=\"lid\" value=\"0\">
	         <input type=\"hidden\" name=\"date\" value=\"$time\">
	         <input style='height:40px;width:80px;font-size:11pt' type=\"submit\" value=\"search\" class=\"button\">

    echo "</form>";

Form field names must be unique. When you reuse a name, only the last field with that name will exist in the php form data. In the example I posted, I used an array name with a unique index for each field. The reason for doing that is so that you can operate on the submitted array of values using php array functions.

Some suggestions that will greatly simplify the code -

  1. Don’t echo ‘static’ markup that contains no php. Just drop out of php mode and put the markup in-line.
  2. Don’t escape quotes. Just use the ‘opposite’ single v.s. double quote.
  3. Use css for styling instead of in-line styling.
  4. Don’t put php ‘business’ logic (database queries) inside your html document. Put these before the start of the html document and fetch the data into appropriately named php variable(s) that you then use in the html document.

In looking at that specific code, you are actually using a php variable as the field name, meaning whatever value is in the variable will be used as the field name. I doubt this is what you intended. This would be the syntax for outputting the variable as the value=’…’ attribute, not for naming the field.

Sponsor our Newsletter | Privacy Policy | Terms of Service