We help IT Professionals succeed at work.

Insert form action on key press

CAE5942
CAE5942 asked
on
475 Views
Last Modified: 2014-11-10
Hi everyone,

I have a form on a web page and I've removed the action part of the form element so that it looks like this:

<form id="catwebformform65782" name="catwebformform65782" method="post" onsubmit="return checkWholeForm65782(this)" enctype="multipart/form-data">

I then use javascript to detect a key press so that the action part is inserted as follows:

$( "#catwebformform65782" ).on( "keypress", function( event ) {
            var action = $("#catwebformform65782").attr("action");
            if ( typeof(action) === "undefined" || action === null || action === "" ) {
                var url = "/FormProcessv2.aspx"
                var params = "?WebFormID=10090&amp;OID={module_oid}&amp;OTYPE={module_otype}&amp;EID={module_eid}&amp;CID={module_cid}";
                $( "#catwebformform65782" ).attr("action",url + params);
            }
});

Open in new window


I test it in the browser by inspecting the form, inserting some text into the first input field, but the action url is not being inserted. I wondered if anyone can help me pinpoint the problem. The full form code is below.

Really appreciate any help.

Thanks in advance.


<form id="catwebformform65782" name="catwebformform65782" method="post" onsubmit="return checkWholeForm65782(this)" enctype="multipart/form-data">
    <div class="item item-third">
    <input type="text" name="FirstName" id="FirstName" class="cat_textbox" maxlength="255" placeholder="First Name" />
    <input type="text" name="LastName" id="LastName" class="cat_textbox" maxlength="255" placeholder="Last Name" />
    <input type="email" name="EmailAddress" id="EmailAddress" class="cat_textbox" maxlength="255" placeholder="Email Address" />
    </div>
    <div class="item item-third">
    <input type="text" name="HomePhone" id="HomePhone" class="cat_textbox" maxlength="255" placeholder="Home Phone" />
    <input type="text" name="WorkPhone" id="WorkPhone" class="cat_textbox" maxlength="255" placeholder="Work Phone" />
    <input type="text" name="CellPhone" id="CellPhone" class="cat_textbox" maxlength="255" placeholder="Mobile Phone" />
    </div>
    <div class="item item-third">
    <input type="text" maxlength="4000" name="CAT_Custom_19935221_134827" id="CAT_Custom_19935221_134827" class="cat_textbox" placeholder="How did you hear about us?" />
    <select name="CAT_Custom_19935223" id="CAT_Custom_19935223" class="cat_dropdown">
    <option value=" ">-- Preferred Contact Method --</option>
    <option value="Phone">Phone</option>
    <option value="Email">Email</option>
    </select>
    </div>
    <div class="item">
    <textarea name="CAT_Custom_869" id="CAT_Custom_869" cols="10" rows="4" placeholder="Comments/Enquiries" class="cat_listbox" onkeydown="if(this.value.length&gt;=4000)this.value=this.value.substring(0,3999);"></textarea>
    </div>
    <div class="item">
    <label>Enter Word Verification in box below <small>(required)</small></label>
    {module_captchav2}
    </div>
    <div class="item">
    <input class="button radius" type="submit" value="Submit" id="catwebformbutton" />
    </div>
    <script type="text/javascript" src="/CatalystScripts/ValidationFunctions.js"></script>
    <script type="text/javascript">
    //<![CDATA[

        $( "#catwebformform65782" ).on( "keypress", function( event ) {
            var action = $("#catwebformform65782").attr("action");
            if ( typeof(action) === "undefined" || action === null || action === "" ) {
                var url = "/FormProcessv2.aspx"
                var params = "?WebFormID=10090&amp;OID={module_oid}&amp;OTYPE={module_otype}&amp;EID={module_eid}&amp;CID={module_cid}";
                $( "#catwebformform65782" ).attr("action",url + params);
            }
});

    var submitcount65782 = 0;

    function checkWholeForm65782(theForm) {
            var why = "";
            if (theForm.FirstName) why += isEmpty(theForm.FirstName.value, "First Name");
            if (theForm.LastName) why += isEmpty(theForm.LastName.value, "Last Name");
            if (theForm.EmailAddress) why += checkEmail(theForm.EmailAddress.value);
            if (theForm.HomePhone) why += isEmpty(theForm.HomePhone.value, "Home Phone Number");
            if (theForm.CaptchaV2) why += captchaIsInvalid(theForm, "Enter Word Verification in box below", "Please enter the correct Word Verification as seen in the image");
            if (theForm.CAT_Custom_19935223) why += checkDropdown(theForm.CAT_Custom_19935223.value, "Please select your preferred contact method");
            if (theForm.CAT_Custom_19935221_134827) why += isEmpty(theForm.CAT_Custom_19935221_134827.value, "Please tell us how you heard about us?");
            if (why != "") {
                alert(why);
                return false;
            }
            if (submitcount65782 == 0) {
                submitcount65782++;
                theForm.submit();
                return false;
            } else {
                alert("Form submission is in progress.");
                return false;
            }
        }
        //]]>
</script>
</form>

Open in new window

Comment
Watch Question

Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
I'm not sure why you would do that.  ??  The action page is clearly displayed in your javascript so you are not hiding anything.  All I have to do is "View Source" and I have it.

Author

Commented:
The purpose of it is to deter spam bots. Do you know why it's not working?
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
I suspect it is because it is too late at the point you are trying to set it.  Try setting the 'action' attribute on page load and see if it will work.

Author

Commented:
Sorry my javascript is not that good. I'm trying to re-use the code that I was given on a current site that I'm building and it doesn't seem to want to function there. To set the action attribute on page load, would I change this:

        $( "#catwebformform65782" ).on( "keypress", function( event ) {

to this:

        $( "#catwebformform65782" ).on( "pageload", function( event ) {
Fixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
You just pinpointed the probably. I didn't realise it was jQuery so I moved the code after the reference to the jQuery and it now works.

Thanks for your help - much appreciated.
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
You're welcome, glad to help.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.