View previous topic :: View next topic |
Author |
Message |
ShamusMay Smarty Rookie
Joined: 28 Apr 2015 Posts: 18
|
Posted: Wed Oct 19, 2016 3:57 pm Post subject: Javascript onChange Not Responding in Smarty Template |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Oct 19, 2016 4:04 pm Post subject: |
|
|
1. You're using PHP code in Smarty templates.
2. You're confusing server scripting language with client scripting events. |
|
Back to top |
|
ShamusMay Smarty Rookie
Joined: 28 Apr 2015 Posts: 18
|
Posted: Wed Oct 19, 2016 4:14 pm Post subject: |
|
|
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 |
|
ShamusMay Smarty Rookie
Joined: 28 Apr 2015 Posts: 18
|
Posted: Wed Oct 19, 2016 4:16 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Oct 19, 2016 4:23 pm Post subject: |
|
|
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 |
|
ShamusMay Smarty Rookie
Joined: 28 Apr 2015 Posts: 18
|
Posted: Wed Oct 19, 2016 4:33 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Oct 19, 2016 6:08 pm Post subject: |
|
|
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 |
|
|