Solved

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

Posted on 2008-10-09
7
609 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now