Search code examples
radio-buttonweb2py

only first word from radio button selection is being returned


To get the selected option from a radio button form, I'm looking at request.vars to get the value. Following is the controller code:

def get_results():

test = request.vars['QuestionOne']

for topic in session.selectedtopics:
    test = test + request.vars[topic]

return locals()

And now the code for the view:

{{extend 'layout.html'}}

<P>Please select the options that most closely approximates the actual scenario.
<br>
<form action="{{=URL('get_results')}}" method="post">
    {{nameTopic =""}}
    {{session.selectedtopics = list()}}
    {{for topic in topics:}}
        {{if nameTopic <> topic.topic.replace(" ", "_"):}}
            <br><h2>{{=topic.topic}}</h2>
        {{nameTopic = topic.topic.replace(" ", "_")}}
        {{session.selectedtopics.append(nameTopic)}}
        {{pass}}
        <p><input type="radio" name={{=nameTopic}} value={{=topic.param}}>{{=topic.param}}</p>
    {{pass}}
    <br>
    <input type="submit">
</form>

Here is the problem: I don't know the reason, but it is getting only the first word of the selected option in the radio form. For example, the option selected is "It is normal", but the var is returning only "It". Any idea why it is happening?

Thank in advance.


Solution

  • You need to quote the HTML attribute values:

    <input type="radio" name="{{=nameTopic}}" value="{{=topic.param}}">
    

    Without the quotes, you'll end up with final HTML like:

    <input type="radio" name=some_topic value=It is normal>
    

    The browser will interpret the value to be "It", with "is" and "normal" being invalid HTML attributes.