Solved

Insert form action on key press

Posted on 2014-11-09
7
142 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

0
Comment
Question by:gwh2
  • 4
  • 3
7 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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.
0
 
LVL 1

Author Comment

by:gwh2
Comment Utility
The purpose of it is to deter spam bots. Do you know why it's not working?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:gwh2
Comment Utility
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 ) {
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
I don't know why yours doesn't work but I found a page which is probably from another question that works like you want.  It's plain javascript and not jquery.  It's got some extra stuff that you can ignore.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script type="text/javascript">
	function verify() {
		var srrmsg = "";
		
		if (document.reg_form.Full_Name.value=="") {
			srrmsg = srrmsg + " - Full Name:\n";
		}
//Adding another IF statement throws error in FireFox
			if (document.reg_form.email.value=="") {
			srrmsg = srrmsg + " - email:\n";
		}

		//alert if fields are empty and cancel form submit
		if (srrmsg != "") {
			alert("Please Check the Following Fields: \n"+srrmsg);
			return false;
		}
		return true;
	}
</script>

<script type="text/javascript">
<!--
	function frmSubmit() {
		document.reg_form.target = "_blank";
		//document.reg_form.action = "FormMail_Contact.pl"
		document.reg_form.action = "#"
		document.reg_form.submit();
		setTimeout('document.reg_form.target = "_self";window.location = ""',0);
	}
	
// -->
</script>

</head>
<body>

<form name="reg_form" id="reg_form" method="get" >
Full Name: <input type="text" name="Full_Name" id="frmname" class="frmfld"  value=""  /><br />

Email : <input type="text" name="email" id="frmemail" class="frmfld"  value=""  /><br />

<a href="#" onClick="if(verify()) frmSubmit(); else return false;"><img src="btn_contact.jpg" width="117" height="26" border="0"></a>
</form>

</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:gwh2
Comment Utility
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.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're welcome, glad to help.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now