Jump to content

Repopulating selection box


ScoobyDont

Recommended Posts

Hi
 
I am creating a dummy 3rd party site for my main site, I have everything working apart from the most important part
 
The trouble I am having is I use the dummy site to add a new user to a db and have a second page for updating, 
 
page 1 ="Enter Customer Number" - Submit button to open page 2 which is a form with populated data from the db
 
The trouble is I cannot get it to re populate the select list

<label>Job Status</label><select name="jobstatus" class="formfields">
  <option value="">Please Select</option>
  <option value="offsite">Off Site</option>
  <option value="onsite">In On Site</option>
  <option value="allocated">Allocated</option>
</select>

Everytime I pull the data back into the form it says "Please Select" I have tried 

<?php echo $data['jobstatus']?>">

And placed it in a couple of areas but no luck.

 

Can anyone help how I can repopulate the form list with what is in the db and not "Please Select"

 

Thanks in advance

Edited by ScoobyDont
Link to comment
Share on other sites

This is the form that starts the query

<form action="update.php" method="post" name="update">
<input id="wipno" placeholder="WIP No" name="wipno" class="textbox" type="text" />
<input name="submit" type="submit" id="submit" value="Get Customer Record" />
<p><a href="insert.php">Insert New Record</a></p>
</form>

This is "update.php"

$search=$_POST['wipno']; 

$data = 'SELECT * FROM `register` WHERE `wipno` = "'.$search.'"'; 
  $query = mysql_query($data) or die("That Didnt work. ". mysql_error()); 
  $data2 = mysql_fetch_array($query); 
   
   
?> 

<div class="header"><h3>Update Customer/Booking</h3></div>

<form action="updated.php" method="post" name="updaterecord">

<label>Reg No</label><input name="registration" id="registration" type="text" class="formfieldssmall" value="<?php echo $data2['registration']?>"/>
<label>Chassis No</label><input name="chassis" id="chassis" type="text" class="formfields" value="<?php echo $data2['chassis']?>"/>
<label>Engine No</label><input name="engine" id="engine" type="text" class="formfields" value="<?php echo $data2['engine']?>"/>
<label>Date In</label><input name="datein" id="datein" type="text" class="formfields" value="<?php echo $data2['datein']?>"/>
<label>Time In</label><input name="timein" id="timein" type="text" class="formfieldssmall" value="<?php echo $data2['timein']?>"/>
<label>Date Out</label><input name="dateout" id="dateout" type="text" class="formfields" value="<?php echo $data2['dateout']?>"/>
<label>Time Out</label><input name="timeout" id="timeout" type="text" class="formfieldssmall" value="<?php echo $data2['timeout']?>"/>
<label>Job Status</label><select name="jobstatus" class="formfields">
  <option value="">Please Select</option>
  <option value="<?php echo $data2['jobstatus']?>"></option>
  <option value="offsite">Off Site</option>
  <option value="onsite">In On Site</option>
  <option value="allocatedtotech">Allocated to Tech</option>
  <option value="awaitingauth">Awaiting Authority</option>
  <option value="qualitycheck">Quality Check</option>
  <option value="awaitingwash">Awaiting Wash</option>
  <option value="awaitinginvoice">Awaiting Invoice</option>
  <option value="readytocollect">Ready to Collect</option> 
</select>
<label>Wip No</label><input name="wipno" id="wipno" type="text" class="formfields" value="<?php echo $search?>">"/>
<label>Contact No</label><input name="contactnumber" id="contactnumber" type="text" class="formfields" value="<?php echo $data2['contactnumber']?>"/>
<label>Email</label><input name="email" id="email" type="text" class="formfields" value="<?php echo $data2['email']?>"/>
<label>Name</label><input name="name" id="name" type="text" class="formfields" value="<?php echo $data2['name']?>"/>
<label>Vehicle</label><input name="vehicle" id="vehicle" type="text" class="formfields" value="<?php echo $data2['vehicle']?>"/>
<label>Team</label><select name="team" class="formfields">
  <option value="">Please Select</option>
  <option value="sa1">SA1</option>
  <option value="sa2">SA2</option>
  <option value="sa3">SA3</option>
  <option value="sa4">SA4</option>
