How do I make the updated value become the default selected value?

Hello again mates,

I am trying to update status of a selected record on the deeds table.

Once the record is successfully updated, we would like the most recently updated record to become the default selected record on the Status dropdown.

I would like to think that this is fairly routine and straightforward.

However, it doesn't appear to be working for me no matter what I try.

After each record is successfully updated, the last updated record is still not the selected value.

Where am I going wrong here?

		  <select name="sStatus" data-placeholder="Select status..." class="chosen-select" style="width:178px;">
            <option value=""></option>
			<?php
			$tsql = "SELECT DISTINCT s.StatusID, s.Status FROM status s inner join deeds d on s.StatusID = d.StatusID";
			$stmt = sqlsrv_query( $conn, $tsql);
			while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
			{
				if($strDefault == $sStatus)
				{
					$sel = "selected";
				}
				else
				{
					$sel = "";
				}
			?>
			<option value="<?php echo strDefault;?>" <?php echo $sel;?>><?php echo $row["Status"];?></option>
			<?php
			}
			?>
		  </select>

Open in new window

LVL 29
sammySeltzerAsked:
Who is Participating?
 
Marco GasiFreelancerCommented:
Not at all, but in the posted code I didn't see that line because it is in another part of your script as I didn't see the line

$strDefault = $row["StatusID"]

Open in new window


so I asked to be sure.

Personally I would try to check the values of these two variables:

while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
    $strDefault = $row["StatusID"];
    echo "$strDefault vs $sStatus<br>";
}

Open in new window


Just to be sure values are correct...
0
 
Dave BaldwinFixer of ProblemsCommented:
XHTML expects selected="selected" instead of just selected and that's what I always use.
$sel = 'selected="selected" ';

Open in new window

http://www.w3schools.com/tags/att_option_selected.asp
0
 
Marco GasiFreelancerCommented:
Where are you defining  $strDefault and $sStatus variables? If they are not defined, thi can be th origin of the problem.
In additio, in line 17 you have a typo: strDefault instead of $strDefault...
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
sammySeltzerAuthor Commented:
Let me start with Marco,

Hi Marco,

sStatus is the <SELECT name='sStatus'....>

Sorry, copy and paste messed up this one below:

$strDefault = $row["StatusID"]

Hi Dave,

Well, that's what I tried originally:

$sel = 'selected="selected"';

It didn't make any difference.
0
 
Marco GasiFreelancerCommented:
sammySeltzer I understood that but where are you assigning to $sStatus the value of sStatus? I ask only because sometimes we forget the obvious things and since I see just the code you post here, I can't be sure that assignement has be done somewhere else... :-)
0
 
sammySeltzerAuthor Commented:
Marco,

Maybe my mistake is trying to replicate my classic asp experience.

Here is what I am doing:

I am select the relevant records from deeds table:

Then I looped:

while($row = sqlsrv_fetch_array($query,SQLSRV_FETCH_ASSOC))

                        {
                              $sStatus=$row["Status"];

}

The whole idea is to grab the values from deeds table to be used later.

Now, I am trying to use sStatus here:
$strDefault = $row["StatusID"]

If $strDefault = $sStatus
{...

Am I doing it incorrectly?
0
 
Chris StanyonCommented:
I think you need to take a step back here - having just read through your comments it seems that you are effectively doing this:

    SELECT DISTINCT s.StatusID, s.Status FROM status s inner join deeds d on s.StatusID = d.StatusID

Then you loop through the records and do this:

    $sStatus = $row["Status"];
    $strDefault = $row["StatusID"]

You then check if $sStatus == $strDefault

I would expect that to always return false - it just seems to not make sense - logically speaking.

Can you explain where you expect to get the value from that indicates what should be selected.
0
 
sammySeltzerAuthor Commented:
Worked!

Thank you both. Both your suggestions helped out tremendously especially Marco.

Everything looked good except I was confusing sStatus with strDefault.
0
 
sammySeltzerAuthor Commented:
Thanks
0
 
Marco GasiFreelancerCommented:
I suspect $sStatus be a php variable filled through javascript with the value of the select 'sStatus', am I wrong?
0
 
sammySeltzerAuthor Commented:
Well, I started to $sStatus variable (yes php variable) initially but somewhere, I get it confused with strDefault.

They are supposed to do the same thing and that is hold the value of StatusID but I only needed one of them, not both.

Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.