2016-10-23 5 views
0

Ich habe ein kleines Problem mit diesem Code darunter. Dies ist abhängig von Bibliothek namens Süße Alarm 2. Ich mag würde „inputOptions“ von PHP jetzt geladen habenHinzufügen von Eingabeoptionen, die vom Backend generiert werden

-Code ich habe:

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

Ich möchte Teil mit Eingabeoptionen haben, wie dies:

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

Ich weiß, das wird nicht funktionieren, aber Sie wissen, was ich meine. Kann mir tatsächlich jemand helfen? Ich würde schätzen, wirklich alle Tipps, wie diese

Antwort

0

zu tun, können Sie Ihre Optionen erhalten auf Wunsch über $.post in jQuery Ihre PHP-Datei zu senden:

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

}); 
} 
.... 

für PHP-Datei wie folgt sein sollte:

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

Wenn Sie nicht wollen, dynamisch die Optionen erhalten Sie th hinzufügen em, um Ihren Code, wenn Sie Ihr Seite:

<?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> 
+0

warum „{Optionen: Optionen}“ Ich bin nicht wirklich gut mit Ajax – Ethris

+0

zum Beispiel, wenn Sie einige andere wollen 'typ' Sie geschickt kann es mit POST und dann Ersetzen Sie "1" durch "$ _POST ['Optionen']" oder ignorieren Sie es einfach und ändern Sie es wie folgt: '$ .post (" option.php ", function (result) {...' –

Verwandte Themen