Pass PHP Variable to Javascript Function

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
GregTexas
Forum Newbie
Posts: 6
Joined: Mon Jan 26, 2015 2:41 pm

Pass PHP Variable to Javascript Function

Post 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>
Last edited by Celauran on Thu Feb 12, 2015 11:03 am, edited 1 time in total.
Reason: Please wrap your code in syntax tags
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Pass PHP Variable to Javascript Function

Post by Celauran »

Why not just pass it in as an argument?
GregTexas
Forum Newbie
Posts: 6
Joined: Mon Jan 26, 2015 2:41 pm

Re: Pass PHP Variable to Javascript Function

Post by GregTexas »

Celauran wrote:Why not just pass it in as an argument?
How do I do that?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Pass PHP Variable to Javascript Function

Post 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); ?>;
(#10850)
GregTexas
Forum Newbie
Posts: 6
Joined: Mon Jan 26, 2015 2:41 pm

Re: Pass PHP Variable to Javascript Function

Post 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
                                                                                
                                                                                
                                                                            },
Post Reply