Displaying textfield information with ajax() function in different pages

31 views
Skip to first unread message

mostwanted

unread,
Dec 29, 2018, 8:11:13 AM12/29/18
to web2py-users
I have a textfield that takes a user's name but i dont want to save the user's name in a database table i just wanna be able to display it in all the pages as long as the user is going through the web pages they should see their name with the welcome message but what i have achieved so far is to only display it in one page, the first page where they entered their name, the other pages dont display anything, here is my code:

CONTROLLER
def echo():
   
return 'Welcome, ', request.vars.name

VIEW 1
<div id="target" class="target" style="float: right; color: #1f8f7b; font-weight: bold;">

</div>
<div id="emc-score"></div>
<div id="greetings" class="greetings">
    Hello, welcome to
<b>T.K's</b> computurised driving theory testing system, please enter your name in the <code>textfield</code> below and click begin to start, GOODLUCK<br />
   
<form id="enterName">
   
<input type="text" name="name" placeholder="Enter your name" /> <a href="#" id="greet" onclick="ajax('{{=URL('default', 'echo')}}', ['name'], 'target')" ><img src="{{=URL('static/images', 'start.jpg')}}" width="60px"/></a>
   
</form>
</div>

How do I get view 2 & the rest to also display the user's name after collecting it the first time when they get started?

VIEW 2
<div id="target" class="target" style="float: right; color: #1f8f7b; font-weight: bold;">

</div>
<div id="emc-score"></div>
<div class="wrap">
 
<div class="row">
 
<section data-quiz-item>
      QUESTION 1
   
<div class="question">What Influences the degree of centrifugal force on bends?</div>
   
<div class="choices" data-choices='["The radius of the bend", "Speed","The height of your vehicle"]'>
   
</div>
 
</section>

Mostwanted


Val K

unread,
Dec 29, 2018, 9:27:01 AM12/29/18
to web2py-users
There is the 'session' that is storage for common needs like this, so you can session.user_name = request.vars.name in the first controller and then use it in other to retrieve user_name

mostwanted

unread,
Dec 29, 2018, 11:43:30 AM12/29/18
to web2py-users
I have changed my controller abit according to your advice

CODE:
def echo():
    session
.name=request.vars.name
   
return 'Welcome, ', session.name

& in view 2 and other views i simply add {{=session.name}} and i get what I want, it works & i hope the semantics of how i used session are ok

VIEW 2
<h3>LESSON 1 QUIZ 2</h3><span><a href="javascript: history.back()">BACK</a> | <a href="javascript:history.go(0);">TEST AGAIN</a></span> | <a href="lesson1quiz3.html">NEXT QUIZ</a>
</header>
<div id="target" class="target" style="float: right; color: #1f8f7b; font-weight: bold;">

Welcome, {{=session.name}}
</div>


Thanks Val K
Reply all
Reply to author
Forward
0 new messages