将数据从 javascript 发送到 html 并使用 php 获取
Send data from javascript to html and fetch with php
好的,我不知道这样做是否正确。如果没有,请给我一个如何做的例子。我如何获取 JS 中的数据并将其发送到 html?
JS
$(document).ready(function() {
var choosenYear = $('#choose_year');
$("#choose_year").select2({
data: [{
id: 0,
text: '2015'
}, {
id: 1,
text: '2014'
}],
val: ["0"]
}).select2('val', 0);
// Start Change
$(choosenYear).change(function() {
var choosenYear = $(choosenYear).select2('data').id;
$('#choosen_year').val(choosenYear);
}); //Change
});
HTML
<form class="form-inline well col-md-8" id="form-choose_usr" action="#" method="post" enctype="multipart/form-data">
//This is what i POST
<div>
<input type='hidden' class='col-md-4' id='choose_usr_email' name='choose_usr_email'>
</div>
<!-- Select2 choose_year -->
<div>
<input type='hidden' class='col-md-2' id='choose_year' name='choose_year'>
</div>
<!-- Select2 choose_month -->
<div>
<input type='hidden' id='choosen_year' name='choosen_year'>
<input type="submit" class="btn btn-info pull-right" value="Hämta" />
PHP
//And this is how i fetch it
$posted_choosen_year = $_POST['choosen_year'];
echo $posted_choosen_year;
好的,使用我刚刚创建的 fiddle 来测试:http://jsfiddle.net/yL6g087x/1/
如果显示该对象
$("#choosenYear").select2("data")
不存在……所以我想我会参考手册……rtm……他们的例子失败了……在两个层面上……他们的fiddle:http://jsfiddle.net/platypusman/xDUUg/
首先是 returns 404 的 js 和 css 文件...所以我链接了他们当前的..这个例子仍然无法工作..看起来 select2 插件是当前版本已损坏..
我可能是错的,但这是我从他们提供的示例中找到的
jquery 更改函数不是正确的事件:
$("#choosenYear").on("select2:select", function (e) { do stuff here... });
会的。有关更多信息,请参阅 "EVENTS" 下的 http://select2.github.io/examples.html。还有 运行 个示例将提供如何使用这个东西。
我更正了你的 fiddle 并得到它 运行 是这样的:
$(document).ready(function() {
function log(text) {
$('#chosen_year').append(text + '<br>');
}
$('#choose_year').select2()
.on("change", function(e) {
log(this.value);
})
});
所以,我并没有真正找到解决方法。所以我将代码更改为具有静态 HTML 值,而不是将值存储在 JS 文件中。
HTML
<form class="form-inline well col-md-8" id="form-choose_usr" action="#" method="post" enctype="multipart/form-data">
<select class="col-md-4" id="choose_year" name="choose_year" required>
<option></option>
<option value="2015">2015</option>
<option value="2014">2014</option>
</select>
<input type="submit" class="btn btn-info pull-right" value="Hämta" />
</form>
JS
$(document).ready(function(){
$('#choose_year').select2({
placeholder: 'Välj år..',
minimumResultsForSearch: -1
});
});
PHP
$posted_chosen_year = $_POST['choose_year'];
echo $posted_chosen_year;
好的,我不知道这样做是否正确。如果没有,请给我一个如何做的例子。我如何获取 JS 中的数据并将其发送到 html?
JS
$(document).ready(function() {
var choosenYear = $('#choose_year');
$("#choose_year").select2({
data: [{
id: 0,
text: '2015'
}, {
id: 1,
text: '2014'
}],
val: ["0"]
}).select2('val', 0);
// Start Change
$(choosenYear).change(function() {
var choosenYear = $(choosenYear).select2('data').id;
$('#choosen_year').val(choosenYear);
}); //Change
});
HTML
<form class="form-inline well col-md-8" id="form-choose_usr" action="#" method="post" enctype="multipart/form-data">
//This is what i POST
<div>
<input type='hidden' class='col-md-4' id='choose_usr_email' name='choose_usr_email'>
</div>
<!-- Select2 choose_year -->
<div>
<input type='hidden' class='col-md-2' id='choose_year' name='choose_year'>
</div>
<!-- Select2 choose_month -->
<div>
<input type='hidden' id='choosen_year' name='choosen_year'>
<input type="submit" class="btn btn-info pull-right" value="Hämta" />
PHP
//And this is how i fetch it
$posted_choosen_year = $_POST['choosen_year'];
echo $posted_choosen_year;
好的,使用我刚刚创建的 fiddle 来测试:http://jsfiddle.net/yL6g087x/1/
如果显示该对象
$("#choosenYear").select2("data")
不存在……所以我想我会参考手册……rtm……他们的例子失败了……在两个层面上……他们的fiddle:http://jsfiddle.net/platypusman/xDUUg/
首先是 returns 404 的 js 和 css 文件...所以我链接了他们当前的..这个例子仍然无法工作..看起来 select2 插件是当前版本已损坏..
我可能是错的,但这是我从他们提供的示例中找到的
jquery 更改函数不是正确的事件:
$("#choosenYear").on("select2:select", function (e) { do stuff here... });
会的。有关更多信息,请参阅 "EVENTS" 下的 http://select2.github.io/examples.html。还有 运行 个示例将提供如何使用这个东西。
我更正了你的 fiddle 并得到它 运行 是这样的:
$(document).ready(function() {
function log(text) {
$('#chosen_year').append(text + '<br>');
}
$('#choose_year').select2()
.on("change", function(e) {
log(this.value);
})
});
所以,我并没有真正找到解决方法。所以我将代码更改为具有静态 HTML 值,而不是将值存储在 JS 文件中。
HTML
<form class="form-inline well col-md-8" id="form-choose_usr" action="#" method="post" enctype="multipart/form-data">
<select class="col-md-4" id="choose_year" name="choose_year" required>
<option></option>
<option value="2015">2015</option>
<option value="2014">2014</option>
</select>
<input type="submit" class="btn btn-info pull-right" value="Hämta" />
</form>
JS
$(document).ready(function(){
$('#choose_year').select2({
placeholder: 'Välj år..',
minimumResultsForSearch: -1
});
});
PHP
$posted_chosen_year = $_POST['choose_year'];
echo $posted_chosen_year;