[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Retrieving value from combo box

Posted on 2004-08-05
15
Medium Priority
?
789 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 140 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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:
Umesh earned 160 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

867 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