Change value of a variable with a PHP check box

0

When I click on the option I want to send by get, a value. Type something like this

    <form action='_cal.php?change=1' method="get">
        <input type="checkbox" name="change" value="change">Alterar<br>
    </form>

and receive in PHP

$change=(isset($_GET['change']) ? $_GET['change'] : '0');

I get a link, I wanted to change it for a checkbox now.

    
asked by anonymous 03.11.2014 / 15:55

2 answers

1

Essentially what you are looking for is to submit the form when someone clicks the checkbox instead of using links.

For this purpose, you can add a onChange attribute to the checkbox element that will execute JavaScript responsible for triggering the form submission:

<form action='_cal.php' method="get">
    <input type="checkbox" name="change" value="1" onChange="this.form.submit()"> Alterar
</form>

Notice that I also changed the form's address because when submitting the same via GET , a string in the format NVP of the fields present in it.

Your checkbox is called change and with the 1 value, in combination with the _cal.php URL where the form will submit the information, you will get:

_cal.php?change=1

Additional note:

To check for the presence of the URL variable change , you can simplify your code to:

$change = (int)isset($_GET['change']);

To explain, the function isset() returns a boolean TRUE or FALSE . Preceded by (int) will pass the boolean to its numeric representation 1 or 0 .

View demo on Ideone .

    
04.04.2015 / 02:07
0

Here is the code of the index.php page I made for testing, I just used php, html and jquery:

<?php
    // Código php pra dar a resposta do ajax
    // Se o get for setado (se o checkbox for marcado)
   if (isset($_GET["change"])) { 
        // Imprime o get (no caso, retorna o valor pra nossa variável data
        echo $_GET["change"];

        // Dá o exit, 
        // pois aqui na minha máquina ele estava retornado o código fonte de toda
        // a página html
        exit();
   } 
?>
<html>
    <head>
        <title>Teste get</title>
    </head>
    <body>
        <form action='index.php' method="get" id="form">
            <input type="checkbox" name="change" value="1" id="change">Alterar<br>
        </form>

        <div id="resultado" style="margin: 20px auto; width: 300px"></div>

        <script src="jquery.js"></script>
        <script>
            $(document).ready(function() {

                // Evento JQuery que captura as mudanças do checkbox - pode ser substituído pelo click()
                $('#change').change(function() {

                    // Se o checkbox estiver :checked
                    if ( this.checked ) {

                        // Guarda o value do input numa variável
                        var chkData = $('#change').val();

                        // Inicia evento ajax, aqui é basicamente a resposta que você procura
                        $.ajax({
                            url: $('#form').attr("action") + "?" + $('#form').serialize(),  // Pega a URL no formato do get
                            type: 'GET',    // Define o tipo do submit
                            data: chkData,  // Aplica o valor do checkbox à variável data

                            // Caso o request termine em sucesso, mostra o resultado recebido na div#resultado
                            success: function(data) {
                                $("#resultado").html("Funcionou!!<br />" + data);
                            },
                            error: function(e) {
                                console.log(e.message);
                            }
                        }); 
                    }
                });
            });
        </script>
    </body>
</html>
    
03.11.2014 / 18:21