0

So, I have this code:

<form id="a">

<select name="day1" id="day1">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
...
</select>

<select name="month1" id="month1">
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
...
</select>

<select name="day2" id="day2">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
...
</select>

<select name="month2" id="month2">
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
...
</select>

<input type="submit" value="Submit" />

</form>

I need the script to redirect the browser to

http://example.com/?from=AA-BB&to=XX-YY

after the form is submitted.

AA would be the selected value from select area "day1",

BB would be the selected value from select area "month1",

XX would be the selected value from select area "day2",

YY would be the selected value from select area "month2"

Thanks in advance!

0

3 Answers 3

1

Try the following

var day1 = $('#day1 option:selected').text();
var month1 = $('#month1 option:selected').text();
var day2 = $('#day2 option:selected').text();
var month2 = $('#month2 option:selected').text();
var suffix = 'from=' + day1 + '-' + month1 + '&to=' + day2 + '-' + month2;
window.location = 'http://mypage.com/?' + suffix;
Sign up to request clarification or add additional context in comments.

3 Comments

I used to think I knew some jQuery but, the more I read the good answers here at stackoverflow; the more I see how much more I have to learn. That's great!
@zzzzBov it was unclear if the OP wanted the value or the text. If they wanted the value then yes val() would be more appropriate.
We always assume the "other" party has the remainder of the puzzle but, just in case, DonCroce, you will need to, of course, put a onSubmit='your-on-submit-processing-function();' and put all that stuff that JaredPar handed to you in there.
0

How about...

var sFrom="02-03",sTo="03-02";

var from = sFrom.split('-');
var to= sTo.split('-');
console.log(from);
console.log(to);

$('select#day1 > option[value="'+from[0]+'"]').attr('selected','selected');

$('select#month1 > option[value="'+from[1]+'"]').attr('selected','selected');


$('select#day2 > option[value="'+to[0]+'"]').attr('selected','selected');

$('select#month2 > option[value="'+to[1]+'"]').attr('selected','selected');

As for the querystring retrieval, check this link out

Comments

0

In plain javascript, you can do the following (separated into the variables as you represented them):

function submitHandler() {
    var AA = document.a.day1.value;
    var BB = document.a.month1.value;
    var XX = document.a.day2.value;
    var YY = document.a.month2.value;

    location.href = "http://example.com/?from=" + AA + "-" + BB + "&to=" + XX + "-" + YY;
}

Then set onsubmit for your form, as follows:

<form name="a" onsubmit="submitHandler(); return false;">

3 Comments

still redirects me to index.php?day1=03&month1=11&day2=08&month2=11 The variables are okay, I've tested them in an alert. The whole url with the params gets alerted correctly - The form still gets submitted though and I end on my index.php, no redirect took place to example.com
added <form name="a" onsubmit="submitHandler();return false;"> and works now :)
Good catch; I've updated the answer accordingly, for completeness.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.