简体   繁体   中英

Get automatic selected checkbox and next input text value in php

I want to get automatic the checkbox value and the next input text value when checked. Please don't tell me 'give name like name1, name2 etc' because I want to get data from my database.

  <?php
    if(isset($_GET['submit'])){

    $amount = $_GET['amount'];
    $checked= $_GET['check'];

    echo "You selected ".$amount.". of ".$checked."";
    }
    ?>

    <form>


    <input type="checkbox" value="Coke" name="check[]">Coke<input type="number" name="amount[]"><br>
    <input type="checkbox" value="Fanta" name="check[]">Fanta<input type="number" name="amount[]"><br>
    <input type="checkbox" value="Water" name="check[]">Water<input type="text" name="amount[]"><br>

    <input type="submit" value='Verder' name="submit">
    </form>

You can do this using jquery:

$("input[type=checkbox]" ).on( "click", function(){
    if($(this).is(':checked')){
        var value_of_checkbox = $(this).val();
        var value_of_next_input = $(this).next().val();

        //DO more stuff

    }
});

Hope this will helps you.You can achieve this by using jquery.

$(function(){
    $("input[name='check[]']").click(function(){
        var isChecked = $(this).is(":checked");
        if (isChecked) {
            var nextInputvalue = $(this).next(':input[name="amount[]"]').val();
            alert(nextInputvalue); // this will give the text box value
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM