-2

How to get value from checkbox or radio button immediately after clicking on it and write it in textfield without using reset or submit button?

<input type="checkbox" name="age" value="21-29">21-29 <input type="text" name="yourAge" value="">
  • 4
    You'll want a javascript event to do that, not php – Darren H Sep 13 '15 at 07:20
  • 2
    possible duplicate of [Pass value from html form to php without submitting the form](http://stackoverflow.com/questions/6065101/pass-value-from-html-form-to-php-without-submitting-the-form) – JJJ Sep 13 '15 at 07:22

3 Answers3

3

You can do like this with jQuery click function More Detail Here

<input type="checkbox" name="age" value="21-29" id="age">21-29
<input type="text" name="yourAge" value="" id="yourAge">

JQuery

$("#age").click(function () {
    $("#yourAge").val($("#age").val());

});

Fiddle

Shehary
  • 9,926
  • 10
  • 42
  • 71
1

@Shehary is on point but there is always room for more.

JS

<script>       
 var changeInput = function  (val){
            var input = document.getElementById("age");
            input.value = val;
        }
</script>

HTML

    <input type="checkbox" name="age" value="21-29" onclick='changeInput(this.value);' >21-29
    <input type="text" name="yourAge" value="" id="age">

Pen

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • Thanks, but you are using JS. I need the code only in PHP. My teacher told me to do it just with PHP. – StykPohlavsson Sep 13 '15 at 12:09
  • @StykPohlavsson Err, no. you'll need to use either JS/jQuery or an AJAX call to POST request back to the PHP on the server. It can't be done in PHP only since PHP is a server-side scripting language. – DirtyBit Sep 13 '15 at 12:15
  • 1
    @StykPohlavsson either your teacher needs to go back to school to learn the difference between server-side and client-side language or you miss understood what your teacher told you. `get value from checkbox or radio button immediately after clicking on it` there is no possible way without jQuery, Ajax or java-script because then you were asking `without using reset or submit button` – Shehary Sep 13 '15 at 13:33
0

I know this is old, and my code may not be great, but I like to use this.

<?php
    $get= "?checked";
    $checked= "";
    if(isset($_GET['checked'])){
        $get= "";
        $checked= "checked";
    } 
?>
<a href="waiting_in_db.php<?=$get?>">
    <input type="checkbox" <?=$checked?> >
</a>

But I prefer using CSS and links without the checkbox.