I have a small tell a friend form made up with input fields My Name, My E-mail, Friends Name and Friends E-mail fields and a Send button.
Each of the fields are scripted like follows:
<?php
if (strlen($m_name) <2 )
{
$write .="Please enter your Name<BR />";
$status = "unchecked";
}
?>
It all works until I introduce a Go Back link facility .....
To validate I am seeking to install a GoBack link as follows to enable visitor go back to form and do corrections:
echo '<a href="javascript:history.go(-1)">Go Back to Form</a>';
When using this arrangement to 'Go Back to Form' all the details already filled in by the visitor have been emptied from the fields and they would have to start filling in the form all over again!
Is there a simple workround to this ? What am I missing?
If you want to show the errors on one page and then go to the separate form page, your most likely choice would be to use PHP sessions for the submitted data, and on the form page checking to see if values are populated in the relevant session variables:
Generally, I like to use the technique where the form and the form-processor is the same file, redisplaying the form if there were any validation errors. In that case, the submitted data will be in the $_POST (or $_GET) array, and then you can use the same technique as above with that array instead of $_SESSION if there were any errors.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Does the above get integrated into the following form code at my <label>position?
<?php
// we must never forget to start the session
session_start();
?>
<html><head><title>form.php</title></head><body>
<form method="post" action="processor.php">
<label for="user">My Name:</label><input type="text" name="m_name" value="" /><br />
<input type="submit" name="submitbutton" id="submitbutton" value="Send the Website Link to my Friend" />
</form></body></html>
I am using the following processor:
<?php
session_start();
//Form Processor Starts
$status = "checked";
$write = "";
$m_name = $_POST['m_name'];
//Validate starts here
if (strlen($m_name) <5 )
{
$write .="Please enter your Name<BR />";
$status= "unchecked";
}
if($status=="checked")
{
mail();//mail parameters removed
echo "<font size='3' color=green>Thank You, Your message was sent to your friend";
}else{//error message
echo "<font size='2' color=red>$write";
}
?>
<br /><!--Go Back Link--><a href="javascript:history.go(-1)">Go Back to Form</a>
Currently not getting fields to maintain information as required even though I have tried different things with the above Foo code!
-------------------------------------------------------------
The 2nd part of your response sounds interesting but I would need another day or two to work out.
You may not be able to do it via PHP if you are going to use the JavaScript history method, as that will most likely just reload the page from the browser cache. You would instead need to make it an actual link to the form page URL.
Then the idea would be to save the form data to session data in the form handler, so you could simply do something like:
PHP Code:
<?php
session_start();
$_SESSION['form_data'] = $_POST;
// form handler processing
// ...
?>
<a href="url_to_your_form">Go back to the form</a>
If you want to get fancier, you could add error messages to the $_SESSION array linked to form field names, so that you could highlight the fields with errors. You might want to then clear any such session data after displaying the form. If so, in this example you could just do:
PHP Code:
</form> <!-- end of form HTML -->
<?php
$_SESSION['form_data'] = null;
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
I am not getting the second part of your direction to do with the form. I have put the form up at http://www.shopdemo.webitry.net/
just to make sure we are on the same track. The form will not work there but I have installed a link to the processor at the bottom of the page source.
I am not as yet finding the correct arrangement for the foo code in the form page. Will your patients last to provide another pointer!
Note that "foo" is just a sample form element name. You'll have to replace it with the applicable form element name in each case. Also, if you have a textarea element, you set the value between the tags, not as a "value" attribute within the tag.
Here's an abbreviated example using just one text element:
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks