Passing dropdown value to session without refreshing the page - php

I need to pass select value to a session without refresh the page.
I think onchange event can be used.
Actually, I can put select value into text field.
HTML
Select:
<select id="dropdown1" class="select" tabindex="2" onchange="run(this)">
<option>Select</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
TextBox:
<input type="text" id="car" name="car" id="car" value="<?=$_POST[car]?>" disabled />
JavaScript
function run(sel) {
var i = sel.selectedIndex;
if (i != -1) {
document.getElementById("car").value = sel.options[i].text;
}
}
Question
How to set value on a variable when select value from dropdown?
$_SESSION['car'] = DROPDOWN VALUE

if you can use jQuery, this will work:
function run(sel) {
var i = sel.selectedIndex;
if (i != -1) {
document.getElementById("car").value = sel.options[i].text;
$.ajax({
type: "POST",
url: "urPHPPage.php",
data: { car: sel.options[i].text}
}).done(function( msg ) {});
}
}
and in your PHP file
if(isset($_POST["car"])){
$_SESSION['car'] = $_POST["car"]; //offcourse you will have to do some sanitization here
}

Since your session is a server-side event and your onchange event is a client-side event, you'll need to use AJAX in order to change your dropdown options without refreshing the page.
You will need to run a JavaScript function on onchange event.
In that function you need to use AJAX in order to connect to your browser/database and save the session['car']
Hope it helps.

Related

How to use onchange with serializeArray in jquery?

I've created a form with 3 select part and I tried to create an array with serializeArray. I want to use jquery ajax to post this array to my php file. But I don't want use submit. when I had just one select tag, I used this code
<form>
<select onchange="myfunction(str)">
<option value="">num</option>
<option value="123">123</option>
<option value="133">133</option>
</select>
</form>
In my ajax code, I used open("GET","myphpfile.php?q="+str,true) and send() without jquery. but now I have 3 select tag and I don't know how too use serializeArray()(or serialize()) with jquery.
this is my new form
<form>
<select name="num1">
<option value="">num1</option>
<option value="12">12</option>
<option value="13">13</option>
</select>
<select name="num2">
<option value="">num2</option>
<option value="123">123</option>
<option value="133">133</option>
</select>
<select name="num3">
<option value="">num3</option>
<option value="12345">12345</option>
<option value="12346">12346</option>
</select>
</form>
the second part of my question is how to write my php code to echo my array. I think it should be something like this
<?php
$myarr = array();
$myarr = $_GET["str"]//or $_POST['str']
echo $myarr[0];
?>
Thanks a lot! and by the way, English is not my native language; please excuse typing errors.
Okay, if I get your question correctly, here is what you're trying to do:
Hooking events on dynamically added fields
$("form").on("change", "select", function() {
var name = $(this).prop("name");
console.log("Select-Name: " + name);
// if you use the plugin I mentioned further down and you'll need to serialize
// all fields already here, you can use the plugin's .fieldSerialize method
})
Get detailed information here: https://api.jquery.com/on/
Serializing forms
Easiest thing here would be to work with this jQuery Form-Plugin:
http://malsup.com/jquery/form/
Create a json-dataset with almost no effort like this:
$("form").ajaxForm({
dataType: "json",
success: function(data) { sendToServer(data); }
});
Working with the data in the backend
The plugin also allows you to work on the server with the script you provided ($_POST["value"])
use
var arrayForm = $('form select').serializeArray();
and then
var paramForm = $.param(arrayForm);
like http://jsfiddle.net/LBKeQ/
then you can use
$.ajax({
type: 'POST',
async:true,
cache: false,
data: paramForm,
success:function (data, textStatus) {
console.log(data);
},
url:"myphpfile.php"
});

Populate a PHP variable from a listbox value with AJAX

I retrieve the value of a select box in this way which goes to populate a input field:
<script>
$(document).ready( function ()
{
$('#dropdown_selector').change(function()
{
var option = $(this).find('option:selected').val();
$('#showoption').val(option);
});
});
</script>
<select id="dropdown_selector">
<option value="Option1">Option1</option>
<option value="Option2">Option2</option>
<option value="Option3">Option3</option>
</select>
<label>Value</label>
<input type="text" name="name" id="showoption" disabled="disabled" />
</label>
But my problem is how to retrieve that value and use it directly in a PHP variable,instead of populating the input field(with id showoption).
Something similar as:
$variable=(var selected in select box)
Thanks for your help!!!
This jQuery code is triggered when an option is selected and then stores the value of the option into variable valu. After that it sends the variable to your php document via Ajax post.
$('select').change(function(){
var valu=$( "select option:selected" ).attr('value');
$.ajax({
type:"POST",
url:"yourphpscript.php",
data: {values:valu}
});
});
http://jsfiddle.net/joey6978/peq74/ :this fiddle shows the select choosing trigger.
In your php script you should have the post method with the name values to recieve your data.
$myvariable=$_POST['values'];

