我对下面包含的代码有一个小问题。这是一个名为Sweet Alert 2的库的功能。我想有" inputtoptions "从php
加载我现在的代码:
function pick(){
swal({
title: 'Choose country',
input: 'select',
inputOptions: {
'SRB': 'Serbia',
'UKR': 'Ukraine',
'HRV': 'Croatia'
},
inputClass: 'form-control select',
confirmButtonColor: '#78339b',
inputPlaceholder: 'choose country',
showCancelButton: true,
}).then(function(result) {
swal({
type: 'success',
html: 'You selected: ' + result
})
});
}
我希望有这样的部分输入选项:
<?php
$ct = mysql_query("SELECT * FROM db WHERE typ = 1",$link);
while($row = mysql_fetch_array($ct)){
echo $row['id'] : $row['country'];
}
?>
我知道这行不通,但你知道我的意思。有人能帮帮我吗?如果有任何关于如何做到这一点的建议,我将非常感谢。
你可以通过jQuery中的$.post
向你的php文件发送请求来获得你的选项:
function pick(){
$.post("option.php", {options: options}, function(result){
if(result){
swal({
title: 'Choose country',
input: 'select',
inputOptions: result,
.
.
.
});
}
....
PHP文件的应该像这样:
// you can get the data you need by senting option to your page $_POST['options']
$ct = mysql_query("SELECT * FROM db WHERE typ = 1",$link);
while($row = mysql_fetch_array($ct)){
$data[$row['id']] = $row['country'];
}
echo json_encode($data);
编辑
如果你不想动态获取这些选项,你可以在创建页面时将它们添加到代码中:
<?php
$ct = mysql_query("SELECT * FROM db WHERE typ = 1",$link);
while($row = mysql_fetch_array($ct)){
$data[$row['id']] = $row['country'];
}
?>
<scirpt>
function pick(){
swal({
title: 'Choose country',
input: 'select',
inputOptions: <?php echo json_encode($data); ?>,
inputClass: 'form-control select',
</scirpt>