Filling form with referrer cookie JS - javascript

I am not very good with JS coding, what i am trying to do is to set cookie with JS and then retrieve it with code in specific field ID. Problem is I am generating form fields based on user geo-location and using Gravity forms, unfortunately gravity forms wont allow me to set class to input so i have to use two different IDs.
This is my Code:
<script type="text/javascript">
document.getElementById("input_2_7").value = getCookie("tir_referrer");
</script>
<script type="text/javascript">
document.getElementById("input_3_7").value = getCookie("tir_referrer");
</script><code>
Now, when user loads website from certain country, he gets input field "2_7" and when other country is loaded the input is "3_7" where first num is form ID and second is form field ID.
The two JS scripts above put cookie in field 7, this works but the problem is when only one form is on site Chrome console outputs an error:
Uncaught TypeError: Cannot set property 'value' of null
at (index):740
Form filling work regardless and only script that is not needed fails, but i want to improve the code and only use one JS for this for both IDs, while heaving some error handling, hope someone can enlighten me on how to.
Regards, D

Just check if getElementById returned an element reference (if it does not find the element, it will just return null), before you try to access properties such as value:
var input_2_7 = document.getElementById("input_2_7");
if(input_2_7) {
input_2_7.value = getCookie("tir_referrer");
}

Related

Autopopulate form input texboxes with javascript

I have this following problem because I don't have expertise in Javascript
I'm testing a Facebook login at
http://goo.gl/3R3owa
After the user is logged in Facebook an alert windows with the birthday and location comes up. So far so good.
Rather than show that window i will like to autopopulate the day, month and year input boxes in that page.
Is there any way to do that?
Here is my code
http://goo.gl/IFhJdu
Thanks in advance!
Assuming that you have all the data in the response object and element_id# is a valid id of an element present on the HTML page. You can simply use following JS code do set the value of an input field in JS:
document.getElementById('element_id1').value = response.gender;
document.getElementById('element_id2').value = response.birthday;
document.getElementById('element_id3').value = response.location.name;
Similarly, for feeding the data to an HTML element, you can use innerHTML, for example:
document.getElementById('element_id1').innerHTML= response.gender;
(You can use the above code to replace the alert() method in you JS code.)

Jquery validation engine ajax, two fields same function?

The jQuery validation engine plugin has the ability to do ajax validation; which works gret except for one small catch...
It sends off the field ID instead of the field name to be validated.
Why is this an issue?
I have a simple item that to create it only requires one textbox to be filled out; so we have this as a modal on every page for managing said item.
We use the jQuery validation engine plugin to validate that the entered value is unique.
Now this also means that the modal shows up on the edit page. Which obviously has the title in a field as well for you to edit.
And we want this field to be validated as well but because the validation engine sends across the field ID instead of the field name we must give the two fields different ID's
e.g. createtitle and edittitle and then on the backend have
if($fieldId == 'createtitle' || $fieldId == 'edittitle'){$fieldId = $fieldId}
Which really is an ugly approach; is there any way to get it to use the name; or another attribute instead?
Maybe this plugin could help you. It uses class names of your element to validate.

Custom validations troubles

I’m using Sys.Mvc to count errors and I have added my own custom validations by jQuery.
var validationErrors = Sys.Mvc.FormContext.getValidationForForm(this).validate('submit');
var errorsCount = validationErrors.length;
And also I have some fields, which will by hide (by using jQuery .hide();)
Question: How I can remove errors from Sys.Mvc.FormContext if the required element is hidden and add the error if the element is appearing again?
OR
How I can ignore hidden elements validation errors?
Best regards Paul.
Try to initialize the field even if it is hidden (using jQuery), and insert a temporary value. after submitting the form - you can insert the right value into the hidden fields (in the controller).
Something like this:
$("#myField").hide();
$("#myField").val("temporaryValidValue");

HTML textarea value is coming up as 'undefined'

