Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append checkbox value to url?

Status
Not open for further replies.

dmoonme

MIS
Jul 21, 2004
33
0
0
US
Not sure if this is the right forum to post this, please excuse me if not.

what I'm trying to do is append the value of a checkbox when checked to the form action url.
So for example, if checkbox1 and checkbox2 is checked then action="
PLEASE HELP!

Code:
<%@LANGUAGE="VBSCRIPT"%>


<form name="form1" method="post" action="[URL unfurl="true"]http://server/webfile.asp?blah&blah">[/URL]
<table width="100%" height="41" border="0">
  <tr>
    <td height="31" valign="middle" class=gc><p><strong>Search For:</strong>          <input class=gc name="in_tx_fulltext" type="text" id="in_tx_fulltext">
          <input name="image" type="image" src="action_search_button.gif" alt="Submit Form" align="absmiddle" width="30" height="20" border="0">
    </p>
      <p>
        <input name="Checkbox1" type="checkbox" id="CB1" value="&ABC=123">
        CheckBox1<br>
        <input name="Checkbox2" type="checkbox" id="CB2" value="&DEF=456">
        CheckBox2<br>
</p>
      </td>
  </tr>
</table>
</form>
 
Hello dmoonme,

I do not see where the server-side comes into play. This might be due to the purged-down version you post. Hence, effectively it is more of a javascript question. We have a dedicated javascript forum:
Also, FYI, we have a dedicated asp forum as well:

I could attempt though to answer your question directly.
[1] If you want to use action method post, those info should be available to server by response.form().
[2] Having side, to add what you want to the action attribute, this should be done on client-side, hence more of a javascript problem than vbscript. This is a cleanup version I let you to figure out the idea to get it done.
Code:
<html>
<head>
<script type="text/javascript" language="javascript">
function validate() {
	var elem=document.getElementsByName("CheckBox_");
	var formobj=document.forms["form1"];
	var re, s;
	for (var i=0;i<elem.length;i++) {
		re=new RegExp(elem[i].value,"gi");
		s=formobj.action
		if (!elem[i].checked) {
			formobj.action=s.replace(re,"");
		} else {
			formobj.action=re.test(s)?s:s+elem[i].value;
		}
	};
			[green]//testing only, comment out later
			alert(formobj.action)[/green]
	[green]//uncomment the submit later, return does not matter
	//document.forms["form1"].submit();
	return false;[/green]
}
</script>
</head>
<body>
<form name="form1" method="post" action="[URL unfurl="true"]http://server/webfile.asp?blah&blah">[/URL]
<table width="100%" height="41" border="0">
	<tr>
	<td height="31" valign="middle" class=gc><p><strong>Search For:</strong>          <input class=gc name="in_tx_fulltext" type="text" id="in_tx_fulltext">
		<input name="image" type="image" src="action_search_button.gif" alt="Submit Form" align="absmiddle" width="30" height="20" border="0" [blue]onclick="return validate();"[/blue]>
    </p>
	<p>
		<input name=[blue]"Checkbox_"[/blue] type="checkbox" id="CB1" value="&ABC=123">
        CheckBox1<br>
		<input name=[blue]"Checkbox_"[/blue] type="checkbox" id="CB2" value="&DEF=456">
        CheckBox2<br>
	</p>
    </td>
	</tr>
</table>
</form>
</body>
</html>
(After testing out thoroughly, change the green color code.)

regards - tsuji
 
Thanks for the tip tsuji! It accomplished exactly what I wanted.
 
dmoonme,

It would, in principle, follow the same pattern as appending the checkbox value. Only this time it is not appending but replacing the url. You have to device the regular pattern for and replace it with your url+"?".

- tsuji
 
Hi tsuji I really appreciate your help. I've been striking out on this. If no checkboxs are checked it works fine, but if more than 1 checkbox is checked only 1 checkbox value is appended to the url.

Here's what I have:

Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<script type="text/javascript" language="javascript">
function validate() {
    var elem=document.getElementsByName("CheckBox_");
    var formobj=document.forms["form1"];
    var re, s;
    for (var i=0;i<elem.length;i++) {
        re=new RegExp(elem[i].value,"gi");
        s=formobj.action
        
        if (!elem[i].checked) {
            formobj.action=s.replace(re,"");
          
        } else {
         
            formobj.action="[URL unfurl="true"]http://server/webfile.asp?&blah"+elem[/URL][i].value;
        }
    };

    //alert(formobj.action)
    //uncomment the submit later, return does not matter
    document.forms["form1"].submit();
    return false;
}
</script>

<form name="form1" method="post" action="[URL unfurl="true"]http://server/webfile.asp?&blah&blah"[/URL] ID="Form1">
<table width="100%" height="41" border="0" ID="Table1">
  <tr>
    <td height="31" valign="middle" class=gcontentsection><p><strong>Search For:</strong>          <input class=gc name="in_tx_fulltext" type="text" id="in_tx_fulltext">
          <input name="image" type="image" src="action_search_button.gif" alt="Submit Form" align="absmiddle" width="30" height="20" border="0" onclick="return validate();" ID="Image1">
    </p>
      <p>
        <input name="Checkbox_" type="checkbox" id="sec" value="&in=796">
        Sec<br>
        <input name="Checkbox_" type="checkbox" id="ip" value="&in=753">
        IProperty<br>
        <input name="Checkbox_" type="checkbox" id="at" value="&in=691">
        AT<br>
        <input name="Checkbox_" type="checkbox" id="en" value="&in737">
        En<br>
        <input name="Checkbox_" type="checkbox" id="fi" value="&in=733">
        FI</p>
      </td>
  </tr>
</table>
</form>




[\code]
 
Change your form method to get and it will automatically send all form element values to the next page in the URL. It will send zero-length form element values, as well, but this should be easily filtered out by the page submitted to.

Lee
 
Thanks trollacious. The get method seems to work but when more than 1 checkbox is checked, only 1 checkbox value is appended to the url. any ideas?
 
You're naming all your checkboxes the same thing. If you name them differently, you'll get the results you want.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top