Solved

Stop select tags re-initialising

Posted on 2004-10-05
21
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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