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.
andy pollinait mgrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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

0
spmtCommented:
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
0
Chris StanyonWebDevCommented:
+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.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

andy pollinait mgrAuthor Commented:
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.
0
CEHJCommented:
Google Recaptcha is just what i'd use
0
andy pollinait mgrAuthor Commented:
will give that a look and try now!
Thanks!!
0
Chris StanyonWebDevCommented:
Hey Andy,

No - there's just one form tag.

Sorry - I made a typo in my code. I've been using jQuery for too long, so the code I posted won't work as it is. This is the sort of thing you need:

<form action="../some/path/formmail.pl" method="get" onsubmit="checkrobot(event)">
    <select name="robotyn" id="dropDown1">
        <option value="Robot">I'm a Robot</option>
        <option value="Human">I'm a human</option>
        <option value="other">Other</option>
    </select>
    <input type="submit" name="submit" value="Submit">
</form>

<script>
function checkrobot(e) {
    if (document.getElementById("dropDown1").value != "Human")
        e.preventDefault();
}
</script>

Open in new window

Change the form method to post if that's what your perl script needs
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
andy pollinait mgrAuthor Commented:
Chris, it wouldn't work with the method=get changed it to post and Whalla!
Thanks!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.