Solved

Stop select tags re-initialising

Posted on 2004-10-05
21
170 Views
Last Modified: 2010-04-09
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.
0
Comment
Question by:colinspurs
  • 7
  • 6
  • 6
  • +2
21 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 12231402
can you show us a sample of your code (the form and selects)?
0
 
LVL 3

Author Comment

by:colinspurs
ID: 12231512
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
 
LVL 3

Author Comment

by:colinspurs
ID: 12231581
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 12231839
What are you submitting to?  There is no action on the form.

Cd&
0
 
LVL 3

Author Comment

by:colinspurs
ID: 12231986
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
 
LVL 53

Assisted Solution

by:COBOLdinosaur
COBOLdinosaur earned 125 total points
ID: 12232101
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
 
LVL 3

Author Comment

by:colinspurs
ID: 12232207

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

Expert Comment

by:COBOLdinosaur
ID: 12232320
No prob.  If that does not solve it we will play with it until we get it right.

Cd&
0
 
LVL 11

Expert Comment

by:neester
ID: 12232323
<?  
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
 
LVL 11

Expert Comment

by:neester
ID: 12232341
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 12232453
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
 
LVL 11

Expert Comment

by:neester
ID: 12232481
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
 
LVL 1

Expert Comment

by:MMabatig
ID: 12233177
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
 
LVL 11

Expert Comment

by:neester
ID: 12233194
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 12237284
That's the way is should be lots of options. :^)

Cd&
0
 
LVL 3

Author Comment

by:colinspurs
ID: 12241018
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
 
LVL 11

Accepted Solution

by:
neester earned 125 total points
ID: 12243518
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 12243575
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
 
LVL 3

Author Comment

by:colinspurs
ID: 12253406
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
 
LVL 11

Expert Comment

by:neester
ID: 12253599
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 12253878
Glad we could help. Thanks for the A. :^)

Cd&
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Do you want to insert HTML5 video into your site? This is the tutorial how to do so. What are the main advantages of HTML5 video? 1) Have good compression, good image quality, and low decode processor use. 2) It is royalty-free 3) It is easi…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now