Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-03-2003, 02:54 AM
|
#1
|
|
Member
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475
Rep:
|
PHP doesn't "remember" the user input. What to do?
Hi,
I have a template driven PHP script that makes certain calculations from the values entered by user.
The problem is that it doesn't remember what values have been entered, including the choice of language. Here is the code:
PHP Code:
<?php
if($lang == "en")
include "auth/lang/lang_en.php";
elseif($lang == "it")
include "auth/lang/lang_it.php";
elseif($lang == "es")
include "auth/lang/lang_es.php";
elseif($lang == "ja")
include "auth/lang/lang_ja.php";
?><?echo $Your_language;?>
And finally a bit of HTML for a drop down:
<select cols="5" name="str">
<option value="12.1" selected>12.1
<option value="12.2">12.3
<option value="12.3">12.4
</select>
Once the page is refreshed, and user is about to perform a new calculation, he/she has to specify the language and the value in drop down once again.
I like the script to remember which value was selected from the drop down, and which language was once included by the user. How do you make it?
|
|
|
|
09-03-2003, 03:05 AM
|
#2
|
|
Member
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248
Rep:
|
You could put the select box in a form with action PHP_SELF, like this:
Code:
<FORM action=" <? echo $PHP_SELF ?>">
When you submit the form, the value of the select box will be in the $str variable.
|
|
|
|
09-03-2003, 01:47 PM
|
#3
|
|
Member
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475
Original Poster
Rep:
|
Hi, thanks. It's a nice tip. I did not know it, so I have learnt something anyway. However the problem is still the same. The user has to retype the $str and choose the language. 
|
|
|
|
09-03-2003, 02:23 PM
|
#4
|
|
Member
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248
Rep:
|
Hmmm... Why is it that the user has to choose the language again?
When you submit the form, you can check the $str variable to select the appropriate option of the select box.
example:
<select cols="5" name="str">
<option value="12.1"
<? if($str= "12.1" echo " selected" ?>
>12.1
<option value="12.2"
<? if($str= "12.1" echo " selected" ?>
>12.2
<option value="12.3"
<? if($str= "12.1" echo " selected" ?>
>12.3
</select>
Hope this helps you out. If not (or if I totally misunderstood), post the complete code and we'll look further into this.
|
|
|
|
09-03-2003, 05:02 PM
|
#5
|
|
Member
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475
Original Poster
Rep:
|
You got it right, and what you proposed hits very near. Great!
Language select is in a similar bit of code a bit closer to the top of the page.
I haven't posted it here. If I solve that bit of code just below, all the rest will be solved, I hope.
Here is the adapted code:
PHP Code:
<select cols="5" name="stringlength">
<option value="12.6" <?php if($stringlength="12.6") echo "selected";?>>12.6
<option value="12.7" <?php if($stringlength="12.7") echo "selected";?>>12.7
<option value="12.8" <?php if($stringlength="12.8") echo "selected";?>>12.8
</select>
Now the script, of course, selects the last value "12.8" because it is the last "selected" in the row. If I select "12.6", after the form is executed, 12.8 is shown in the drop down. It is very intriguing now, because I feel a solution isn't far.
|
|
|
|
09-04-2003, 12:57 AM
|
#6
|
|
Member
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248
Rep:
|
I think I see your problem. Change the if($stringlength="...") to if($stringlength=="...").
Right now, you're assigning the test value to your test variable so that will always return true.
|
|
|
|
09-04-2003, 03:07 AM
|
#7
|
|
Member
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475
Original Poster
Rep:
|
Nephilim! You got it absolutely right, and the problem is solved! Actually I tried to do what you proposed. It did not work because apparently I typed in something wrong without seeing it. Thanks a lot!
Just one more question, if I may. How do you make the script "remember" the user input, if no value is hardcoded? Example:
<input type="text" name="stringlength" size="4" maxlength="5" value="">
I'd be glad to do it without registering a session if that's possible (I am not good at sessions yet).
Once the value entered by the user it is displayed in the form results below.
I'd like the script to pick that value up, and redisplay in the form.
Last edited by linuxfond; 09-04-2003 at 03:34 AM.
|
|
|
|
09-04-2003, 03:39 AM
|
#8
|
|
Member
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248
Rep:
|
Try this:
<input type="text" name="stringlength" size="4" maxlength="5" value="<? echo $stringlength; ?>">
That should work.
|
|
|
|
09-04-2003, 06:30 AM
|
#9
|
|
Member
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475
Original Poster
Rep:
|
You see, I tried it and that doesn't work. The script doesn't reintroduce the $stringlenght into the form, because the $stringlenght is displayed in a HTML output just below the <form>.
<? echo $stringlength; ?> is correct.
I'll try to make it clearer. User-entered value is displayed in a bit of HTML just below the </form>. This value is not redisplayed in the line above, that is in the <form>.
If it was possible to change the order of lines in the code, placing HTML output before the <form>, it would work, but that's not the case.
So, <input name="stringlength" value"<? echo $stringlength; ?>">
goes before HTML output which looks like this:
<p><? print $lang_subtitle $stringlength; ?> <br>
Once the form is executed, the script can not fetch the $stringlength from HTML, and redisplay it in the form above. I am sure it is possible, but I don't know how. I tried to do it with session, but it's a complicated matter. After all, you taught me how to redisplay the choosen values in a simple manner without sessions. Thanks for all.
|
|
|
|
09-04-2003, 06:38 AM
|
#10
|
|
Member
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475
Original Poster
Rep:
|
Yeah! It works. Apparently I typed it in with some mistakes. It is still hard to close all " and > etc and without these PHP doesn't really work ;-)
Many thanks for your help!!
|
|
|
|
09-04-2003, 07:11 AM
|
#11
|
|
Member
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248
Rep:
|
You're welcome. Enjoy your new knowledge 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:46 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|