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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.