I am using the popSelect jquery plugin (http://jquer.in/popSelect/)
This is the code I use :
<select name="tun" id="tun" class="form-control">
<option value='op1'>op1</option>
<option value='op2'>op2</option>
<option value='op3'>op3</option>
<option value='op4'>op4</option>
</select>
And this is the javascript code to run the plugin
$('#tun').popSelect({
placeholderText: 'tun',
showTitle: false ,
autoIncrease: true
});
I want to get the selected items once the form is submitted (post). I tried this but I got an empty array.
$tun=$_POST['tun']
In the documentation they said that this code allow to returns the current selected values.
$(elem).popSelect('value')
This is with javascript, but I want these elements in my PHP code.
Can someone help me ?.
Let assume you have the following form:
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
Select a color:
<select id="myselect" name="colors[]" multiple>
<option value="green">Green</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="violet">Violet</option>
<option value="orange">Orange</option>
<option value="white" selected="selected">White</option>
</select>
<input type="submit">
</form>
And you have initialized the popSelect plugin in javascript as follows:
$(function() {
$("#myselect").popSelect({
showTitle: false,
maxAllowed: 4
});
});
Then, inside your welcome.php file you can fetch what the user has selected by accessing the $_POST array variable of the post request:
$colorsSelected = $_POST['colors'];
Related
Hey guys I'm using the Laravel framework. I fetched data from my database and populated my drop down list like this.
<select class="form-control" id="username" name="username">
<option value="" selected>Select User</option>
#foreach($getUsers as $list)
<option value="{{$list->id}}" >{{$list->name}}</option>
#endforeach
</select>
I want to be able to click one of the options in the drop down list and send the value through a url (preferably using href) and reload the page.
You could use a simple javascript function bound to the onchange event of the select menu like this.
<script>
function offyoupop(e){
location.search='username='+e.target.value;
}
</script>
<select name='username' id='username' class='form-control' onchange='offyoupop(event)'>
<option>Please select
<option value='1'>One
<option value='2'>Two
<option value='3'>Three
<option value='4'>Four
<option value='5'>Five
</select>
I'm trying to do something very simple in PHP, but keep getting an error message. Essentially, when someone selects "Cat", I want "Miaow" to appear.
My idea was:
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<?php if ($_POST['demo'] == 'Cat') { echo 'Miaow'; } ?>
However, in PHPFiddle,
I get 'E_NOTICE : type 8 -- Undefined index...'
as soon as the code runs. Am I missing something basic? Thanks!
Your form might be passing data by $_GET instead of $_POST.
Did you specify the method ?
<form method="post" action="index.php">
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<input type="submit" value="Submit">
</form>
You can var_dump($_POST); and var_dump($_GET); to see what those variables contains in your PHP file.
Or you can do it in javascript like this :
function animal(value) {
if(value == "Cat") {
document.getElementById("myAnimal").innerHTML = "Miaouw";
} else {
document.getElementById("myAnimal").innerHTML = "Rooooah";
}
}
<form action="#">
<select name="demo" onchange="animal(this.value)">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
</form>
<span id="myAnimal"></span>
Is it even possible to do this using PHP so that "Miaow" comes up automatically on select, rather than having to submit the form?
You are looking for JavaScript code, not PHP. Here is a jQuery example:
$(document).on('change', '.animals', function(){
$('.noise-target').html( $('option:selected', this).data('noise') );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="animals">
<option>Select</option>
<option data-noise="Woof">Dog</option>
<option data-noise="Meow">Cat</option>
</select>
<div class="noise-target"></div>
Maybe this will help, I write some block as far as I understand...
<form action="#" method="post">
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<input type="submit" name="sub" />
</form>
<?php
if(isset($_POST['sub'])){
if($_POST['demo'] == "cat"){
echo "Miao";
}
}
?>
I have a select tag with different values as shown below.
I want to send the selected value to a PHP page as a POST variable or any other way.
Is this possible?
<select id="city" name="city" align="left">
<option value="Pune">Pune</option>
<option value="Bhopal">Bhopal</option>
<option value="Mumbai">Mumbai</option>
<option value="New Delhi">New Delhi</option>
</select>
Put your select inside a form. Give the form an action that is a php file. give it the method of post and include a <input type="submit" value="Submit"> inside the form as well.
Then, in your php file you will access the $_POST array
echo $_POST['city'];
If you'd like to submit it without the button, you'll need to use javascipt's onChange event.
Yes, and it's very simple. You have to use the from tag : http://www.w3schools.com/html/html_forms.asp and learn a little of PHP and HTML basic interaction
<form action = 'myPage.php' method = 'post'>
<select id="city" name="city" align="left">
<option value="Pune">Pune</option>
<option value="Bhopal">Bhopal</option>
<option value="Mumbai">Mumbai</option>
<option value="New Delhi">New Delhi</option>
</select>
<input type = 'submit' name = 'send' value = 'send'/>
</form>
(I also added a button son send data)
in myPage.php :
<?php
if(isset($_POST['city'])){
echo isset($_POST['city'];
}
?>
I'm still learning to use PHP with MySQL tables and I'm sorry if this is a novice question but how would I change the dropdown code below to be able to insert normal a href links (with an image or text) that link to the playerMenu.php page? Here's the dropdown menu code I have:
<form action="playerMenu.php" method="get">
<select id="players" name="selectvalue" onchange="showMe(this.value);">
<option value="">Select</option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
<input type="submit" value="Submit">
<form>
Submit
Thanks in advance.
Here's how using JQuery. It works! Try it out!
<!-- Your Form -->
<form>
<select id="players" name="selectvalue" onchange="showMe(this.value);">
<option value="">Select</option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
<input id="button" type="button" value="Submit">
<form>
<!-- Include JQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
var sub_link = "playerMenu.php?electvalue="
$("#button").click(function() {
sub_link = sub_link + $("#players").find('option:selected').text();
// Here's your link
alert(sub_link);
// And now we do a javascript redirect
window.location.replace(sub_link);
});
</script>
From the PHP side, once you submit that form then the url will be
playerMenu.php?players=1&submit=Submit
If the option A was chosen. You can remove the onchange and onclick javascript, if this is what you want to achieve.
From there you have passed in the value of the players select box. You would want to then likely save this as a variable and what you want with the code. If you are doing something with your database then make sure to santize the variable as well.
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>