Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11
  1. #1

    almeno un checkbox checked

    Stò cercando di costruire una funzione che mi controlli che almeno una delle checkbox che fanno parte del "gruppo" e solo se almeno una è selezionata rimanda alla pagina 2 altrimenti visualizza un alert di errore. Purtroppo non conosco quasi niente il javascript e stò provando a modificare vari script trovati su internet ma ciccia. Qualcuno può aiutarmi modificando il codice?

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Documento senza titolo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function Controllo(f) {
    }
    // -->
    </script>
    </head>
    <body>
    <div align="center">
    <form name="form1" action="pagina2.php" method="post" onsubmit="return Controllo(this)">
    <table width="200" border="1">
    <tr>
    <td><input type="checkbox" name="n1" id="gruppo"></td>
    <td><input type="checkbox" name="n2" id="gruppo"></td>
    <td><input type="checkbox" name="n3" id="gruppo"></td>
    </tr>
    <tr>
    <td colspan="4">
    <input type="submit" value="Invia">
    </td>
    </tr>
    </table>
    </form>
    </div>
    </body>
    </html>
    Prima o poi anch'io vi insegnerò qualcosa

  2. #2
    Utente di HTML.it L'avatar di Pastore12
    Registrato dal
    Oct 2008
    Messaggi
    1,051
    Ecco una possibile soluzione

    Codice PHP:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Documento senza titolo</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    SCRIPT LANGUAGE="JavaScript">
    function 
    Controllo(f)
    {
        
    alert ("controllo");
        var 
    ckIndex = -1;
        for (var 
    i=0i<3i++)
        {
            var 
    document.getElementById("ck"+i).checked;
            if (
    b)
            {
                
    ckIndex i;
                break;
            }

        }
        if (
    ckIndex>=0)
        {
            
    alert ("Selezionato"+ckIndex);
            
    document.form1.action="pagina0.html"
        
    }
        else
        {
            
    alert ("Nessuno selezionato");
            
    document.form1.action="pagina1.html"
        
    }

    }
    </script>
    </head>
    <body>
        <div align="center">
        <form name="form1" action="" method="post" onsubmit="return Controllo(this)">
        <table width="200" border="1">
            <tr>
                <td><input type="checkbox" name="gruppo" id="ck0"></td>
                <td><input type="checkbox" name="gruppo" id="ck1"></td>
                <td><input type="checkbox" name="gruppo" id="ck2"></td>
            </tr>
            <tr>
                <td colspan="4">
                    <input type="submit" value="Invia">
                </td>
            </tr>
        </table>
        </form>
        </div>
    </body>
    </html> 
    Gli alert li lascio rimuovere a te..

    Un'altra cosa! l'attributo ID di un elemento DEVE ESSERE UNIVOCO!!!!!
    Sul name invece hai più possibilità.. ciao!
    "Ethics are to me something private. Whenever you use it as an argument for why somebody_else should do something, you’re no longer being ethical, you’re just being a sanctimonious dick-head"
    Linus Torvalds

  3. #3
    Thenks Funge!!! Ho potuto verificare solo oggi.

    Mi trovo però adesso nella condizione di dover recuperare nella pagina2 i valori delle varie check.

    Nel caso in cui avessi messo (come ho sempre fatto fino ad adesso), nomi diversi delle checkbox, recuperavo il loro valore facendo un ciclo e controllando una ad una ogni variabile n1, n2, n3, nn.

    Adesso invece non ho idea di come recuperare tale valore:

    Se ho invece:

    <input type="checkbox" name="gruppo" id="ck0">
    <input type="checkbox" name="gruppo" id="ck1">
    <input type="checkbox" name="gruppo" id="ck2">

    come vengo a conoscenza delvalore della 1°, 2°, e 3° checkbox?
    Prima o poi anch'io vi insegnerò qualcosa

  4. #4
    Utente di HTML.it L'avatar di Pastore12
    Registrato dal
    Oct 2008
    Messaggi
    1,051
    Dovrebbe bastare:

    Codice PHP:
    function Controllo(f)
    {
        var 
    ckIndex = -1;
        var 
    parametro "";

        for (var 
    i=0i<3i++)
        {
            var 
    document.getElementById("ck"+i).checked;
            if (
    b)
            {
                
    ckIndex i;
                
    parametro += " ",
            }

        }
        if (
    ckIndex>=0)
            
    document.form1.action="pagina0.html?value="+parametro;
        else
            
    document.form1.action="pagina1.html";


    nella pagina pagina0.html ti ritrovi un parametro "value", passato via get, che contiene i valori checkati.
    "Ethics are to me something private. Whenever you use it as an argument for why somebody_else should do something, you’re no longer being ethical, you’re just being a sanctimonious dick-head"
    Linus Torvalds

  5. #5
    ok, ma cosè un' array? Perchè se i valori checcked sono più di uno o addirittura tutti???
    Prima o poi anch'io vi insegnerò qualcosa

  6. #6
    Utente di HTML.it L'avatar di Pastore12
    Registrato dal
    Oct 2008
    Messaggi
    1,051
    Scommetti che se provi lo scopri?

    Ciao!
    "Ethics are to me something private. Whenever you use it as an argument for why somebody_else should do something, you’re no longer being ethical, you’re just being a sanctimonious dick-head"
    Linus Torvalds

  7. #7
    ok, appena ho un minuto provo e vi faccio sapere!!!
    Prima o poi anch'io vi insegnerò qualcosa

  8. #8
    Sinceramente NON SOLO non riesco a recuperare il valore di value in nessun modo (uso php) ma non mi funziona neppure quest'ultimo script che hai postato e del quale ho modificato solo l'estensione da html a php.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Documento senza titolo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <SCRIPT LANGUAGE="JavaScript">
    function Controllo(f)
    {
    var ckIndex = -1;
    var parametro = "";

    for (var i=0; i<3; i++)
    {
    var b = document.getElementById("ck"+i).checked;
    if (b)
    {
    ckIndex = i;
    parametro += i + " ",
    }

    }
    if (ckIndex>=0)
    document.form1.action="pagina0.php?value="+paramet ro;
    else
    document.form1.action="pagina1.php";

    }
    </script>
    </head>
    <body>
    <div align="center">
    <form name="form1" action="" method="post" onsubmit="return Controllo(this)">
    <table width="200" border="1">
    <tr>
    <td><input type="checkbox" name="gruppo" id="ck0"></td>
    <td><input type="checkbox" name="gruppo" id="ck1"></td>
    <td><input type="checkbox" name="gruppo" id="ck2"></td>
    </tr>
    <tr>
    <td colspan="4">
    <input type="submit" value="Invia">
    </td>
    </tr>
    </table>
    </form>
    </div>
    </body>
    </html>
    Questo è attualmente lo script...
    Prima o poi anch'io vi insegnerò qualcosa

  9. #9
    Utente di HTML.it L'avatar di Pastore12
    Registrato dal
    Oct 2008
    Messaggi
    1,051
    Salva come pagina0.php

    nella funzione che ti avevo postato c'era un
    ,
    invece di
    ;
    ...

    Codice PHP:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title>Documento senza titolo</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

        <script language="javascript">
        function controllo(f)
        {
            var ckIndex = -1;
            var parametro = "";
        
            for (var i=0; i<3; i++)
            {
                var b = document.getElementById("ck"+i).checked;
                if (b)
                {
                    ckIndex = i;
                    parametro += i + " ";
                }
            }
            if (ckIndex>=0)
            {
                f.action="pagina0.php?value="+parametro;
            }
            else
            {
                f.action="pagina0.php?value=null";
            }
            
            alert ("action: "+f.action);
            f.submit();
        
        }
    </script>

    </head>
    <body>

    <?php
        
    echo "Valore letto: $_GET[value]";
    ?>    


    <div align="center">
        <form name="form1" action="" method="post" onsubmit="return controllo(this)">
            <table width="200" border="1">
                <tr>
                    <td><input type="checkbox" name="gruppo" id="ck0"></td>
                    <td><input type="checkbox" name="gruppo" id="ck1"></td>
                    <td><input type="checkbox" name="gruppo" id="ck2"></td>
                </tr>
                <tr>
                    <td colspan="4">
                        <input type="submit" value="Invia">
                    </td>
                </tr>
            </table>
        </form>
    </div>
    </body>
    </html>
    "Ethics are to me something private. Whenever you use it as an argument for why somebody_else should do something, you’re no longer being ethical, you’re just being a sanctimonious dick-head"
    Linus Torvalds

  10. #10
    Ok l'ho un pò modificato, non che così non funzionasse, ma l'ho adattato alla mia fattispecie.

    GrazieMille
    Prima o poi anch'io vi insegnerò qualcosa

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.