how to concatinate a link with <select> <option> value on "onchange" event - php

<?php echo $this->getUrl(); ?> gives me my index page url .i.e. www.domain.com/index.php/
But now, what I want is, when any particular option is selected, it should add to this url.
.e.g.
www.domain.com/index.php/wordpress/
I write this code for it but now don know how to get it done. :(
<select onchange="setLocation('<?php echo $this->getUrl(); ?>')">
<option value=""> </option>
<option value="wordpress">Wordpress</option>
<option value="drupal">drupal</option>
<option value="magento">Megento</option>
</select>
Also, I had searched this link but couldn't take help from it. As it didnt work for me.
<select name="forma" ONCHANGE="location = this.options[this.selectedIndex].value;">
<option value="Home">Home</option>
<option value="Contact">Contact</option>
<option value="Sitemap">Sitemap</option>
</select>

With this (note that I've added this as the first argument):
<select onchange="setLocation(this, '<?php echo $this->getUrl(); ?>')">
<option value=""> </option>
<option value="wordpress">Wordpress</option>
<option value="drupal">drupal</option>
<option value="magento">Megento</option>
</select>
You could write the following JS:
function setLocation(elm, baseUrl) {
var keyword = elm.options[elm.selectedIndex].value; // Gets the selected keyword (some sanity check could be done)
window.location = 'http://' + baseUrl + keyword + '/'; // Redirects
}
Alternatively:
<select name="forma" onchange="window.location='http://<?php echo $this->getUrl(); ?>' + this.options[this.selectedIndex].value + '/';">
<option value="Home">Home</option>
<option value="Contact">Contact</option>
<option value="Sitemap">Sitemap</option>
</select>
(EDIT Prepended http:// to both examples.)

Related

Creating dynamic button with Jquery

Hey guys I'm trying to create a dynamic button from a drop down list. for example, in the HTML code below...
function dropdownbutton(){
var make = document.getElementById("makelist");
var answer = make.options[make.selectedIndex].value;
alert("answer")
}
<div class="make">
<label>Make</label>
<select name="make" id="makelist" onchange="getId(this.value);">
<option value="">Select Make</option>
<option value="">1</option>
</select>
</div>
<div id="model">
<label>Model</label>
<select name="model" id="modellist" onchange="getId2(this.value);">
<option value="">Select Model</option>
<option value="">1</option>
<option value="">2</option>
</select>
</div>
<div id="year">
<label>Year</label>
<select name="year" id="yearlist" onchange="getId3(this.value);">
<option value="">Select Year</option>
<option value="">1</option>
<option value="">2</option>
</select>
</div>
<button id="dropdownbutton" onclick="dropdownbutton()" class="vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-rounded vc_btn3-style-3d vc_btn3-color-success">Dropdown</button>
I'm open to trying it in different languages but I would prefer to do it in php, I simply don't know how to get the values from each dropdown.
I have taken help of Jquery here, inside the dropdownbutton() function i call all the select box element.
And using .each function extracted the value selected value of dropdowns.
i check those value if not empty then created a button inside element having Id #append_new_button
Please check the below code it might solve your issue
Thanks
function dropdownbutton() {
jQuery('#append_new_button').html('');
jQuery('select').each(function(){
var select_value = jQuery(this).val();
var select_label = jQuery("#"+jQuery(this).attr('id')+" option[value='"+select_value+"']"). text();
if(select_value != ''){
jQuery('#append_new_button').append('<input type="button" id="'+select_value+'" value="'+select_label+'"></button>')
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="make">
<label>Make</label>
<select name="make" id="makelist">
<option value="">Select Make</option>
<option value="make_1">Make 1</option>
<option value="make_2">Make 2</option>
</select>
</div>
<div id="model">
<label>Model</label>
<select name="model" id="modellist" >
<option value="">Select Model</option>
<option value="model_1">Model 1</option>
<option value="model_2">Model 2</option>
</select>
</div>
<div id="year">
<label>Year</label>
<select name="year" id="yearlist" >
<option value="">Select Year</option>
<option value="year_1">year 1</option>
<option value="year_2">year 2</option>
</select>
</div>
<button id="dropdownbutton" onclick="dropdownbutton()" class="vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-rounded vc_btn3-style-3d vc_btn3-color-success">Dropdown</button>
<div id="append_new_button">
</div>
I updated that code to find the values for each drop down
function dropdownbutton(val){
var make = document.getElementById("makelist");
var answer = make.options[make.selectedIndex].value;
var model = document.getElementById("modellist");
var answer2 = model.options[model.selectedIndex].value;
var year = document.getElementById("yearlist");
var answer3 = year.options[year.selectedIndex].value;
alert(answer2);
}
Now i need to figure out how to pass these variables to a link for example, mydomain.com/answer/answer2/answer3
i think that this code is more dynamic.
$("button").click(function() { // if button is clicked
var arr = $('select').map(function() { // run through all selects and get value
return this.value
}).get();
var url = "http://exp.com"; // base for the url
arr.forEach(function(item) {
url = url + '/' + item; // append all options to the base url
});
window.location.href(url); // redirect to base url + all options
});
Don't forget to add value to your options. <option value="1">1</option>
Please look at jsfiddle for a working example

jquery load passing a parameter

Hello just want to ask what is the proper way to pass a parameter using jquery my parameter is from a html input , below is my code.
function ajax_post(){
var param = document.getElementById('def').value;
$('#chatlogs').load('main.php?id='+param);
}
I'm able to load the main.php but cannot get to echo the param, here is my main.php code.
<?php echo $_GET['id'];?>
Adding more details.. im getting the parameters from a HTML select input then after getting the value, will then load the main.php into a div with a id of #chatlogs.
<select name="topic" id="def" class="form-control" style="width:450px;" onChange='ajax_post()'>
<option value="" selected>---</option>
<option value="Solder Short">Solder Short</option>
<option value="Insufficient Solder">Insufficient Solder</option>
<option value="Misaligned Component">Misaligned Component</option>
<option value="Missing Component">Missing Component</option>
<option value="Inverted Component">Inverted Component</option>
<option value="Pad Contamination">Pad Contamination</option>
</select>
<div id="chatlogs">
Please select a defect category...
</div>
Try this:
<select name="topic" id="def" class="form-control" style="width:450px;">
<option value="" selected>---</option>
<option value="Solder Short">Solder Short</option>
<option value="Insufficient Solder">Insufficient Solder</option>
<option value="Misaligned Component">Misaligned Component</option>
<option value="Missing Component">Missing Component</option>
<option value="Inverted Component">Inverted Component</option>
<option value="Pad Contamination">Pad Contamination</option>
</select>
<div id="chatlogs">
Please select a defect category...
</div>
<script>
$(document).ready(function(){
$('#def').on('change', function() {
var param = document.getElementById('def').value;
//alert(param);
$('#chatlogs').load('main.php?id=', param);
})
});
</script>
Try this one:
function ajax_post(){
var param = document.getElementById('def').value;
$('#chatlogs').load('main.php?id='+param);
}

PHP/JQuery Dynamic Drop Down not sending variable via POST

I have a two step drop down menu. The second select is triggered with jquery. Since I have put this step in, my form does not pass the variable of the selected option. I expect it to just post but it doesn't. I have always had it loaded dynamically, so I didn't think that was the issue. I can't get it to work by passing it through the URL either.
Here is my code:
search.php
while($row = mysqli_fetch_array($result)) { ?>
<option value="<?=$row['id']?>"> <? echo $row['name'] ?></option>
<?php }
<form method = "post" action="index.php?page=users&id=<?php echo $row['id']; ?>">
<p>
<select name="list-select" id="list-select">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
</select>
<?php
echo '<select name="list-target" id="list-target">';
echo '</select>';
$installation_id = $_POST['list-target'];
mysqli_close($con);
?>
users.php:
$installation_id = $_POST['list-target'];
.js
$(document).ready(function($) {
$("#list-select").change(function() {
$("#list-target").load("index.php?page=search&svalue=" + $("#list-select").val());
});
});
Thanks.

Simple multiple selection checkbox dropdown that passed variables to the url

I want to create a very simple (no plugins and basic layout) for a dropdown menu that has checkboxes for options. For example, if C and F are selected and the form is submitted, I want the url to show /index.php?category1=3&&category2=6
<form action="" method="get">
<select multiple="multiple" name="category">
<optgroup label="Category1">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</optgroup>
<optgroup label="Category2">
<option value="4">D</option>
<option value="5">E</option>
<option value="6">F</option>
</optgroup>
</select>
<br><input type="submit" value="go">
</form>
I am not even sure how to start the jquery to pass the values to the url. Any help is much appreciated!
EDIT:
I figured I can do this using instead and simulate a dropdown with jquery.
Here is the jsfiddle: http://jsfiddle.net/k6R7g/
how can I show "2 Selected" instead of "Select..." when selections are made?
See code below, construct the url yourself and it will work. In case the form contains more values you can better add the category to the value of the option.
<form method="get">
<select multiple="multiple" name="category">
<optgroup label="Category1">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</optgroup>
<optgroup label="Category2">
<option value="4">D</option>
<option value="5">E</option>
<option value="6">F</option>
</optgroup>
</select>
<br><input type="button" value="go" onclick="clicked();">
</form>
<script type="text/javascript">
function clicked(){
var v = "";
$('select').find(":selected").each(
function(){
if (v != ""){
v += "&";
}
v += $(this).parent()[0].label + '=' + $(this).context.value;
}
);
window.location.href = "index.php?" + v;
}
</script>

How to prevent the set of a get variable?

I have a question. Let me explain this with an example.
I have this piece of code:
<form action="vehicles.php" method="get">
<span>Marca:
<select name="brand">
<option value="null" selected="selected"></option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model">
<option value="null" selected="selected"></option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="submit">
</form>
Is any way to prevent the assignment of those variables if the option selected is the "null" one?
If, for example, I select brand="Renault" and model="null" the url is
http://mywebpage.com/vehicles.php?brand=Renault&model=null
but it should be
http://mywebpage.com/vehicles.php?brand=Renault
I know how to unset the variables with "null" value after the form submission with PHP. Is any way to do it before the submission and after the variables are setted to "null".
I would like a cleaner url, free of "variable=null" results.
P/D: I don't have a native english speaking so feel free to edit my question. I wish you understand me.
I am afraid that you might need javascript to achieve what you want.
Take a look at this code:
<form action="vehicles.php" id="carForm" method="GET">
<span>Marca:
<select name="brand" id="brand">
<option value="none" selected>-</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model" id="model">
<option value="none" selected="selected">-</option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="button" value="Submit" onclick="submitFormFilter();" />
</form>
Javascript : code will be like this:
<script>
function submitFormFilter(){
var myForm = document.getElementById("carForm");
var carBrand = document.getElementById("brand");
var carModel = document.getElementById("model");
if(carBrand.value === "none" || null){
carBrand.parentNode.removeChild(carBrand);
}
if(carModel.value === "none" || null){
carModel.parentNode.removeChild(carModel);
}
myForm.submit();
}
</script>
http://jsfiddle.net/7xzKP/1/
you can it try here.
Include the value attribute of your option tags.
<option value="" selected="selected">-</option>
You should really do this for all of your options.
if you don't want to use $_GET superglobal because of the url extension that it comes with try using $_POST instead. It will not have a url extension and it can still store values that you can later retrieve. Just be sure to change your method to equal POST instead of GET.
So the code for the form tag would change to:
<form action="vehicles.php" method="POST">
And you can later access it by (for example):
echo $_POST['brand'];
or
echo $_POST['model'];
as well, you probably want to add a value param to the values that you have in your option tag.
EDIT-
I've added this new section since you don't want to use POST even though I think you should.
You can stay with the GET method by doing this line of code:
<form action="vehicles.php" method="GET">
<span>Marca:
<select name="brand">
<option value="none" selected>-</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model">
<option value="none" selected>-</option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="submit">
</form>
Let me know if that helps
You should set the value for the option tag of <select> :)
You's missing set value for option tag. you should set the value for option tag in select tag. With the blank value, you can assign it to a special value like zero(0)
<select name="model">
<option value="0" selected>-</option>
<option value="1">206</option>
<option value="2">Suran</option>
<option value="3">Passat</option>
<option value="4">Punto</option>
</select>
EDIT:
Ok. I got your point. You can import jquery to your page and make a check by javascript before you submit, if the value of select box is blank. you can remove it.
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$.ready(function(){
$("form").submit(function(){
if($("select[name='model']").val() == "0") $("select[name='model']").remove();
return true;
});
})
</script>

Categories