Question

Im scratching my head once again and need your help.

What I have is a form that submits to a second page, with sessions enabled, i am storing the name value 2 fields on the form and setting their value names in the session so that if a user returns to a page their Username will already be populated in the text field. I have this working ok ..

The second page i am storing

$_SESSION['captured_by'] = $_POST['captured_by'];
$_SESSION['prid'] = $_POST['prid'];

HOWEVER..

Where i am stuck is getting a select option that is populated from a query to have the same functionality, so that when a user returns to the page, the selected option which has been saved in the session will keep that selection in the box rather than having to select it again.

Here is what i have as follows:

Form Page: This does work and does return the captured_by text when i return to the page

<fieldset><legend>Lesson Added By *</legend>
<p class="multiple">(Required - Logon ID)</p>
<input name="captured_by" maxlength="12" id="searchfield" type="text" value="<?php 
if(isset($_SESSION['captured_by'])){print stripslashes($_SESSION['captured_by']);}
else{print "   ";} ?>">
</fieldset>

The problem code is this

<select id="searchfield" name="prid">

<?php
 $query = "SELECT project_name, project_id FROM ll_project ORDER BY project_name ASC;";
 $result = mysql_query($query) or die(mysql_error());

 while($row = mysql_fetch_assoc($result)) {

 echo '<option value="'.$row['project_id'].'">'.$row['project_name'].' | '.$row['project_id'].'</option>';
 }
?>
</select>
</fieldset>

I need to edit this last section so that it picks up the previously selected option value from the stored session value.

Hope someone can help? Many Thanks. Tazzy

Was it helpful?

Solution

Try this,

<select id="searchfield" name="prid">

<?php
 $query = "SELECT project_name, project_id FROM ll_project ORDER BY project_name ASC;";
 $result = mysql_query($query) or die(mysql_error());

 while($row = mysql_fetch_assoc($result)) {

 echo '<option value="'.$row['project_id'].'"'. ((isset($_SESSION['prid']) && !empty($_SESSION['prid']) && ($_SESSION['prid'] == $row['project_id'])) ? 'selected="selected"' : '') .'>'.$row['project_name'].' | '.$row['project_id'].'</option>';
 }
?>
</select>
</fieldset>

OTHER TIPS

You could simply use ajax when someone change the selection.

$('select#searchfield').change(function(){
   $.ajax({
      type: 'GET',
      url: 'changeSession.php', // This is the url that will be requested
      data: {prid: $('select#searchfield').val()},
      success: function(html){ 
        // nothing really happens because you simply update your session 
      },
      dataType: 'html'
    });
});

Then in changeSession.php

if(isset($_GET['projectId']{
   $_SESSION['captured_by'] = $_POST['prid'];
}

That should do the job. You then add a condition when the form is generated and you add selected='selected' if $_SESSION['prid'] is not empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top