Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cis4life's avatar

How can I pass a PHP variable to an select onChange event

I have a blade page where I sent to it (via controller) a 'Class' object (its literally a class for a school application). I have a select where onChange I need it to check it registration exisit already...

<div class="form-group">
                        {{Form::label('forRegMember', "Select The Member That This Registration Is For")}} 
                        {{Form::select('forRegMember', $memberList, null, ['class'=>'form-control', 'placeholder' => 'Select Member...', 'onchange' => 'checkRegistrationExist(this.value, ' . $class->SFLClassID . ')' ])}}
                    </div>

The page Renders, but the checkRegistrationExist function doesn't fire and I get this error in the console...

Uncaught SyntaxError: Invalid or unexpected token

Whats the best way to handle this in Laravel.

Again, forgive the newbish nature of this question. I'm literally just getting started with Laravel. (Love it though thus far)

0 likes
6 replies
Cronix's avatar

Does the html look correct for the select when viewing the rendered page's source? You should post the checkRegistrationExist() function too.

cis4life's avatar

Yeah, The select renders correctly and all just doesn't fire the function...

function checkRegistrationExist(memberID, classID){
    $.ajax({
            url:'/api/checkIfRegistrionExisit/member/'+memberID+'/classid/'+classID,
            type: 'GET',
            data: {},
            success:function(msg){
                if(msg.length > 0){
                    $('#forRegMember').val('');
                    alert("This Member Is Already Registered For This Class.  Please Select Another Member Of Your Group You Wish To Register.");
                }
                else
                {
                    alert('Not Registered...');
                }
            },
            dataType: "json"
        });
}
Cronix's avatar

I really dislike that form library and don't use it. Since you're using jquery already, why not give the select element an id and then just use jquery?

$(function() {
    $('#select-id').on('change', function(e) {
        let memberId = this.value;
        console.log(memberId);
        // other stuff...

        // your ajax call...
    });
});
Cronix's avatar
Cronix
Best Answer
Level 67

Try changing the select to this:

{{Form::select('forRegMember', $memberList, null, [
    'class'=>'form-control',
    'placeholder' => 'Select Member...',
    'id' => 'reg-member-select',
    'data-classid' => $class->SFLClassID
]) }}

and this js

$(function() {
    $('#reg-member-select').on('change', function(e) {
        let memberID = this.value;
        let classID = $(this).data('classid');

        $.ajax({
            url:'/api/checkIfRegistrionExisit/member/'+memberID+'/classid/'+classID,
            type: 'GET',
            dataType: "json",
            success:function(msg){
                if(msg.length > 0){
                    $('#forRegMember').val('');
                    alert("This Member Is Already Registered For This Class.  Please Select Another Member Of Your Group You Wish To Register.");
                }
                else
                {
                    alert('Not Registered...');
                }
            }  
        });
    });
});
cis4life's avatar

You put me on the right track. Thanks a million as always...

1 like

Please or to participate in this conversation.