Solved

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

Posted on 2008-10-09
7
638 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

707 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