LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 09-03-2003, 02:54 AM   #1
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Rep: Reputation: 30
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?
 
Old 09-03-2003, 03:05 AM   #2
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
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.
 
Old 09-03-2003, 01:47 PM   #3
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Original Poster
Rep: Reputation: 30
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.
 
Old 09-03-2003, 02:23 PM   #4
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
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.
 
Old 09-03-2003, 05:02 PM   #5
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Original Poster
Rep: Reputation: 30
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.
 
Old 09-04-2003, 12:57 AM   #6
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
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.
 
Old 09-04-2003, 03:07 AM   #7
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Original Poster
Rep: Reputation: 30
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.
 
Old 09-04-2003, 03:39 AM   #8
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
Try this:

<input type="text" name="stringlength" size="4" maxlength="5" value="<? echo $stringlength; ?>">

That should work.
 
Old 09-04-2003, 06:30 AM   #9
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Original Poster
Rep: Reputation: 30
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.
 
Old 09-04-2003, 06:38 AM   #10
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Original Poster
Rep: Reputation: 30
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!!
 
Old 09-04-2003, 07:11 AM   #11
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
You're welcome. Enjoy your new knowledge
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
chrooted user: "write" and "talk" don't work. ldp Linux - Software 2 04-12-2005 02:05 AM
User "list" running process "python" TroelsSmit Linux - Newbie 2 02-22-2005 04:55 AM
"User" & "System" CPU load difference JJX Linux - General 3 06-06-2004 01:42 AM
Shorten a Qmail user name to "user" instead of "user@domain.com" anorman Linux - Software 0 12-12-2003 08:29 AM
<input type="button" disabled="true" > does not work in ns4.7 or 4.9 cybercop12us Programming 2 11-29-2002 08:31 AM


All times are GMT -5. The time now is 11:16 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration