Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Javascript onChange Not Responding in Smarty Template

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
ShamusMay
Smarty Rookie


Joined: 28 Apr 2015
Posts: 18

PostPosted: Wed Oct 19, 2016 3:57 pm    Post subject: Javascript onChange Not Responding in Smarty Template Reply with quote

I have a simple sort dropdown form with a javascript onChange="this.form.submit()" value that is not working right within the Smarty template.

I have tested the form outside the Smarty environment and it works, so I must be overlooking something to make this work inside the Smarty template file.

Anyone see anything I'm doing wrong?

Code:
<form name="sort" action="" method="post">
            <select class="form-control" name="order" onChange="this.form.submit()">
            <option>Default Sort</option>
            {php} $selected_sort=$_POST["order"];{/php}
            <option {php}
                         if ($selected_sort == 'value1')
                         echo 'selected=true'; }
                        {/php}
                        value="value1">Sort by popularity
            </option>
             <option {php}
                         if ($selected_sort == 'value2)
                         echo 'selected=true'; }
                        {/php}
                        value="value2">Sort by Rating
            </option>
            </select>
</form>
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Oct 19, 2016 4:04 pm    Post subject: Reply with quote

1. You're using PHP code in Smarty templates.
2. You're confusing server scripting language with client scripting events.
Back to top
View user's profile Send private message
ShamusMay
Smarty Rookie


Joined: 28 Apr 2015
Posts: 18

PostPosted: Wed Oct 19, 2016 4:14 pm    Post subject: Reply with quote

Ignore the PHP part... I am just testing with that to pass data.

I'm more focused on the javascript and why it wont work on the client side.

Shouldn't the onChange="this.form.submit()" still work?
Back to top
View user's profile Send private message
ShamusMay
Smarty Rookie


Joined: 28 Apr 2015
Posts: 18

PostPosted: Wed Oct 19, 2016 4:16 pm    Post subject: Reply with quote

So this example with no PHP is not working... I just can;t get it to successfully submit. If I add a submit button it works, but I'm trying to make it work when the user picks a value from the dropdown.

Code:
<form name="sort" action="" method="post">
            <select class="form-control" name="order" onChange="this.form.submit()">
            <option>Default Sort</option>
            <option value="value1">Sort by popularity</option>
            <option value="value2">Sort by Rating</option>
            </select>
</form>
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Oct 19, 2016 4:23 pm    Post subject: Reply with quote

Again: test it WITHOUT PHP.
Make a page in your favorite HTML editor and test your scripts there.
PHP has nothing to do with client scripting.
Results of PHP(Smarty) script's execution is a bunch of bytes sent over the wire. What this bunch of bytes does in your browser, it doesn't care - it already dead by the time your browser render the page and start running events.
Back to top
View user's profile Send private message
ShamusMay
Smarty Rookie


Joined: 28 Apr 2015
Posts: 18

PostPosted: Wed Oct 19, 2016 4:33 pm    Post subject: Reply with quote

Sorry to confuse you with the PHP tags... Let's move on from that with this code that has NO php tags...

It works fine in my HTML editor (outside of the Smarty template). The Javascript onChange successfully submits the form when I chose a new value.

It will not work inside the Smarty template. Will Javascript onChange not work inside a Smarty template. What am I missing here?

Code:
<form name="sort" action="" method="post">
            <select class="form-control" name="order" onChange="this.form.submit()">
            <option>Default Sort</option>
            <option value="value1">Sort by popularity</option>
            <option value="value2">Sort by Rating</option>
            </select>
</form>
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Oct 19, 2016 6:08 pm    Post subject: Reply with quote

You didn't confuse me with PHP tags. That was just a generic reference to "what you're doing wrong".
In regard to your JS question: you're doing it wrong by asking it on PHP forum. Even if we know the answer (in fact, I have a good guess, since "this.form" i not a valid construct), it'd be an offtopic here.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP