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

Check if radio button is Checked

 
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
dextranovich
Smarty n00b


Joined: 19 May 2018
Posts: 3

PostPosted: Sat May 19, 2018 11:25 pm    Post subject: Check if radio button is Checked Reply with quote

Hello everyone,

I have a radio button in the template like this:
<input type="radio" name="acceptTerms" id="accept_terms" value="Yes"/>

So, I have to check if the radio button is selected, to assign variable result with yes.

{assign var="result" value="Yes"} and if the radio button is not checked, I have to assign variable with No.

Any help please how to implement the solution in smarty template (.tpl) ?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun May 20, 2018 12:49 am    Post subject: Reply with quote

You're making very little sense.
You can't know in a template, what would happen long after the Smarty is dead and gone.
Back to top
View user's profile Send private message
dextranovich
Smarty n00b


Joined: 19 May 2018
Posts: 3

PostPosted: Sun May 20, 2018 6:56 am    Post subject: Reply with quote

So, it's not possible?
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Mon May 21, 2018 4:09 am    Post subject: Reply with quote

I would say it is possible, but very convoluted, and incredibly inappropriate.

Once Smarty has rendered the template and has submitted the final content to stdout or to a variable, your PHP code that has accepted Smarty's output can scan for id="accept_terms". Now knowing where in the output this HTML is located, your PHP can scan for the opening angle and closing angle of the input tag.

Once having found the entire input tag in Smarty's output, you can scan to see if the tag has checked="checked" as an attribute of the tag. (This will have been programmed by earlier PHP code to assign this phrase to a Smarty variable which got rendered.)

You will then interject some more stuff into the output content, such as {assign var="result" value="Yes"}.

With this modified content of Smarty's output, having inserted more Smarty code to process, you can then feed this string content back through Smarty's fetch/display function to get new output.

There is a huge problem with this: any HTML from the original template that had braces escaped will no longer have escaped braces. Thus, you have a guaranteed crash.

Please know that Smarty runs under PHP. Once Smarty has rendered the template and delivered it to the output stream, Smarty is done.

It is better to examine Smarty's output, look for something in particular (as described above), but have your PHP code change other parts of the output - not Smarty.

You say you have to check if a radio button is checked. The only way it will display in a browser as checked is if that input tag already has a checked="checked" attribute, or the user viewing the web page actually checks the radio button.

Your practical solution is to have a bit of javascript at the browser test for the initial checked state of "accept_terms", and also apply a change() event viewer on the input tag.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon May 21, 2018 3:25 pm    Post subject: Reply with quote

How does that help to know, if a user changed options on HTML page he's seeing?
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