I have HTML textareas that 'sometimes' have 'undefined' in the value property - I can't work out what is causing it.
Background: I have a PHP script that generates lots of textareas on a page - each with a unique ID using a counting system. The textareas belong to different forms that appear on the page. It is like an email inbox with a 'quick reply' form under each email.
Sometimes, when the forms are submitted the corresponding textarea value comes up as 'undefined' instead of the actual value the user has typed into the field. Example code:
//Javascript
function quickReply(id)
{
message = document.getElementById('textarea_' + id).value();
//Send 'message' and other details to PHP for handling...
}
I have also tried calling the value with the jQuery equivalent:
$('textarea_' + id).val();
Most of the time everything works as expected, but sometimes the value comes up as 'undefined'. 'undefined' is then send to my PHP code in Javascript and ends up getting saved in the database as the 'reply' which is how the issue is being discovered.
What causes a textarea to have an 'undefined' value property? Anything I can try to resolve this?
This is prob because your dom hasnt loaded yet is your tag in a document ready
$(document).ready(function() {
$('textarea_' + id).val();
});
To select an item by Id in Jquery, don't forget to add the "#"
$('#textarea_' + id).val();
I was able to resolve this issue by using tags to enclose each form (with each form having a unique ID).
I believe it should be possible to consistently access the .value property of a textarea without it being contained within form tags, however it seems Internet Explorer needs it in form tags 'sometimes'.
Not the best answer - but it did solve the issue.

How can 2 Html forms share 1 hidden field?

I have a page with 2 forms and a hidden field that is outside of both of the forms.
How can the hidden field be submitted with either of the forms?
I thought about doing something like this with jQuery:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
// do something to move or copy the
// hidden field to the submitting form
});
});
</script>
Will this work? Any other ideas?
EDIT
The hidden field stores a serialized object graph that doesn't change. Both server methods require the object. The serialized object string can get pretty hefty, so I don't want this thing sent down from the server 2 times.
You can simply inject a copy of the element into each form right before submission.
This way, you can have the option of having different information for each hidden form field without affecting the other.
Something like this:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
$("#hidden_element").clone().appendTo(this);
});
});
</script>
If you want to use the exact same element for both forms without creating a fresh copy, just don't use clone()
See documentation for clone() and for appendTo()
EDIT:
If you don't want to send the hidden element with every request the form sends. Consider storing it in the database for that user for that time. You can submit its content once, and once only for every page reload, and then just send the database id of the hidden element with each form post.
On page load, something like this:
$.post("page.php", { reallyBigObject : $("#hiddenfield").val() }, function(insertedID){
$("#hiddenfield").val(insertedID);
});
Then, in the server side code:
//insert $_POST["reallyBigObject"] into databse
//get the just inserted id (in php it's done with mysql_insert_id())
//echo, or print the just inserted id, and exit
This way your js gets the callback.
Now, you can submit the form as you would, but this time, you're only sending the id (integer number) to the server.
You can then simply delete the object from your server (run a cron to do it after X amount of time, or send another request to delete it.
Honestly, though, unless you object is HUGE(!!), I think storing it by submitting it only once is a lot more complex to execute than to simply send two requests to the server.
Let me know if you have any other questions...
With HTML5, you can include a "form" attribute with an input element. The value of the form attribute is the id of the form(s) the field belongs to. To include the field in more than one form, include all form ids in a space-delimited list. Unfortunately, the form attribute is not supported in IE at all (as of IE 9). FF and Chrome support start in version 4 and 10 respectively.
Append the field to both forms at page load:
$(function() {
$('#form1, #form2').append($('input[name=fieldName]'));
});
Assuming you are doing a non ajax submit you could just append the field into the form being submitted. However if you need this info in both forms is it not better to store this value server side in a session store. This way any non js clients will also work!
$(function() {
$('form').submit(function() {
$('input.yourhiddenSubmit').appendTo(this);
});
});
The only way to pass the variable to the next form is to have that variable in the data that is passed when the form is submitted (either GET or POST), unless you want to use AJAX. Assuming you want to have the hidden variable outside of the actual form for a "good reason", I would recommend using jQuery to include the hidden input field into the form just before submission, like so:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
$(this).append("<input type='hidden' name='hiddenField' value='"+$("#hiddenField").val()+"' />");
return true;
});
});
</script>
Replace all the instances of "hiddenField" with the ID of your hidden field outside the form. This will create a new input inside of the form just before it is submitted with the value of the hidden field that is elsewhere on the page.
Beyond that, you'd have to be a bit more specific about what your exact problem was (and why the field isn't being included in the form) for me to help.
Note that the above code should work in theory, but I didn't have a chance to actually test it out myself.

Categories