Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Retrieving value from combo box

Posted on 2004-08-05
15
Medium Priority
?
786 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.
Suggested Courses

610 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