Solved

Retrieving value from combo box

Posted on 2004-08-05
15
776 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 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

726 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