Grabbing the page URL and submitting it when form is submitted? - javascript

I am trying to create a form that will appear when a button on an item page is pressed to receive a quote. Since there are many items, I want to know the URL of the page the form was received on and I am not sure how to achieve this.
My question is how do I grab the URL of the page the button was pressed on and then submit it along with the form as a hidden value so that I am aware what item was requested for a quote? Here is my code for the form. The Source URL input is just a placeholder.
<form action="http://et-signup.topright.com/oemSignup/subscribe" name="subscribeForm" method="post" onsubmit="alert('Thank You We Will Contact You Shortly'); return true;">
<input type="hidden" name="urlOfSubscribeThankYouPage" value="http://myurl.com">
<input type="hidden" name="urlOfErrorPage" value="http://myurl.com">
<input type="hidden" name="urlOfUnsubscribeThankYouPage" value="http://myurl.com">
<input type="hidden" name="mid" value="6286675">
<input type="hidden" name="listName" value="Item Page List - 52083">
Email Address: <input type="email" required="required" name="emailAddress"/> <br/>
First Name: <input name="profile.First Name"><br/>
Last Name: <input name="profile.Last Name"><br/>
Company Name: <input name="profile.Company Name"><br/>
Phone: <input name="profile.Phone"><br/>
Estimated Quantity: <input name="profile.Estimated Quantity"><br/>
Comments: <input name="profile.Comments"><br/>
Small: <input name="profile.Small"><br/>
Medium: <input name="profile.Medium"><br/>
Large: <input name="profile.Large"><br/>
Extra Large: <input name="profile.Extra Large"><br/>
2XL: <input name="profile.2XL"><br/>
3XL: <input name="profile.3XL"><br/>
Source URL: <input name="profile.Source URL"><br/>
<input type="hidden" name="SubAction" value="sub_add_update">
<input type="submit" value="Join">
</form>

You must be looking for the referrer variable.
If you are using plain html and javascript you can find it using:
document.referrer;
In PHP:
$_SERVER['HTTP_REFERER']
in ASP.NET:
Request.UrlReferrer
Hope that helps.

What I do for these kind of 'meta' options is make a hidden input field like so.
<input id="pageUrlInput" type="text" name="pageUrl" style="display: none;">
Then populate the value of this on document ready
$(document).function(){
$('#pageUrlInput').val(window.location.href);
});
This will populate the value of the hidden input field with the page url(window.location.href). Then just get the pageUrl param when processing the form on a server

Related

HTML - Two forms with 'enter' keypress to know which form I am entering

