Solved

Javascript add input radio value to a parameter of a query string

Posted on 2013-12-28
13
1,182 Views
Last Modified: 2013-12-28
I'm working in classic asp and I have a form that I want to add the value of the radio button to a url with parameters. mypage.asp?party=yes or mypage.asp?party=no

<label>Go To party</label><br />
<input type="radio" name="party" id="rsvpyes" value="yes" /><br />
<input type="radio" name="party" id="rsvpno" value="no" /><br />
  

<a href="mypage.asp?party=yes">RSVP</a> or
<a href="mypage.asp?party=no">RSVP</a>

Open in new window

0
Comment
Question by:swaggerking
[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
  • 3
  • 3
  • 3
  • +2
13 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39744127
If you put it in a form with a 'GET' method, it will do that without javascript.

<form action="mypage.asp" method="get" >
<label>Go To party</label><br />
<input type="radio" name="party" id="rsvpyes" value="yes" /> YES<br />
<input type="radio" name="party" id="rsvpno" value="no" /> NO<br />
<input type="submit" value="Submit" />

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39744128
Is this what you mean?   http://jsbin.com/emIPiHuW/1/edit?html,js,output
function getParty()
{
var party = document.getElementsByName('party');

for (var i = 0, length = party.length; i < length; i++) {
    if (party[i].checked) {
       
        //alert(party[i].value);
         window.location='mypage.asp?party='+party[i].value;
     
        break;
    }
}
}

Open in new window

  <label>Go To party</label><br />
<input type="radio" name="party" id="rsvpyes" value="yes" /><br />
<input type="radio" name="party" id="rsvpno" value="no" /><br />
  

<a href="#mypage.asp?party=yes" onclick="getParty()">RSVP</a> or
<a href="#mypage.asp?party=no"  onclick="getParty()">RSVP</a>

Open in new window


Did you want to click the link? or the radio?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39744133
As Dave has said why not use a form but with jquery

http://jsfiddle.net/GaryC123/c2Np6/4/

<label>Go To party</label><br />
<input type="radio" name="party" id="rsvpyes" value="yes" />Yes<br />
<input type="radio" name="party" id="rsvpno" value="no" />No<br />
<a href="mypage.asp?">RSVP</a>

Open in new window



$("a").click(function(){
   $(this).attr("href",$(this).attr("href") + "party="+$(":radio[name=party]:checked").val())
})

Open in new window

0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:swaggerking
ID: 39744199
Dave GET won't work in this scenario.
Scott and Padas,
Very close to what I need but I should have been more clear with my question. My apologies, so I will increase points.

My form POST but redirects to "thankyoupage.asp" based upon a value in a hidden input. I need to add/append "yes" or "no" to the end of "thankyoupage.asp?party=" so I can display the correct message dependent on whether they can attend or not attend party.

<form action="somepage.asp" method="post" name="form" >
<input type="radio" name="party" id="rsvpyes" value="yes" /> YES<br />
<input type="radio" name="party" id="rsvpno" value="no" /> NO<br />

<button name="Submit" type="submit" value="Submit" />RSVP</button>

<input type="hidden" name="confirm page" value="thankyoupage.asp?party=">
</form>

Open in new window


Again, sorry for not being clearer.
0
 
LVL 58

Accepted Solution

by:
Gary earned 225 total points
ID: 39744207
<form action="somepage.asp" method="post" name="form" >
<input type="radio" name="party" id="rsvpyes" value="yes" /> YES<br />
<input type="radio" name="party" id="rsvpno" value="no" /> NO<br />

<button name="Submit" type="submit" value="Submit" />RSVP</button>

<input type="hidden" name="confirm page" value="thankyoupage.asp?party=">
</form> 
<script>
$("[name=form]").submit(function(){
   $("[name='confirm page']").val($(this).val() + $(":radio[name=party]:checked").val())
})
</script>

Open in new window


Still as clear as mud...
If you are posting to somepage.asp then why do you need to pass thankyoupage.asp?party={value} to the page as well?
Just grab the value of the radio buton in somepage.asp and then do whatever you need to do.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39744212
when posting your page to somepage.asp, the script(somepage.asp) know if party is << yes >> or << no >> 
so why do you need an hidden field ?
just concatenate the party query string value to your redirect string :

Response.Redirect "http://www.w3schools.com?party=" & Request.QueryString("party")
%>

Open in new window

0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 total points
ID: 39744220
Ok, try this.  It is much more direct, doesn't require a hidden input or javascript.  You probably will want to make the 'no' radio button the default (checked="checked") to make sure some value gets sent.  If neither button is checked, no value will be sent.

On "somepage.asp", make this the last code on the page.  It takes the value from the radio button and appends it to the redirect URL as a query string.  It's what I would and have done.
<%
' all the other code comes first.
dim party
party=Request.Form("party")
Response.Redirect("thankyoupage.asp?party=" & party)
Response.end
%>

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39744223
There really is no need to use js or jquery on this page.  The user selects yes or no then clicks the submit button.  Then on the somepage.asp you would use

<%
party="no"
if request.form("party")<>"" then
    if request.form("party")="yes" then
         party="yes"
end if
' later in the page

if party="yes" then
     response.write "Thank you for coming to my party"
      else
     response.write "I am sorry you can not come to my party"
end if
%>

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39744230
There you go, @swaggerking , three almost identical suggestions in a row as soon as we knew enough about what you were trying to do.
0
 

Author Comment

by:swaggerking
ID: 39744249
I hear you guys loud and clear and I agree with you 100%. Unfortunately I don't have access to "somepage.asp". Somepage.asp is actually an internal form processing script. Internally we use this to process any inet form and redirect to any page using the hidden input "confirm page".

GaryC123,
<script>
$("[name=form]").submit(function(){
   $("[name='confirm page']").val($(this).val() + $(":radio[name=party]:checked").val())
})
</script>

Open in new window


This only adds the value "yes" or "no" to the hidden
example:
<input type="hidden" name="confirmpage" value="yes">
or
<input type="hidden" name="confirmpage" value="no">

I'd prefer:
<input type="hidden" name="confirmpage" value="thankyoupage.asp?party=yes">
or
<input type="hidden" name="confirmpage" value="thankyoupage.asp?party=no">
0
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 175 total points
ID: 39744276
A couple of options.

1) Use ajax to post to somepage.asp and let the done function transfer to the thank you page.

2) Use the radio as the confirm page and depending on the value you have your yes or no plus your redirect.
<input type="radio" name="confirm page" id="rsvpyes" value="thankyou.asp?rsvp=yes" /><br />
<input type="radio" name="confirm page" id="rsvpno" value="thankyou.asp?rsvp=no" /><br />

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39744302
Too late for me to test this now but if it is only setting the value to yes/no then change the script to

<script>
$("[name=form]").submit(function(){
   $("[name='confirm page']").val($("[name='confirm page']").val + $(":radio[name=party]:checked").val())
})
</script> 

Open in new window


edit
I see why now

Still confused as to the logic behind this.
0
 

Author Closing Comment

by:swaggerking
ID: 39744347
I got working with your suggestions. Much appreciation.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 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