Stop select tags re-initialising

Hi experts,

html newbie here!

  Basically i have a form, with some select tags, like a drop down list.  When I select from these, and hit the submit button, I want to see the items I selected, while the form goes about its stuff, but in fact they are reverting back to their initial state.

How can I keep what I chose in the select tags on the screen?

Thanks,

  Colin.
LVL 3
colinspursAsked:
Who is Participating?
 
neesterConnect With a Mentor Commented:
Are you mixing PHP And Javascript there?
You cannot say onclick="PHP FUNCTION HERE"

the function you have there is a PHP one - and needs to go inside hte server side code...

what you can do is, in the PHP File.

I usually have this:


if ($_POST['submit'])
{
   db_update_table_firsts()
}

that will basically, check and see if there is a value for the SUBMIT button, if there is.
Then it will run your PHP function for you!
:)
0
 
knightEknightCommented:
can you show us a sample of your code (the form and selects)?
0
 
colinspursAuthor Commented:
I've tried to keep this simple as poss.  Apologies for the php script tags etc.  I'll try to remove these if necessary.

<?  
echo "<form name = 'Form1'>\n";
echo "<select name = 'HScore1'>\n";              

for ($HScore1 = 0; $HScore1 <= 15; $HScore1++ )
{
    echo "<option value = $HScore1>$HScore1";
}
echo "</select>\n";

echo "<select name = 'AScore1'>\n";              
for ($AScore1 = 0; $AScore1 <= 15; $AScore1++ )
{
    echo "<option value = $AScore1>$AScore1";
}
echo "</select>\n";

echo "<br><input type = 'submit'  value = 'Update'>\n";

echo "</form>\n";
?>
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
colinspursAuthor Commented:
ok here's the html version

<form name = 'Form1'>
  <select name = 'HScore1'>          
   <option>A
   <option>B
   <option>C
  </select>

  <select name = 'AScore1'>          
   <option>1
   <option>2
   <option>3
  </select>

<br><input type = 'submit'  value = 'Update'>

</form>
0
 
COBOLdinosaurCommented:
What are you submitting to?  There is no action on the form.

Cd&
0
 
colinspursAuthor Commented:
The idea is to take the selections and use them to update a table which will be shown on the same page.  Before I get on to that coding i want to try to make sure I have no errors thus far, and then I noticed the selects were being re-set.

So if I added the action to update the table the selects would stay as selected?

Colin.
0
 
COBOLdinosaurConnect With a Mentor Commented:
If you are just using the data to run local functions, don't use a submit button.  Use:

<input type="button" value="update" onclick="yourfunction*()">

Cd&
0
 
colinspursAuthor Commented:

So action would remain blank?

Thanks for your help, looks like the points will be yours, but it's late here (UK) so I'll check in again tomorrow - ok?

Colin.
0
 
COBOLdinosaurCommented:
No prob.  If that does not solve it we will play with it until we get it right.

Cd&
0
 
neesterCommented:
<?  
echo "<form name = 'Form1'>\n";
echo "<select name = 'HScore1'>\n";              

for ($HScore1 = 0; $HScore1 <= 15; $HScore1++ )
{
if ($_POST['HScore1'] == $HScore1)
{
    echo "<option selected="selected" value = $HScore1>$HScore1";
}else{
    echo "<option value = $HScore1>$HScore1";
}
}
echo "</select>\n";

echo "<select name = 'AScore1'>\n";              
for ($AScore1 = 0; $AScore1 <= 15; $AScore1++ )
{
if ($_POST['AScore1'] == $AScore1)
{
    echo "<option selected="selected" value = $AScore1>$AScore1";
}else{
    echo "<option value = $HScore1>$HScore1";
}
    echo "<option value = $AScore1>$AScore1";
}
echo "</select>\n";

echo "<br><input type = 'submit'  value = 'Update'>\n";

echo "</form>\n";
?>



THAT CODE will check if hte POSTED VALUE.
Is == To the current ITEM in the LOOP.
IF SO.
It will MARK It SELECTED
0
 
neesterCommented:
BTW

CobolDinosaur - If a FORM is missing the ACTION - it will submit the form the the page that holds the form...
if the METHOD is blank it will default to GET...
Neither are good practice to rely on - but I am assuming his code posts to itself - thats what my code posted above relies on...
0
 
COBOLdinosaurCommented:
Yes it submits to itself.  That is why it re-initializes. Using an input button makes the function standalone, and there is no submit. There is no need for anykind of submit to run local JS functions.

Cd&
0
 
neesterCommented:
No i think you missed what he wants.
I think he wants you to select an option.
Hit SUBMIT.
And your option stays the same.
And that option you selected, remains selected....

When he says re-initialises - I took it as - resets the value to the 1st OPTION in the select list...
0
 
MMabatigCommented:
I think you guys are looking at the same apple from different sides of the tree.  (You both seem to have come up with correct answers)  However, I have a minor question:

Why not use:

<form action="javascript:yourfunction*();">
<input type="submit" value="Submit">
</form>

This would use the native form submission action instead of triggering off of an onClick event.  It also solves the problem of having a form without an action. Like COBOLdinosaurs solution, It also prevents the page from being reloaded.

Hope it helps,
-Mark
0
 
neesterCommented:
Well the author of this question can have a look through here...
he has a PHP answer, HTML answer, and a Javascript answer....

well not really a HTML answer - but more HTML than javascript...

:)
0
 
COBOLdinosaurCommented:
That's the way is should be lots of options. :^)

Cd&
0
 
colinspursAuthor Commented:
Wow - thanks people.

Well I tried the local function method - and get "object expected" - where am I going wrong?

echo "<br><input type = 'button'  value = 'Update League Table' onclick = 'db_update_table_firsts()'>\n";
-
-
-
function db_update_table_firsts()
{
$query = "UPDATE league_table_firsts
      SET Played = Played+1, GFor = GFor + $_POST[HScore1], GAgainst = GAgainst + $_POST[AScore1]
      WHERE Team = $_POST[Team]";
$result = mysql_query or die("Could not update league_table_firsts.");
return;
}
0
 
COBOLdinosaurCommented:
You sre going to have to seperate teh client and the server.  PHP runs on the server during generation.  Javascript events are executed on the client, and they are not interchangeable.

Cd&
0
 
colinspursAuthor Commented:
Hi guys,

I really appreciate your help.  It's clear I have alot of thinking to do about this whole thing.  As I just have a couple of hours each evening, that may take some time!  but I'm alot further forward thanks to you guys and I think it best if I split the points (250 now) 50/50 Cd& & neester - fair?

I also think i'll have alot more questions before I'm done, so watch this space.  It seems this question wasn't as clear cut as I thought, and provoked a small debate, which is satisfying.  Not such a dumb question!

Cheers, Colin.
0
 
neesterCommented:
Thanks ColinSpurs,
Sorry that we couldnt completly solve your problem - but I hope in the future we can give you the answer you were looking for.
Thanks for hte points and the rating :)

Cheers,
Chris :)
0
 
COBOLdinosaurCommented:
Glad we could help. Thanks for the A. :^)

Cd&
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.