What is a way to return the form values back with a page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BobN
Forum Newbie
Posts: 8
Joined: Fri Feb 03, 2012 1:03 am

What is a way to return the form values back with a page?

Post by BobN »

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 :wink:

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;
};
The array $formFields is defined thusly:

Code: Select all

$formFields = array("TXname", "TXemail", "TXcity", "TXstate", "TXcountry", "TXwebsite", "RBhowFound", "TXwhoReferred", "TXotherFindText", "TXcomments", "RBuseful");
The output looks like this:

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>
Bob
Last edited by BobN on Sun Feb 05, 2012 8:26 pm, edited 3 times in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: What is a way to return the form values back with a page

Post by Celauran »

Process the form on the same page. If there are errors detected, you can just echo them out. Certainly easier than using session variables + redirects.
BobN
Forum Newbie
Posts: 8
Joined: Fri Feb 03, 2012 1:03 am

Re: What is a way to return the form values back with a page

Post by BobN »

I was thinking of using only one page, I'll give that a try.

I did some reading last night about reCAPTCHA issues and have decided to remove it from the form. I intend to deploy 3, perhaps more, spam killing ideas I've come across.

I tried to update my original post here last night and I never got it done - I entered the wrong CAPTCHA about 7 or 8 times and then the software crashed with a MYSQL error.

When I tried to post a message about the crash, I once again had to reenter the CAPTCHA several times and then the software crashed again.

I don't have time to waste with such problems - particularly trying to report possible software problems and having problems doing even that - and I just gave up and moved on to work I needed to get down.

I'm going to dump reCAPTCHA because they are just getting too hard and I don't want to subject my visitors to that hassle. I'm going to use, as I said above, some other methods, some I've read about and some I've come up with myself. I'll just have to wait and see how effective they are.

As I think I mentioned in the original post, this form is not for comments which will be posted on the web site. It is strictly for the use of people who have information germane to the web site or leads to information.

The form contents will never be posted on the site as is. If any of it is posted it will be because it is something germane to the site. Even then, I'll edit and reformat as needed. I'll never post the raw contents of a form to a page on the site.

Even if I do implement a user comments page, or a message board, I'll check every posting before it is put online. I have not expectations that the volume of posts would ever come even close to being more than I could manually approve or toss.

I've read that some spammer bots will look to see if their material is being posted on the web site and will eventually skip the site if the material does not show up.

Has anyone heard that before?

Thanks for the reply. I'll rework the page so that it performs all four functions - initial input of data, data validation, display of "invalid... try again" messages, and a "thank you for the information" when the validation passes.

I'd prefer to keep those a bit separated but I've not come up with a way yet.

Bob
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: What is a way to return the form values back with a page

Post by Celauran »

BobN wrote:I'll rework the page so that it performs all four functions - initial input of data, data validation, display of "invalid... try again" messages, and a "thank you for the information" when the validation passes.

I'd prefer to keep those a bit separated but I've not come up with a way yet.
If you really want to keep validation separate, you could always have the validation script write the $_POST data to a session variable on error, then redirect back to the page containing the form. Alternately, you could submit the form asynchronously and have a pass/fail message returned.
BobN
Forum Newbie
Posts: 8
Joined: Fri Feb 03, 2012 1:03 am

Re: What is a way to return the form values back with a page

Post by BobN »

Celauran,

I just spent a few minutes trying something and I think I may have found a fairly simple way to do what I want with two separate files.

I try try my idea out, I put the following just before the main fuction exited.
ob_clean();
include("submitcomments.php");
echo setCommentFormValues();
The result is that the output buffer is flushed, getting rid of the first part of the commentprocessing.php file which is the action= for the form which submits the data.

The include, above, includes a file which ends with </form> - there is no </body> or </html>

The result is the orginal submit page with the fields filled in by the JavaScript that the funcition setCommentFormsValues() generates, like this:

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>
The creation of the JavaScript code is driven by the array $formFields. It is a simple indexes arrray (that is the "keys" are integers starting with 0 by default). Each element provides the name of a form field and the field type. I put both pieces of info together in one element. For example, if the field is named "firstName" and it is a text field, I code TXfirstName for that field when creating the formFields array.

Once you make that array, you can use the code that generates the JavaScript to set it and, you can use another function I wrote to check that all of your fields are present. I've found that some spam bots don't send all the fields they find on a form.

Okay - it does what I want it to in both FF and IE but I'm a tad concerned it may not work in other browsers or even other releases of FF and IE because ---

The file submitcomments.php has an end body and end html tag at the end and the JavaScript is insert after them. Like this:

Code: Select all

</body></html>

<script type="text/javascript">
var radioBoxes;
var foundRadioButton = false;
document.getElementById('name').value='kkkkk'
document.getElementById('email').value='kkkkk'
document.getElementById('city').value='kkkkk'
document.getElementById('state').value='kkkkk'
document.getElementById('country').value='kkkkk'
document.getElementById('website').value='kkkkk'
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='kkkkk'
document.getElementById('otherFindText').value='kkkkk'
document.getElementById('comments').value='kkkkk'
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>
</body></html>
The JavaScript is being run even though it comes after </body></html>

Very strange.

If this work with all browsers, it does exactly what I want to do when there is a validation error - return an error message and set the fields to the user's input.

To set the error message, I'll create collapsed div and insert a statement in the generated code to put the message into the div and make it visible.

So, any thoughts on why IE an FF (I don't have any other browsers on this system to try) process a script that comes after </body></html>?

Sort of makes you wonder why we both to include them - I tried a page without them and FF displayed it properly.

New question - I'll makle a separete post asking this question but, what the heck, I'll throw it in here:

If I "include" a file, any PHP includes inside the included file are "executed."

If I read the included file into a string (using file_get_contents) and echo the string, the includes in the file are not "executed."

Any thoughts on why and how I might be able to cause them to be "executed" in this situation?

Bob
Post Reply