Solved

how do I keep form field populated after using the back button?

Posted on 2008-10-09
7
630 Views
Last Modified: 2012-06-21
I'm building a form in a custom Joomla component. It collects payment details and has a captcha image.
When the form is submitted and the captcha string is incorrect a message is displayed..."incorrect code please try again" and there's a button that takes the user back to re-enter the code.

<form>
 <input type=button value="Go back to payment form" onClick="history.go(-1)">
</form>

when you click this the form appears a gain but all the fields are empty.

How can I make it so that the field retain the information previously entered.

thanks
0
Comment
Question by:xoundboy
  • 5
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
lightit earned 500 total points
ID: 22680207

Maybe use sessions?

in your page with the al the fields, put this on top
<?php
session_start();
?>

in every value from a field you put this code:
 <?php echo $_SESSION["fieldname"];?>

In the page where you go to after the form is submitted you first do this, you register every session and give it a value

<?php
session_start;
@ session_register("fieldname1"); // The @-sign removes the error if this session is already registered
@ session_register("fieldname2");

$_SESSION("fieldname1") = $_POST["fieldname1"];
$_SESSION("fieldname2") = $_POST["fieldname2"];

// PUT HERE THE REST OF THE CODE
?>

You will see, after you returned to your previous page the fields will be filled with the value,
it is best that you destroy the sessions after the form has a succesfull submit, because otherwhise if the people come back at this page the fields will be still filled in, do that like this
<?php
session_start();
session_destroy();
?>

Sorry for bad english

Greetz
0
 
LVL 2

Author Comment

by:xoundboy
ID: 22682415
thanks

I tried that but I'm getting this error which I don't understand:

Fatal error: Can't use function return value in write context in /vservers/mydomain/htdocs/test/components/com_payment/form_action.php on line
0
 
LVL 2

Author Comment

by:xoundboy
ID: 22685066
let me clarify that last comment.

this line:

$_SESSION("students_name") = $_POST["students_name"];

 in the page that the form submits to is generating this error:

Fatal error: Can't use function return value in write context in /vservers/mydomain/htdocs/test/components/com_payment/form_action.php on line 19

 what is wrong with setting a session variable like this?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Assisted Solution

by:lightit
lightit earned 500 total points
ID: 22685433
Sorry my bad!

$_SESSION["students_name"] = $_POST["students_name"];

That is the right way! It was already very late yesterday
0
 
LVL 2

Author Comment

by:xoundboy
ID: 22685760

I followed your instructions very carefully. It still doesn't work though. It could be something to do with the fact that the form is being included into a joomla index.php page, its URL being:

www.mydomain.com/index.php?option=com_payment

I tried dumping the conents of the session variable just before the form and it appears to be empty - hence the fields not being repopulated. The strange thing is though that if I run the same session dump script afterwards from a non index.php  location then the session is still alive and the values are still held there. This would imply that joomla's index controller is hiding the session variables from the form but without actually destroying them. It's very frustrating.

Anyone have any ideas? Anyone know the Joomla framework? This is the 1st time I'm developing a component for Joomla so it's all a bit strange.

HELP!
0
 
LVL 2

Author Comment

by:xoundboy
ID: 22693103
I've had another go at it and realised that I forgot to add one field to the list of session_register('fieldname"); 's. Now it works :)

0
 
LVL 2

Author Closing Comment

by:xoundboy
ID: 31504690
thanks very much - great answer
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question