0

I'm facing a problem that with my Laravel project in JavaScript part.

My issue is that I have a form and when taking the variable outside the function it becomes empty ("") and works only inside the function

<form method="POST" action="#">
  @csrf

  <label for="package">إختر الباقة:</label>
  <div class="card-deck mb-4 text-center">
    <div class="card shadow-sm">
    <div class="card-header bg-dark-1">

                            <h6 class="my-0 text-white">اشتراك شهري</h6>

                        </div>
                        <div class="card-body">

                            <h1 class="card-title">$20</h1>

                            <div class="form-check">

                                <input type="radio" onclick="setPrice()" class="form-check-input" id="package"
                                    name="package" class="radios" value="20" />
                                <label class="form-check-label" for="package">إختر هذه الباقة</label>

                            </div>
                        </div>
                    </div>

                    <div class="card shadow-sm">
                        <div class="card-header bg-dark-1">
                            <h6 class="my-0 text-white">نصف سنوي</h6>
                        </div>
                        <div class="card-body">
                            <h1 class="card-title">$50</h1>
                            <div class="form-check">
                                <input type="radio" class="form-check-input" onclick="setPrice()" id="package"
                                    name="package" value="50" class="radios" />
                                <label class="form-check-label" for="package">إختر هذه الباقة</label>
                            </div>
                        </div>
                    </div>

                    <div class="card shadow-sm">
                        <div class="card-header bg-dark-1">
                            <h6 class="my-0 text-white">اشتراك سنوي</h6>
                        </div>
                        <div class="card-body">
                            <h1 class="card-title">$100</h1>
                            <div class="form-check">
                                <input type="radio" class="form-check-input" onclick="setPrice()" id="package"
                                    name="package" value="100" class="radios" />
                                <label class="form-check-label" for="package">إختر هذه الباقة</label>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <input type="text" name="name" onclick="setName()" class="form-control" id="name" required
                        data-error="الرجاء ادخال الاسم الكامل" placeholder="الاسم الكامل" />
                    @if ($errors->has('name'))
                        <span class="help-block">
                            <strong>{{ $errors->first('name') }}</strong>
                        </span>
                    @endif
                </div>
                <div class="form-group">
                    <input type="text" name="phone" onchange="setPhone()" class="form-control" id="phone" required
                        data-error="الرجاء ادخال رقم الهاتف" placeholder="رقم الهاتف" />
                    @if ($errors->has('phone'))
                        <span class="help-block">
                            <strong>{{ $errors->first('phone') }}</strong>
                        </span>
                    @endif
                </div>
                <div class="form-group">
                    <input type="email" name="email" onchange="setEmail()" class="form-control" id="email" required
                        data-error="الرجاء ادخال البريد الالكتروني" placeholder="البريد الالكتروني" />
                    @if ($errors->has('email'))
                        <span class="help-block">
                            <strong>{{ $errors->first('email') }}</strong>
                        </span>
                    @endif
                </div>
                <div class="form-group">
                    <input type="password" onchange="setPassword()" name="password" class="form-control" id="password"
                        required data-error="الرجاء ادخال كلمة السر" placeholder="كلمة السر" />
                    @if ($errors->has('password'))
                        <span class="help-block">
                            <strong>{{ $errors->first('password') }}</strong>
                        </span>
                    @endif
                </div>
                <div class="form-group">
                    <input type="password" name="password_confirmation" class="form-control" id="password-confirm"
                        required data-error="الرجاء ادخال تأكيد كلمة السر" placeholder="تأكيد كلمة السر" />
                    @if ($errors->has('password_confirmation'))
                        <span class="help-block">
                            <strong>{{ $errors->first('password_confirmation') }}</strong>
                        </span>
                    @endif
                </div>
                <button class="default-btn confirm_payment" name="submit" onclick="Checkout.showPaymentPage();"
                    id="form-submit">تسجيل</button>

SHOW

and when taking a variable only works inside the function and outside always empty here's my script part:

<script type="text/javascript">
    var package1 = $('input[name="package"]:checked').val();
    var name1 = $('input[name="name"]').val(); //name
    var phone1 = $('input[name="phone"]').val(); //phone
    var email1 = $('input[name="email"]').val(); //email
    var password1 = $('input[name="password"]').val(); //password
    $("#show").click(function() {
        console.warn(name1);//result ""


        // if i write var package1 = $('input[name="package"]:checked').val(); and others inside the function here it works. 
     });
 </script>
1
  • This seems like the very basics of scoping - the variables outside are page-load time set, while inside the click handler it's click-time Commented Feb 9, 2022 at 4:46

1 Answer 1

0

Try in this way

$(document).ready(function(){
// Get value on button click and show alert
$("#myBtn").click(function(){
    var str = $("#myInput").val();
    alert(str);
});

});

Sign up to request clarification or add additional context in comments.

2 Comments

I tried it and still giving empty value , any additional solution please
$("#myBtn").click(function(e){ e.preventDefault(); var str = $("#myInput").val(); alert(str); }); please try this one

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.