</select>
<br />
<input name="submit" type="submit" class="button"value="Update Customer" />
</form>

Hope this helps

Link to comment
Share on other sites

HI, Thanks I will make sure my site is sqli'd  :happy-04:

 

This is just a dummy site to test populating my site from a third party site, so I am not to bothered about security etc etc etc at the minute.

 

Here is the "update.php" code

$registration = $_POST['registration'];
 $chassis = $_POST['chassis'];
 $engine = $_POST['engine'];
 $datein = $_POST['datein'];
 $timein = $_POST['timein'];
 $dateout = $_POST['dateout'];
 $timeout = $_POST['timeout'];
 $jobstatus = $_POST['jobstatus'];
 $wipno = $_POST['wipno'];
 $contactnumber = $_POST['contactnumber'];
 $email = $_POST['email'];
 $name = $_POST['name'];
 $vehicle = $_POST['vehicle'];
 $team = $_POST['team'];


$data = "UPDATE `register` SET registration='$registration', chassis='$chassis', engine='$engine', datein='$datein', timein='$timein', dateout='$dateout', timeout='$timeout', jobstatus='$jobstatus', wipno='$wipno', contactnumber='$contactnumber', email='$email', name='$name', vehicle='$vehicle', team='$team' WHERE wipno=".'"'.$wipno.'"'; 
  $query = mysql_query($data) or die("Ooops Its snapped. ". mysql_error()); 
   
?> 
Edited by ScoobyDont
Link to comment
Share on other sites

The trouble is I cannot get it to re populate the select list

 

 

have you researched what the html would be to cause a option to be selected? if you haven't, that would be your first step, because if you don't know what html you are trying to produce, there's no way you can write the code to do it. there's hint in the bold part of my question to you in this paragraph.

 

next, after you know what html you are trying to produce, the easiest way of producing it is to dynamically produce the list of <option ...>...</option> choices and the easiest way of doing that is to have the choices defined in a data structure (database table or an array), then simply loop over that defining data and produce the list of option choices. this will reduce the line of code that causes the the option to be selected to a single line, rather than to repeat it for ever possible option in every select menu.

 

page 1 ="Enter Customer Number"

 

 

rather than to expect the user to know what the customer numbers/names are and use the error-prone method of having them type the value in a form field, you should query your table that defines the customer numbers/names and produce a select/option menu to allow the customer number to be picked.

Link to comment
Share on other sites

ScoobyDont,

 

Asmac_gyver is alluding to, you are not outputting the HTML correctly to have a "selected" option. It would look like this

 

<option value ="1" selected="selected">Option 1</option>

 

So, you need to get the currently selected value and determine which option to add that parameter to. IMO, this is easiest when the option list is dynamically created. If you have a table to define the options in the DB, then you can use that. Else, you can define an array. Then create a function to create the option list passing the list of available options and the selected option. Also, you can use the same data (DB or array) to validate that the user passed a valid value.

 

 

function createOptions($options, $selectedValue=false) {
    $optionsHTML = '';
    foreach($options as $value => $label) {
        $selected = ($value == $selectedValue ) ? ' selected="selected"' : '';
        $optionsHTML .= "<option vaue='{$value}'{$selected}>{$label}</option>\n";
    }
    return  $optionsHTML;
}

 

 

Usage

 

<?php
//In head of document
$jobStatuses = array(
    'offsite' => 'Off Site',
    'onsite' => 'In On Site',
    'allocated' => 'Allocated'
);
 
//Get the selected value
$selectedJobStatus = //Some code to retrieve selected value
 
//Create job status option list
$jobStatusOptions = createOptions($jobStatuses, $selectedJobStatus);
 
?>
 
<select name="team" class="formfields">
<?php echo $jobStatusOptions; ?>
</select>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.