LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   PHP doesn't "remember" the user input. What to do? (http://www.linuxquestions.org/questions/programming-9/php-doesnt-remember-the-user-input-what-to-do-88722/)

linuxfond 09-03-2003 02:54 AM

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?

nephilim 09-03-2003 03:05 AM

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.

linuxfond 09-03-2003 01:47 PM

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. :confused:

nephilim 09-03-2003 02:23 PM

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.

linuxfond 09-03-2003 05:02 PM

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.

nephilim 09-04-2003 12:57 AM

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.

linuxfond 09-04-2003 03:07 AM

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.

nephilim 09-04-2003 03:39 AM

Try this:

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

That should work.

linuxfond 09-04-2003 06:30 AM

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.

linuxfond 09-04-2003 06:38 AM

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!!

nephilim 09-04-2003 07:11 AM

You're welcome. Enjoy your new knowledge ;)


All times are GMT -5. The time now is 12:44 PM.