Domanda

Ci scusiamo per il titolo cattivo, ma non so come nominare questo. Il mio problema è che ogni volta che passo un valore da una casella di selezione attivo questo evento jQuery per controllare le caselle di controllo. Bassicamente I Echo $ Res []; a selecctedgr.php. Devo usare JSON? E come posso farlo?

Pagina principale:

$("#group_name").change(function(){
    var groupname = $("#group_name").val();
    var selectedGroup = 'gr_name='+ groupname;
    $.post("selectedgr.php", {data: selectedGroup}, function(data){
        $.each(data, function(){
            $("#" + this).attr("checked","checked");
        });
    },"json");


});

PHP (selectedGr.php):

<?php
    include_once '../include/lib.php';
    $gr_name=mysql_real_escape_string($_POST['gr_name']);

    $sqlgr = "SELECT * FROM PRIVILLAGE WHERE MAINGR_ID=".$gr_name;
    $resultgr = sql($sqlgr);
    while($rowgr = mysql_fetch_array($resultgr)){
        $res[] = $rowgr['ACT_ID'];
    }

    echo $res[];
?>
È stato utile?

Soluzione

Cambia l'ultima riga nel campione PHP (echo $res[];) a:

echo json_encode($res);

json_encode() La pagina manuale ti dirà di più.

Inoltre, come dice @Unicron, è necessario convalidare la variabile $ GR_NAME prima di passarla all'istruzione SQL.

Potresti usare:

if(isset($_POST['gr_name'])) {
    $gr_name = mysql_real_escape_string($_POST['gr_name']);
}

Vedere: http://php.net/manual/en/function.mysql-real-escape-string.php Per ulteriori informazioni nel manuale PHP.

Altri suggerimenti

Puoi usare JSON_ENCODE funzione per convertire i dati arbitrari in JSON. Supponendo che tu voglia restituire una serie di stringhe, ecco come userai JSON_ENCODE:

<?php
    include_once '../include/lib.php';
    $res = array(); // initialize variables
    $sqlgr = sprintf("
        SELECT ACT_ID
        FROM PRIVILLAGE
        WHERE MAINGR_ID=%d
        ",
        $_POST['gr_name']
    ); // only select those columns that you need
       // and do not trust user input
    $resultgr = sql($sqlgr);
    while($rowgr = mysql_fetch_array($resultgr)){
        $res[] = $rowgr['ACT_ID'];
    }
    echo json_encode($res); // use json_encode to convert the PHP array into a JSON object
                            // this will output something like ['foo', 'bar', 'blah', 'baz'] as a string
?>

Sul lato client puoi usare Metodo jquery.post, come questo:

<script type="text/javascript">
$("#group_name").change(function () {
    $.post("selectedgr.php", {
        gr_name: $(this).val()
    }, function (data) {
        // console.log(data);
        // jQuery will convert the string "['foo', 'bar', 'blah', 'baz']" into a JavaScript object
        // (an array in this case) and pass as the first parameter
        for(var i = 0; i < data.length; i++) {
            $("#" + data[i]).attr("checked", "checked");
        }
    }, "json");
});
</script>

Se vuoi usare JSON, basta usa echo json_encode($res);Ma non capisco davvero cosa otterrai se il tuo codice funziona ora, dal momento che dovrai comunque fare un po 'di elaborazione nel JavaScript per gestire il risultato.

Ho trovato il mio problema principale come di seguito

invece di (prima):

 $.post("selectedgr.php", {data: selectedGroup}, function(data){

fallo (dopo):

$.post("selectedgr.php", selectedGroup, function(data){

Perdona il mio male. Ahh ya ragazzi, per quanto riguarda la fuga su mysql in realtà #group_name non è alcun campo di input ma una casella selezionata. Apprezzo per ogni commento, suggerimento e guida.

Eric.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top