I have two forms and I would like to make it easy to type in one form text field and press enter and the page knows what form is being filled out.
Form 1 (example: search):
<form action="" method="post" name="form1">
<input type="text" name="txt1" />
<input type="submit" value="Enter 1" />
</form>
Form 2 (example: login):
<form action="" method="post" name="form2">
<input type="text" name="txt2" />
<input type="submit" value="Enter 2" />
</form>
Both passes through a PHP script to validate and off to its correct site.
Search is added to a page that is included in every page (MVC) header and the login is on its own page but both come together in one page as two forms. When logging in on the login page I enter username and password and press enter but it defaults to the search submit button and would like to know its being entered on the login submit button.
Appreciate your help...
If you give your submit buttons a name, you will be able to detect them in PHP.
<input type="submit" name="submit" value="Enter 2" />
and later
if ($_POST['submit'] == 'Enter 2') // ...
Since you know the field names you're looking for in each form, you can key off of that:
<?php
if (isset($_POST['txt1']) {
// do one thing
} else {
// do the other
}
<form action="" method="post" name="form1">
<input type="hidden" name="form" value="1" />
<input type="text" name="txt1" />
<input type="submit" value="Enter 1" />
</form>
<form action="" method="post" name="form2">
<input type="hidden" name="form" value="2" />
<input type="text" name="txt2" />
<input type="submit" value="Enter 2" />
</form>
The intval() will validate the posted value.
if (intval($_POST['form']) == 1){}
elseif (intval($_POST['form']) == 2){}
Taken from this article: http://www.javascript-coder.com/html-form/html-form-submit.phtml#multiple
Multiple Submit buttons
You can have more than one submit buttons in a form. But, how to identify from the server side which of the buttons was pressed to submit the form?
One way is to have different names for the submit buttons.
<input type="submit" name="Insert" value="Insert">
<input type="submit" name="Update" value="Update">
In the server side script you can do a check like this :
if(!empty($_REQUEST['Update']))
{
//Do update here..
}
else
if(!empty($_REQUEST['Insert']))
{
//Do insert Here
}
The second method is to have different values for submit buttons with the same name.
<input type="submit" name="Operation" value="Insert">
<input type="submit" name="Operation" value="Update">
The server side code goes like this (PHP code):
if($_REQUEST['Operation'] == 'Update')
{
//Do update here..
}
else
if($_REQUEST['Operation'] == "Insert")
{
//Do insert here
}

pass input value to another form on a diff url and run submit wordpress

I have 2 forms, 1st form is to submit an input value to the next page, and on the next page there's the 2nd form which is a search map function.
the 1st form is displayed on the homepage
<form role="search-map" method="" id="search-map" action="/find">
<h4>Where To Buy</h4>
<input type="text" class="form-control" id="addressInput" name="addressInput" placeholder="Your Suburb or Postcode">
<button type="submit" class="btn btn-vc btn-default btn-lg" id="search-address-btn">Find</button>
</form>
the 2nd form is on another page which is /find that has a map search plugin
<form onsubmit="cslmap.searchLocations(); return false;" id="searchForm" action="">
<input type="text" id="addressInput" name="addressInput" placeholder="Your Suburb" size="50" value="where-i-want-value-to-be-pass">
<input type="submit" class="slp_ui_button" value="Find" id="addressSubmit">
</form>
any ideas how can i pass the value from the 1st form "addressInput" to the 2nd form? and the run the search on the 2nd form? here's the 2nd form btw
i tried searching here but what i need seems to be more complex that what i have found
or maybe how can i get the 2nd page to get the value from the url (?addressInput=North+Bega%2C+NSW%2C+2550) into the 2nd form input "addressInput" and run the submit button function when the page loads?
thanks in advance guys
I'm assuming you want to get the value from s to the second form.
replace where-i-want-value-to-be-pass
with <?php echo $_REQUEST['addressInput']; ?>
<form onsubmit="cslmap.searchLocations(); return false;" id="searchForm" action="">
<input type="text" id="addressInput" name="addressInput" placeholder="Your Suburb" size="50" value="<?php echo $_REQUEST['addressInput']; ?>">
<input type="submit" class="slp_ui_button" value="Find" id="addressSubmit">
</form>

Dynamic Hidden Form Field Data Absent From POST

I have a form that, when submitted, does not post the dynamically added hidden field.
html:
<div>
<form action="thankyou.php" onsubmit="return validate()" id="orderform" method="post">
<input type="text" name="name" /><br>
<input type="text" name="email" required /><br>
<input type="text" name="charterco" required /><br>
<input type="text" name="bname" /><br>
<input type="text" name="dtime" required /><br>
<input type="submit" />
</form>
</div>
jquery:
$('#orderform').submit(function(eventObj){
$('<input />').attr('type','hidden')
.attr('id','list')
.attr('name','shopList')
.attr('value',sliststr>)
.appendTo('#orderform');
return true;
});
POST data from Chrome DevTools:
name:b
email:b#b.com
charterco:b
bname:b
dtime:12:00
message:Comment
I can't work out what's gone wrong. My sliststr variable turns up filled and correct in my little debugging test on jsfiddle here. For whatever reason, it isn't POSTing.
EDIT: As #JayBlanchard pointed out below, I am adding to the form after the POST has been written.
Try to append the dynamic element, set the value of it and then submit the form. Otherwise it's submitting the form and then appending the html in callback.
Try the following.
function validate(){
var shoplist = [1,2,3];
$('#orderform').append("<input type='text' name='shop' id='list'>")
$('[name="shop"]').val(shoplist)
$('#orderform').submit(function(eventObj){
return true;
});
}

Walk through a form in javascript and check for blank inputs?

I apologize for asking such a "noob" question but I'm outside my area of expertise here.
I'm using Dojo 1.9 and I need to walk through a submitted form and determine if any of the input fields are blank. The tricky part is that the form is dynamic, it can contain an array of child elements of each array element, with names like itemList[1].myName:
<form id="businessReferences" action="/yabba/dabba/doo" method="post">
<input id="itemList[0].myName" name="itemList[0].myName" type="text" value=""/>
<input id="itemList[0].myAddress" name="itemList[0].myAddress" type="text" value=""/>
<input id="itemList[1].myName" name="itemList[1].myName" type="text" value=""/>
<input id="itemList[1].myAddress" name="itemList[1].myAddress" type="text" value=""/>
<input id="itemList[2].myName" name="itemList[2].myName" type="text" value=""/>
<input id="itemList[2].myAddress" name="itemList[2].myAddress" type="text" value=""/>
</form>
What is the best way to walk through this form and check to see if all the fields for each parent element are empty? For example if all the fields for itemList[2] are empty? Is there a particular method for doing this? Seems like it would be a fairly common problem but I haven't been able to track down an answer.
Just add an submit button in the form
<form id="businessReferences" action="/yabba/dabba/doo" method="post" onsubmit="return validate()">
<input id="itemList[0].myName" name="itemList[0].myName" type="text" value=""/>
<input id="itemList[0].myAddress" name="itemList[0].myAddress" type="text" value=""/>
<input id="itemList[1].myName" name="itemList[1].myName" type="text" value=""/>
<input id="itemList[1].myAddress" name="itemList[1].myAddress" type="text" value=""/>
<input id="itemList[2].myName" name="itemList[2].myName" type="text" value=""/>
<input id="itemList[2].myAddress" name="itemList[2].myAddress" type="text" value=""/>
<input type='submit' value="Submit"/> <!-- to trigger validation on this button click -->
</form>
Javscript for validation , it will account for dynamically added element also
<script type='text/javascript'>
function validate(){
FromRef = document.getElementById('businessReferences');
for(i=0; i<FromRef.elements.length; i++)
{
if((FromRef.elements[i].value).trim() ==""){ // see if the value is blank popup and alert
alert( FromRef.elements[i].name +"is required");
return false; // not to submit form
}
return true;//submit form
}
</script>

How to prevent form element from sending some fields we don't want?

I have a form element that contains about 5 fields which final query is going to create by processing values those fields. So I want to send only final query, not all of those, to the server. How can I exclude those fields from being submitted (using jQuery)?
<form action="abc/def.aspx" method="get">
<input type="text" name="field1" />
<input type="text" name="field2" />
<input type="text" name="field3" />
<input type="text" name="field4" />
<input type="text" name="field5" />
<input type="hidden" name="final" />
<input type="submit" value="Send" />
</form>
Output of form submission looks like below:
abc/def.aspx?field1=val1&field2=val2&field3=val3&field4=val4&field5=val5&final=finalQuery
Remove the name attribute on the fields you do not want submitted to the server.
<form action="abc/def.aspx" method="get">
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="hidden" name="final" />
<input type="submit" value="Send" />
</form>
This is the simplest way to achieve what you want, and it works on all major browsers.
W3 spec talks about only submitting form values when name is present: http://www.w3.org/TR/html401/interact/forms.html#h-17.2
Remove the element on submit.
On the onsubmit handler:
$(formElement).submit(function() {
$(this.field1).remove(); //removing field 1 from query
return true; //send
});
Disabling the form element also stops it from being entered into the query.(tested on Chrome)
$(formElement).submit(function() {
this.field1.disabled = true;
return true; //send
});
I think the best solution is to handle the submit and then send the request yourself:
$(form).submit(function() {
//do everything you need in here to get the final result
var finalResult = alotoflogic();
$.get("abc/def.aspx",final=finalResult, "callbackfunction", "responseType");
return false;
});
that should do exactly what you want.
EDIT: as Alex pointed out this solution wouldnt send you to that page, just get the results if you need to go to the new page you can do:
$(form).submit(function() {
//do everything you need in here to get the final result
var finalResult = alotoflogic();
window.location('abc/def.aspx?final='+finalResult);
return false;
});
This way the browser is redirected to that page and only the final result is send.

Categories