What is a way to return the form values back with a page?
Posted: Fri Feb 03, 2012 9:53 pm
I have a comments form to which I have added reCAPTCHA. No problem with the reCAPTCHA, really very simple to implement.
What is causing me a bit of problem is figuring out how to return the form to the user, with the fields set to the data they entered, with the error message when they enter the reCAPTCHA incorrectly.
The file which contains the form and reCAPTCHA is named submitcomments.php The form action is action="commentprocessing.php"
That filecontains PHP which checks the reCAPTCHA entry and, if it is correct, saves the form data in a .txt file with a time-stamp as part of the file name to make it unique. Then the PHP function ends and the user sees the commentprocessing.php page which thanks them for their comments.
What's the best way to send back the original page, with the form fields set to the values entered by the user, if they enter an incorrect reCAPTCHA, or the data fail one of my other tests?
I've thought of a few ways to do it but they all seem to be over elaborate - in my 37+ years working with computers, I've learned when something just doesn't "look" right that I should take a break and reconsider the entire situation.
So, what is the current wisdom as regards this sort of thing?
I can handle JavaScript if a particular method requires it - in fact, I spent a bit of time last night playing with some code to set fields - problem is how to get it into the correct file to send back to the user.
BTW - I will add the necessary code to sanitize the input before I return it to the user. Hey, I've heard that the Internet can be a dark dangerous place
Also, these are not "comments" as you might think of them - they are not going to be posted on any page as "user comments." The "comments" will, hopefully, be information germane to the web site. New information and leads to new information.
The site involved is a bit unusual,, I'll give you the URL if you promise not to laugh (too much) at the ridiculous format I came up with - I was just so close to the tree (the data) that all I could see was the bark - "Forest? What Forest? I don't see one."
I'm working on a much better, cleaner format, with pages that ease you into all of the data, for example the data about the many companies, without dumping you into an enormous vat of facts and expecting you to not drown and to actually come away with information you can use.
http://www.jonpsalmonds.net (.com I just bought that name also)
Here's the PHP code I played with to generate JavaScript to set field values via the getElementByid function and a getElementsByClassName function I came across. The idea was to generate some JavaScript to set the forms fields - the problem I've not solved, or at least not to my liking, is getting the code into the file with the form.
The array $formFields is defined thusly:
The output looks like this:
Bob
What is causing me a bit of problem is figuring out how to return the form to the user, with the fields set to the data they entered, with the error message when they enter the reCAPTCHA incorrectly.
The file which contains the form and reCAPTCHA is named submitcomments.php The form action is action="commentprocessing.php"
That filecontains PHP which checks the reCAPTCHA entry and, if it is correct, saves the form data in a .txt file with a time-stamp as part of the file name to make it unique. Then the PHP function ends and the user sees the commentprocessing.php page which thanks them for their comments.
What's the best way to send back the original page, with the form fields set to the values entered by the user, if they enter an incorrect reCAPTCHA, or the data fail one of my other tests?
I've thought of a few ways to do it but they all seem to be over elaborate - in my 37+ years working with computers, I've learned when something just doesn't "look" right that I should take a break and reconsider the entire situation.
So, what is the current wisdom as regards this sort of thing?
I can handle JavaScript if a particular method requires it - in fact, I spent a bit of time last night playing with some code to set fields - problem is how to get it into the correct file to send back to the user.
BTW - I will add the necessary code to sanitize the input before I return it to the user. Hey, I've heard that the Internet can be a dark dangerous place
Also, these are not "comments" as you might think of them - they are not going to be posted on any page as "user comments." The "comments" will, hopefully, be information germane to the web site. New information and leads to new information.
The site involved is a bit unusual,, I'll give you the URL if you promise not to laugh (too much) at the ridiculous format I came up with - I was just so close to the tree (the data) that all I could see was the bark - "Forest? What Forest? I don't see one."
I'm working on a much better, cleaner format, with pages that ease you into all of the data, for example the data about the many companies, without dumping you into an enormous vat of facts and expecting you to not drown and to actually come away with information you can use.
http://www.jonpsalmonds.net (.com I just bought that name also)
Here's the PHP code I played with to generate JavaScript to set field values via the getElementByid function and a getElementsByClassName function I came across. The idea was to generate some JavaScript to set the forms fields - the problem I've not solved, or at least not to my liking, is getting the code into the file with the form.
Code: Select all
function setCommentFormValues() {
$lfCR = chr(13). chr(10); // create a variable containing the carriage return and line feed characters for use in other statements.
$formFields = get_formFieldsArray();
$ffCount = count($formFields);
$javaScriptCode = $lfCR . '<script type="text/javascript">' . $lfCR;
$javaScriptCode .= "var radioBoxes;" . $lfCR;
$javaScriptCode .= "var foundRadioButton = false;" . $lfCR;
print_r($_POST);
for ($i = 0; $i <= $ffCount - 1; $i++) {
$type = substr($formFields[$i], 0, 2);
$name = substr($formFields[$i], 2);
switch ($type) {
case "TX";
$javaScriptCode .= "document.getElementById('". $name . "').value='" . $_POST[$name] . "';" . $lfCR;
break;
case "RB";
$javaScriptCode .= "radioButtons = document.getElementsByClassName('" . $name . "');" . $lfCR;
$javaScriptCode .= "for (i = 0; i <= radioButtons.length; i = i + 1) {" . $lfCR;
$javaScriptCode .= " if (radioButtons[i].value ='" . $_POST[$name] . "') {" . $lfCR;
$javaScriptCode .= " radioButtons[i].checked = 'checked';" . $lfCR;
$javaScriptCode .= " foundRadioButton = true;" . $lfCR;
$javaScriptCode .= " break;" . $lfCR;
$javaScriptCode .= " };" . $lfCR;
$javaScriptCode .= "};" . $lfCR;
$javaScriptCode .= "if (foundRadioButton == false){" . $lfCR;
$javaScriptCode .= " alert('RadioButton with name=" . $name . " with value=\'" . $_POST[$name] . "\' Not found!');" . $lfCR;
$javaScriptCode .= "};" . $lfCR;
break;
default:
echo $name . " value is unrecognizable, value='" . $type . "'";
break;
};
};
$javaScriptCode .= "</script>" . $lfCR;
return $javaScriptCode;
};Code: Select all
$formFields = array("TXname", "TXemail", "TXcity", "TXstate", "TXcountry", "TXwebsite", "RBhowFound", "TXwhoReferred", "TXotherFindText", "TXcomments", "RBuseful");Code: Select all
<script type="text/javascript">
var radioBoxes;
var foundRadioButton = false;
document.getElementById('name').value='wqqqqqqqqqqqqqqqqqqqq';
document.getElementById('email').value='';
document.getElementById('city').value='';
document.getElementById('state').value='';
document.getElementById('country').value='';
document.getElementById('website').value='';
radioButtons = document.getElementsByClassName('howFound');
for (i = 0; i <= radioButtons.length; i = i + 1) {
if (radioButtons[i].value ='notSpecified') {
radioButtons[i].checked = 'checked';
foundRadioButton = true;
break;
};
};
if (foundRadioButton == false){
alert('RadioButton with name=howFound with value=\'notSpecified\' Not found!');
};
document.getElementById('whoReferred').value='';
document.getElementById('otherFindText').value='';
document.getElementById('comments').value='waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaw';
radioButtons = document.getElementsByClassName('useful');
for (i = 0; i <= radioButtons.length; i = i + 1) {
if (radioButtons[i].value ='notSpecified') {
radioButtons[i].checked = 'checked';
foundRadioButton = true;
break;
};
};
if (foundRadioButton == false){
alert('RadioButton with name=useful with value=\'notSpecified\' Not found!');
};
</script>