Solved

Retrieving value from combo box

Posted on 2004-08-05
15
771 Views
Last Modified: 2008-03-17
In one script I have a combo box that is filled with values retrieved from my database.  I also have a link which passes 2 parameters to another script.  One of these parameters must be the value from the combo box:

<a href="script2.php?value=value1&combo=comboboxvalue">

How can I retrieve this combo box value and add it to the link.  My other script is setup to retrieve this value via $_GET so I don't want to post all the data.
0
Comment
Question by:abstractionz
  • 8
  • 6
15 Comments
 
LVL 36

Assisted Solution

by:Zyloch
Zyloch earned 35 total points
ID: 11729997
Hi

Your best bet is to use client side scripting with Javascript since this doesn't use the server. Something like this would work: (where you want to put your link, have this instead):

<script language="javascript">document.write("<a href=\"script2.php?value=value1&combo=" + document.FORM_NAME.COMBO_BOX_NAME.options[document.FORM_NAME.COMBO_BOX_NAME.selectedIndex].value + \"></script>
<noscript><a href="script2.php"></noscript>Script2.php</a>

Regards,
Zyloch
0
 

Author Comment

by:abstractionz
ID: 11730038
and what if value1 was a php variable?
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11730084
Simple.

document.write("<a href=\"script2.php?value=<?php echo($value1); ?>&combo=" + document.FORM_NAME.COMBO_BOX_NAME.options[document.FORM_NAME.COMBO_BOX_NAME.selectedIndex].value + \">

Regards,
${Zyloch}
0
 

Author Comment

by:abstractionz
ID: 11730274
This doesn't seem to be working.  Perhaps it is a small syntax error, because it looks like it should work?
0
 

Author Comment

by:abstractionz
ID: 11730283
 <script language="javascript">
  document.write("<a href=\"show_cart.php?productid=<?php echo($productid);?>&size=" + document.selectedsize.options[document.selectedsize.selectedIndex].value + \">
  </script>
  <noscript><a href="script2.php"></noscript>Script2.php</a>
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11730298
You have to access the select box like document.FORM_NAME.selectedsize.option[document.FORM_NAME.selectedsize.selectedIndex].value

Regards
0
 

Author Comment

by:abstractionz
ID: 11730376
 <script language="javascript">
  document.write("<a href=\"show_cart.php?productid=<?php echo($productid);?>&size=" + document.sizesform.selectedsize.options[document.sizesform.selectedsize.selectedIndex].value + \">")
  </script>
   <noscript><a href="show_cart.php"></noscript>show_cart.php</a>

There is no link, just show_cart.php in text
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 36

Expert Comment

by:Zyloch
ID: 11730472
Hmm... That might be my fault. Do this:
<script language="javascript">
  document.write("<a href=\"show_cart.php?productid=<?php echo($productid);?>&size=" + document.sizesform.selectedsize.options[document.sizesform.selectedsize.selectedIndex].value + \">show_cart.php</a>");
  </script>
<noscript><a href="show_cart.php">show_cart.php</a></noscript>

Regards
0
 

Author Comment

by:abstractionz
ID: 11730601
That didnt show anything
0
 

Author Comment

by:abstractionz
ID: 11730650
does the fact that the form is within a table have anything to do with it?
0
 

Author Comment

by:abstractionz
ID: 11731184
OK I got it working with:

document.write('<a href="show_cart.php?productid=<?php print $productid; ?>&size=' + document.sizesform.selectedsize.options[document.sizesform.selectedsize.selectedIndex].text + '">Add One To Cart');

document.write('</a>');

but this only puts the first selected size in the url.  if the user changes the size in the combo box it doesnt change the url.

0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11731558
That's true. It may be better if you have it like this instead. Don't use document.write:

<a href="show_cart.php?productid=<?php print $productid; ?>&size=" onclick="window.location='show_cart.php?productid=<?php print $productid; ?>&size='+document.sizesform.selectedsize.options[document.sizesform.selectedsize.selectedIndex].text;">Add One To Cart</a>

This uses onclick instead of document.write. It takes the size that the user has selected when they clicked the link. If the user doesn't have Javascript, size will be nothing (as shown in href field)

Regards,
0
 

Author Comment

by:abstractionz
ID: 11731672
that didnt show anything
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11731893
Did you take out the document.write and the <noscript> tags?
0
 
LVL 26

Accepted Solution

by:
ushastry earned 40 total points
ID: 11733152
Try with this..

<?php
$value1 ="test";
?>

<SCRIPT LANGUAGE="JavaScript">
   var combo = "";
  function setValues(val)
 {
    myForm.combo.value    = val;
  }

</script>

<form name="myForm1">
<select name="selsize" onChange="setValues(this.options[this.selectedIndex].value)">
<option value="1">1
<option value="2">2
</select>
</form>

<a href="#" onclick="myForm.submit();"> send values </A>

<form name=myForm action="script2.php" method="GET">

<input type="hidden" name="combo" value="">
<input type="hidden" name="value" value="<?=$value1?>">

</form>


Hope this helps!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

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

22 Experts available now in Live!

Get 1:1 Help Now