Solved

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

Posted on 2008-10-09
7
601 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
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.
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 …

747 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

12 Experts available now in Live!

Get 1:1 Help Now