dongtuoao7987 2016-10-23 15:20
浏览 95
已采纳

添加从后端生成的输入选项

I have a little problem with this code included beneath. This is function of library called Sweet Alert 2. I'd like to have "inputOptions" loaded from php

Code I have right now:

    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
  })
});
}

I'd like to have part with input Options like this:

<?php
$ct = mysql_query("SELECT * FROM db WHERE typ = 1",$link);                                             
while($row = mysql_fetch_array($ct)){
echo $row['id'] : $row['country'];
}
?>

I know this won't work but you know what I mean. Can someone actually help me? I would genuinely appreciate any tips on how to do this

  • 写回答

1条回答 默认 最新

  • dongyilai4214 2016-10-23 15:43
    关注

    you can get your options by sending request via $.post in jQuery to your php file :

    function pick(){
    $.post("option.php", {options: options}, function(result){
            if(result){
    swal({
      title: 'Choose country',
      input: 'select',
      inputOptions: result,
      .
      .
      .
    
     });
    }
    ....
    

    for php file should be like this:

    // 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);
    

    EDIT

    If you don't want to dynamically get the options you can add them to your code when you make your page:

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题