moazamin6's avatar

send checkbox value to controller using laravel blade and javascript

hello developers i am new on laravel and I am stuck in a problem since 3 days. Following is my code.

<?php
$check = 'not defined';
?>
<script>
    function checkFunction() {
        var checkBox = document.getElementById("chk").checked;

        if (checkBox) {
            document.getElementById("lbl").innerText='true';
            <?php
            $check = 'true';
            ?>
        } else {
            document.getElementById("lbl").innerText='false';
            <?php
            $check = 'false';
            ?>
        }
    }
</script>
<link href="{!! asset('public/css/app.css') !!}" media="all" rel="stylesheet" type="text/css"/>
<p id="lbl">true/false</p>
{!! Form::checkbox('isInvoiceHeader', 1, true, ['onClick' => 'checkFunction()','id'=>'chk']) !!}
<span><strong>Include Header</strong></span><br><br>
<a href="{{ url('showheader/'.$check) }}" class="btn btn-primary">
    click here to go forward
</a>
public function index($isCheck)
    {
        echo $isCheck;
        return view('showheader');
    }
}

$check give always else section value weather checkbox is checked or unchecked but when I include true and false in paragraph then it give good output.... I think it is magic

please please please help me

0 likes
17 replies
Snapey's avatar

Its just like a load of random code thrown on the page !

Sorry, cannot understand what you are trying to do and how all this fits together

moazamin6's avatar

My question is very simple that how to pass checkbox value from one blade template to another blade template with controller or without controller

moazamin6's avatar

actually I am working on an open source project in this project I want to add a new functionality so there is the following code this achor tag is outside the form

<a href="{{ url('incomes/invoices/' . $invoice->id . '/print') }}" target="_blank"
                       class="btn btn-default">
                        <i class="fa fa-print"></i>  {{ trans('general.print') }}
                    </a>

and this is route

Route::get('income/invoices/{invoice}/print', 'Incomes\Invoices@printInvoice');

and controller code

public function printInvoice(Invoice $invoice)
    {
        //dd($invoice);

        $invoice = $this->prepareInvoice($invoice);

        $logo = $this->getLogo();
        //I want to get that checkbox value in this method
        return view($invoice->template_path, compact('invoice', 'logo'));
    }
topvillas's avatar

You're not making any sense. You started talking about checkboxes and now you're telling us something about anchor tags.

If you want to send a checkbox value then you'll have to use a form, a submit button and a post route.

Oh, and please stop assuming that everyone's here itching to answer your question within the hour.

Snapey's avatar

What I think you are talking about.

When the user clicks the href, you want to have the current state of the checkbox also passed to the controller.

For this you will need to pass as a query string parameter.

Your print URL is currently like /incomes/invoices/100/print

To pass additional information, you will need a URL like /incomes/invoices/100/print?header=true or you could create an additional route like /incomes/invoices/100/printnoheader

When the checkbox is checked or unchecked in the client you need javascript to vary the url of the anchor.

Snapey's avatar

(or alternatively, forget the checkbox and show two buttons or anchors)

moazamin6's avatar

Thanks to all I have already post my code of javascript .... this code not working properly. I don't know why...... I can send the parameter to controller ......... but javascript code is not giving me the correct output..... my code is following

<?php
$check = 'not defined';
?>
<script>
    function checkFunction() {
        var checkBox = document.getElementById("chk").checked;

        if (checkBox) {
            document.getElementById("lbl").innerText='true';
            <?php
            $check = 'true';
            
            ?>
        } else {
            document.getElementById("lbl").innerText='false';
            <?php
            $check = 'false';
            // this variable is always give me false 
            ?>
        }
    }
</script>
<link href="{!! asset('public/css/app.css') !!}" media="all" rel="stylesheet" type="text/css"/>
<p id="lbl">true/false</p>
{!! Form::checkbox('isInvoiceHeader', 1, true, ['onClick' => 'checkFunction()','id'=>'chk']) !!}
<span><strong>Include Header</strong></span><br><br>
<a href="{{ url('showheader/'.$check) }}" class="btn btn-primary">
    click here to go forward
</a>

in my logic I am setting a php variable in javascript code.. set to true in if section when checkbox is checked and false in else section when checkbox is unchecked...but always it execute the else section ..... this is the problem please read my code carefully and please solve my problem because my all work is stuck due to this bloody problem what is the problem with my code I don't know...

Snapey's avatar

You cannot set php variable inside javascript. Thats just dumb.

1 like
moazamin6's avatar

so how to solve this

this is your statement

When the checkbox is checked or unchecked in the client you need javascript to vary the url of the anchor.

so how to vary the anchor url using javascript

Snapey's avatar

If your javascript skills are not there, just use two links or buttons.

moazamin6's avatar

OMG I solve this by using your suggestions

javascript code

<script>
    function checkFunction() {

        
        // Get the checkbox
        var checkBox = document.getElementById("chk").checked;


        // If the checkbox is checked, display the output text
        if (checkBox) {
            document.getElementById("link").href = '{{url('showheader/'.'true')}}';
        } else {
            document.getElementById("link").href = '{{url('showheader/'.'false')}}';
        }

    }

</script>

html code

{!! Form::checkbox('isInvoiceHeader', 1, true, ['onClick' => 'checkFunction()','id'=>'chk']) !!}

<a href="{{url('showheader/'.'true')}}" id="link" class="btn btn-primary">
    click here to go forward
</a>
1 like
jlrdw's avatar

i am new on laravel and I am stuck in a problem since 3 days

Laravel has nothing to do with a checkbox, it is an html thing, works the same in php, asp.net, and java, coldfusion, classic asp, etc.

<script>

$(function () {
    function makeAJAXRequest (text) {
        $.ajax({
            url: '<?php echo DIR . "owner/getowner"; ?>',
            type: 'GET',
            dataType: 'json',
            data: { id: text },
            success: processSuccess
        });
    }

    function processSuccess (data) {
        $.each(data, function (key, value) {
            var id = '#' + key,
                elem = window.opener.$(id);
            if (key === 'ocheck') {
                elem.attr('checked', value === '1');
            }
            else {
                elem.val(value);
            }
        });

        self.close();
    }

    $("#myTable td:nth-child(1)").click(function (event) {
        event.preventDefault();
        var $td = $(this).closest('tr').children('td');
        var currentCellText = $td.eq(0).text();
        makeAJAXRequest(currentCellText);
    });
});



</script>

In an if statement to determine checked status of the checkbox

Another ex:

if (mystr[4] == '1')
                    {
                        // checkbox is checked
                        window.opener.$('#ocheck').attr('checked', true);
                    }
                    else
                    {
                        // checkbox is not checked
                        window.opener.$('#ocheck').attr('checked', false);
                    }

Many examples of this on the web.

And if having trouble getting a checkbox state in controller:

$adopted = (isset($_POST['adopted']) == '1' ? '1' : '0');

of course convert this part

(isset($_POST['adopted'])

to laravel request as desired. Remember if a checkbox isn't checked in a form, then nothing is passed during the POST but if checked, then a 1 or `true' is passed.

rawilk's avatar

You are lacking knowledge of some basic programming concepts. You really should learn the basics of PHP and JavaScript first.

1 like
moazamin6's avatar

ok then please guide me to learn the basic concept of php and javascript

Please or to participate in this conversation.