Display Javascript variable into PHP variable

In my form action there's a url: www.url.com/?quantity=$quantity
And in the form there's a select box where customers choose the quantity.
<form method="post" name="jform" action="www.url.com/?quantity=$quantity">
<select class="font_12" id="quantity" name="quantity">
<option value="10">10 PCs</option>
<option value="25">25 PCs</option>
<option value="50">50 PCs</option>
<option value="99">99 PCs</option>
</select>
I am trying to get the value in the select box using ajax, and then display into the action form url. I did a alert and it works, I am getting the value of the select box.
But I don't know how to put this vaue into the PHP varaible $quantity?
Here's my Ajax code:
$('#quantity').on('change', function() {
var val = $(this).val();
if(val != '') {
$.get('index.php', {'quantity' : val}, function(resp) {
alert(val);
});
}
});
Actually I want it to change the php variable right away when the quantity in the select box change before submitting the form.
Any help?
Use $_GET
If your URL is ?quantity=### then just use $_GET['quantity'] in your PHP code.
To change the action attribute on the form when you change the quantity you can just put the following inside your onchange event:
$('form[name="jform"]').attr('action','http://url.com/?quantity=' + val);

Related dropdown using javascript and php

I have this script and I want to get the value of drop down select list in the PHP and check the if (jws1 != "") then show 1 to 10 value in the second drop down box using of for loop...
JavaScript code:
<script language="javascript" type="text/javascript">
function test()
{
var e = document.getElementById("JWSections");
var Sections = e.options[e.selectedIndex].value;
alert(Sections);
}
</script>
HTML code:
<select name="JWSections" id="JWSections" onchange="test();">
<option value="">Select Sections</option>
<option value="jws1">Section 1.1</option>
<option value="jws2">Section 1.2</option>
<option value="jws3">Section 1.3</option>
<option value="jws4">Section 1.4</option>
</select>
I want to get the value like jws1, and check in the PHP.
For PHP to be able to do anything with your HTML form data, you'll have to submit it to PHP.
You can either submit the form itself or use AJAX to pass in the value of jws1 and build the options from the result. Jquery is pretty good with doing that kind of thing, all you gotta do is set your AJAX request's data type to HTML and assign the data from the "success" callback to your destination drop down list.
<select name="JWSections" id="JWSections" onchange="test(this);">
<option value="">Select Sections</option>
<option value="jws1">Section 1.1</option>
<option value="jws2">Section 1.2</option>
<option value="jws3">Section 1.3</option>
<option value="jws4">Section 1.4</option>
</select>
<select name="JWSections" id="secondDropDown"> </select>
<script language="javascript" type="text/javascript">
function test(dropdown)
{
var selectedjws = dropdown.options[dropdown.selectedIndex].value;
var secondDropDown = document.getElementById('secondDropDown');
$.post('path', { selectedjws : selectedjws }, function(key, value){
// Remove all options from second dropdown
for(var i = 0; i < secondDropDown.options.length; i++){
secondDropDown.remove(i);
}
// Add options by key value pair returned from server
secondDropDown.add(new Option(value, key))
});
}
</script>
jsfiddle: http://jsfiddle.net/KTq6y/1/

How to send a selected value of an option to a PHP function as a argument

I want to send the value of selected index to a function, where all option values are coming through database.
<select onchange="get_api_key();" name="service">
<option value="">Select your Service</option>
<optgroup label="Apple Iphone">
<option value="53">Apple </option>
<option value="72">Ipad </option>
<option value="77">iPhone</option>
<option value="24">Iphone</option>
</optgroup>
</select>
for the selected index use this.selectedIndex instead of this.value
see http://jsfiddle.net/corinnekm/FZY2v/2/
UPDATE: You can't use a javascript variable inside a PHP function. However, you can use Ajax to change a javascript variable to a PHP variable and then use it for your purpose.
<script type="text/javascript">
$(function(){
var selVal = $('select[name="service"]').val();
// send selVal as a PHP variable using Ajax
$.ajax({
type:'post',
url:'getvalue.php',
data:'selVal='+selVal,
success:function(data){
//do something if you want to with returned data
}
});
});
</script>
Then in getvalue.php page,
$val = $_POST['selVal'];
// proceed to use $val for whatever purpose you want in this page

Categories