Link to home
Start Free TrialLog in
Avatar of andy pollina
andy pollinaFlag for United States of America

asked on

check button value before submit

Hi, my web site has a request information form page which uses formmail.pl. Lately I've been receiving a lot of robot generated emails from the page so I decided to put a option box on the page whereby if the option "Human" is not selected, then no email will be sent through the formmail.pl file.

I've not too experienced with this and I've not worked with JavaScript for a long time, thus I can't get my idea to work.

On the page is a form which I've described as below:
<form name="infoRequest" enctype="application/x-www-form-urlencoded">
<input name="subject" type="hidden" value="Information Request">
<table width="100%" border="0" cellspacing="0" cellpadding="0">

I then put in a select option box for a visitor to select a value from the list:
<tr>
<td height="30" width="33%">what am I:</td>
<td textarea rows="3" cols="30" height="30" width="67%"</TD>
<select name="robotyn" onchange="checkrobot();" id="dropDown1">
        <option value="Robot">I'm a Robot</option>
        <option value="Human">I'm a human</option>
        <option value="other">Other</option>
</select>
</tr>

below that is the submit and reset buttons. My intension is for a script to be run when the submit button is checked.

<tr>
<td colspan="2" width="100%" align="center">
<input TYPE="submit" NAME="Submit" VALUE="Submit" onclick="(function() {checkrobot();return false;})()"><input TYPE="reset" VALUE="Reset">
</td>
</tr>

here is the script:
function checkrobot(){

  a = document.getElementById("dropDown1");        

        if(a.value == "Human")
                window.open('file://"../cgi-bin/formmail.pl"');
            return true;
                  
        else
            window.location.reload();
                  document.getElementById("submit").reset();
                  return false;
        }

</script>

What I'm trying to accomplish in the script is for it to check if the option "Human" was selected, if so - to open the formmail.pl file which will send an email containing the other form elements.
If "Human" is not selected, then to reset the form and start over again.

The script seems to be working if other than the option "Human" is selected but it is not opening the formmail,pl file if "Human" is selected.

I've attached the entire htm file.  Hopefully an expert can help me with solving this issue.
Your assistance is appreciated.
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hmmm. This is proper old-school. I've not seen a formmail.pl script for years! If you're sure you want to carry on using that, then you'll need to edit your code. Basically, you don't open a perl script - you submit your form to it, so you would have the form's action property set to your script's path:

<form action="../some/path/formmail.pl" method="post">

Then you would bind your function to the onsubmit() of the form, so:

<form action="../some/path/formmail.pl" method="post" onsubmit="return checkrobot(e);">

And now your script should return true or false. It's not very robust, but something like this.

function checkrobot(e){
    e.preventDefault();
    a = document.getElementById("dropDown1");        
    if (a.value == "Human")
        return true;
    else
        return false;
}

Open in new window

Avatar of spmt
spmt

Hello

it's better to use google reCaptcha to test if it's a human or a robot it's more secure

https://www.google.com/recaptcha/intro/v3beta.html
+1 for Google reCaptcha - that's generally the way most forms are validated against autobots these days. It's what people are used to and is much harder to by-pass than your current implementation.
Avatar of andy pollina

ASKER

Hi Chris,
Yes, very old school. Thanks for offering your support.
Excuse my ignorance. So are there 2 form lines or is that just 1 line with the :

<form action="../some/path/formmail.pl" method="post" onsubmit="return checkrobot(e);">

I tried both ways and getting the same result - the formmail.pl file executes regardless of what is chosen.
Google Recaptcha is just what i'd use
will give that a look and try now!
Thanks!!
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Chris, it wouldn't work with the method=get changed it to post and Whalla!
Thanks!!