Reputation: 1
So I'm passing variables in the url like this:
First select menu
<select name="car_id" onchange="location='newcar/'+this.value" >
<option>-</option>
<?php foreach ($cars as $car) {
if (isset($car->id)) {
echo "<option value=\"" . $car->id . "\">";
echo $car->car . "</option>\n";
}
} ?>
</select>
So if i press one of the options it will store it's value on the url. But then I have another select menu that shows the models and the problem is that the url isn't showing right. I don't know what I should write on the onchange that the first select menu's variable would be there to.
I want my url to show www.example.com/newcar/2/3
but this far i can get is www.example.com/newcar/2
and then the number 3 replaces number 2.
Upvotes: 0
Views: 84
Reputation: 92274
I would use a single function to respond to changes to both select
(window.onSelectChange = function() {
var val1 = document.getElementById('sel1').value;
var val2 = document.getElementById('sel2').value;
document.getElementById('url').value = 'http://example.com/something/' + val1 + '/' + val2;
})()
input {width: 100%}
<select id="sel1" onchange="onSelectChange()">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<select id="sel2" onchange="onSelectChange()">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<hr />
URL: <input id='url' />
Note that the above does not show "best practices", I was trying to keep it as close to your code as possible.
Upvotes: 1