Page 1 of 1

Pass PHP Variable to Javascript Function

Posted: Thu Feb 12, 2015 10:52 am
by GregTexas
I'm new at programming and I can't get a PHP variable into a specific spot in a Javascript function. For the line that has min: myvar I can never get my PHP variable to show up in place of myvar. Is it possible to do this? Below is my last attempt:

Code: Select all

<script type="text/javascript">
                                        $(function ()
                                        
                                        {
                                            myvar =    <?php echo json_encode($high_offer); ?>;
                                            $('<i class="icon-ok"></i><i class="icon-remove"></i>').appendTo($('#form section'));
                                            $("#form").validate(
                                                    {
                                                        // Rules for form validation
                                                        rules:
                                                                {
                                                                    name:
                                                                            {
                                                                                required: true
                                                                            },
                                                                    email:
                                                                            {
                                                                                required: true,
                                                                                email: true
                                                                            },
                                                                    offer:
                                                                            {
                                                                                required: true,
                                                                                digits: true,
                                                                                min: myvar
                                                                            },
                                                                    terms:
                                                                            {
                                                                                required: true
                                                                            },
                                                                },
                                                        // Messages for form validation
                                                        messages:
                                                                {
                                                                    name:
                                                                            {
                                                                                required: 'Please enter your name'
                                                                            },
                                                                    email:
                                                                            {
                                                                                required: 'Please enter your email address',
                                                                                email: 'Please enter a VALID email address'
                                                                            },
                                                                    offer:
                                                                            {
                                                                                required: 'Please enter your offer',
                                                                                min: 'Our client has already turned down an offer greater than your offer',
                                                                                digits: 'Please enter only numbers'
                                                                            },
                                                                    terms:
                                                                            {
                                                                                required: 'Please agree to our terms before submitting your offer'
                                                                            },
                                                                },
                                                        // Do not change code below
                                                        errorPlacement: function (error, element)
                                                        {
                                                            error.appendTo(element.parent());
                                                        }
                                                    });
                                        });
                                    </script>

Re: Pass PHP Variable to Javascript Function

Posted: Thu Feb 12, 2015 11:04 am
by Celauran
Why not just pass it in as an argument?

Re: Pass PHP Variable to Javascript Function

Posted: Thu Feb 12, 2015 11:12 am
by GregTexas
Celauran wrote:Why not just pass it in as an argument?
How do I do that?

Re: Pass PHP Variable to Javascript Function

Posted: Thu Feb 12, 2015 2:13 pm
by Christopher
Two things:

1. Is the PHP variable $high_offer set? What does echo json_encode($high_offer); display? I have found you often need echo (isset($high_offer) ? json_encode($high_offer) : '{}');

2. It should be:

Code: Select all

                                            var myvar =    <?php echo json_encode($high_offer); ?>;

Re: Pass PHP Variable to Javascript Function

Posted: Thu Feb 12, 2015 9:06 pm
by GregTexas
Christopher wrote:Two things:

1. Is the PHP variable $high_offer set? What does echo json_encode($high_offer); display? I have found you often need echo (isset($high_offer) ? json_encode($high_offer) : '{}');

2. It should be:

Code: Select all

                                            var myvar =    <?php echo json_encode($high_offer); ?>;
It is set and that was one of the variations I tried (var myvar). Do I need to put something around myvar after min: ?

This is what view source looks like:

Code: Select all

 <script type="text/javascript">
                                        $(function () 
                                        { var myvar =    "100";
                                            $('<i class="icon-ok"></i><i class="icon-remove"></i>').appendTo($('#form section'));
                                            $("#form").validate(
                                                    {
                                                        // Rules for form validation
                                                        rules:
                                                                {
                                                                    name:
                                                                            {
                                                                                required: true
                                                                            },
                                                                    email:
                                                                            {
                                                                                required: true,
                                                                                email: true
                                                                            },
                                                                    offer:
                                                                            {
                                                                                required: true,
                                                                                digits: true,
                                                                                min: myvar
                                                                                
                                                                                
                                                                            },