Solved

Retrieving value from combo box

Posted on 2004-08-05
15
774 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 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