insert javascript value into form field - javascript

I have a javascript snippet:
<script language="JavaScript" type="text/javascript" src="/partner/display.php?token=id"></script>
It's from the affiliate program we use, iDevaffiliate.
When included in the html of a page, it reads a value (the affiliate id) to the page, so the visitor can see it.
I need to get this same value inserted into to a hidden form field such as:
<input type="hidden" name="affid" id="affid"" value="value goes here">
This will allow us to submit the affiliate id in the form as a hidden value.
Any suggestion on how to include the value outputed by the javascript snippet as a value into the hidden form field?
The script itself doesn't work as a value in the form, atleast when I tried it in different ways. Thanks for any help.
Here is example page with js in the page, and a sample form. The value -100 should be created and visible when viewing the page (from the js): http://www.lifeleap.org/100-1-3-19.html
After Help and More Research, This Is What I Have So Far:
Script prints value here: <div id="affjs"><script language="JavaScript" type="text/javascript" src="/partner/display.php?token=id"></script></div>
<script type="text/javascript">
$('#testform').submit(function () {
var outputValue=document.getElementById("affjs").innerHTML;
document.getElementById("aff").value=outputValue;
});
</script>
<form method="post" action="/scripttest3.php" id="testform">
<input type="hidden" name="aff" id="aff" value="">
<button type="submit" value="Submit">Submit</button>
</form>
It's not finished, but I think I'm on the right track. Maybe onsubmit script needs to be included inside form? Thanks for any suggestions.

You will need to find the value in the DOM and then store in your hidden input field.
var outputValue=document.getElementById("id-of-html-tag-where-output-value-is").innerHTML;
document.getElementById("id-of-hidden-input-field").value=outputValue;
You should do this on form submit.

Inside the Javascript you can use like this
document.getElementById("affid").value ='value goes here' ;

Related

Is there a way to retrieve hidden input values on another page?

I am trying to take the inputs the customer selects, and add them to hidden fields, to then be able to display them on the cart page.
<form method="post" action="https://omgneonsigns.com/cart/?add-to-cart=5825" name="contentForm" runat="server">
Here is the code for my form, and of course I have my fields setup this way:
<input type="hidden" id="hiddenText" name="hiddenText" value="">
So, what is the next step? I am able to alert the values back to me, so I know they are being held/stored... but I'm not sure how to take them with me to the next page and alert them there?
Firstly, you need to select the hidden input. JavaScript way:
<input type="hidden" id="hiddenText" name="hiddenText" value="" onchange="myFunction(this.value)">
<script>
function myFunction(val) {
let hidden_val = val
// now store it using `session`
}
</script>
Basically you can do it in many ways:
Use JavaScript session. link: https://www.w3schools.com/jsref/prop_win_sessionstorage.asp
Use php session. link:
https://www.w3schools.com/php/php_sessions.asp
Then get the value in the next page from session.

Getting "Invalid URL Exception" on submit due to multiple form tags in Lotus Domino

I'm getting the error, "Invalid URL Exception" on submit of a button. I am assuming that this issue is coming from three form tags I have in the web page.
In the body, the first form tag is for "post" of the fields and redirection. The second one, which is inside of the first form tag, is for the search bar as part of company header.
The third one is a form class for the input fields, which is also inside the first form tag.
I tried placing an end form tag in the first line of my code, it closes the form tag which now looks like this:
<form method = "post" action="/sample/sampleweb.nsf/myForm?OpenForm&Seq=1" enctype="multipart/form-data" name=_myForm">
<input type="hidden" name="__Click" value="0">
</form>
Then the search form tag comes in after as also part of body. Upon clicking the submit button, I'm getting such error. When I tried searching by clicking the search, search functionality working as expected.
When I tried removing the form tag and end form tag for the search, and I click on Submit, it's working as expected.
I notice that domino is still creating end form tag at the last part of the page, right before the end of body tag. Could it be because of this?
To give you a better idea, here is the structure of it:
<html>
<body>
<form method = "post" action="/sample/sampleweb.nsf/myForm?OpenForm&Seq=1" enctype="multipart/form-data" name=_myForm">
<input type="hidden" name="__Click" value="0">
</form> //This comes out when I add end form tag in the first line of code.
<form class="searchbox">
<input id="searchtxt" class="searchbox-submit">
<input type="submit" class="searchbox-submit">
</form>
<form class="form-horizontal">
//Rest of the code go here having the input fields.
<button type="submit" onclick="return validatefields(event)">Submit</button>
</form>
</form> //This part is the generated form tag.
</body>
</html>
Any help would be appreciated.
Domino creates the form tags for you because it assumes you are doing data entry if the form is in edit mode. To prevent this use the setting on the form properties second tab (propeller head) for "On Web Access" "HTML". With that you can do anything you want in HTML but as Richard suggested you need to define where to go when the form is submitted.
Once created this way you need to use the ?ReadFrom url parameter rather than the ?OpenForm parameter

How to retain value of text input while navigating to back page in PHP?

I want to retain entered value in text box after navigation. I am explaining my code here.
**main.php**
<html>
<head></head>
<body>
<input type="text" id="myname" value="">
Next
</body>
</html>
**next.php**
Back
I don't want to use 'button' field here. On link navigation I want to retain the entered value in text box field. I am using php, html
you should save it in a cookie/session.
Or you can use localStorage too. Here's a link to read more
<form id="myform" name="myform" method="POST" action="next.php">
<input type="text" name="query" id="query">
Submit
</form>
<script type="text/javascript">
function submitform(){
// alert('form submit');
document.forms["myform"].submit();
} </script>
And in next.php
$_SESSION['last_search_term'] = $_REQUEST['query'];
echo $_SESSION['last_search_term'];
This will work . I have check it
I would use cookies. Note sessions and Opera back button do not work well together.
However if you want a cookieless solution you can use "window.name" string which is persistent i.e. its value is retained on navigation between pages. No need for a button as you required.
Just add this code before the closing </body> tag.
<script>
// on navigation to, or back: load users input from previous page (if any)
document.getElementById("myname").value = window.name;
// save value on navigation out
window.onbeforeunload = function() {
window.name = document.getElementById("myname").value;
return;
}
</script>
Assuming you give your text field the same id you can use the same js without modification on both/all pages.
The basic example code above should work with all modern browsers and most old browsers (even Opera since 2013). Modify it to use an event listener sanitize/check for empty values etc as required

assigning javascript variable to jsp in the same page

I need to assign a javascript variable to jsp. I am doing this by submitting a html hidden field inside a form to server.
But finally the value in jsp is alwasy null. These jsp code and javascript are in the same page. Please have a look a the code:
<html>
<body>
<form action= "Custom_DHTMLDashboard_Content.jsp" method="post">
<input type="hidden" id="hiddenField" />
document.getElementById("hiddenField").value = reportID;
<input type="submit" value="Submit" />
</form>
</body>
</html>
<%String MyID = (request.getParameter("hiddenField"));%>
alert("this is scriptlet" + "<%=MyID%>");//always null
You need to:
Put <script> tags around your script (document.getElementById("hiddenField").value = reportID;)
Define reportID somewhere (before you try to use it)
Escape the data you output with <%=MyID%>. Outputting unfiltered, unescaped data to the page is how you make XSS vulerabilities
If you fix that then:
The first time you load the page, <%=MyID%> will be null
When you submit the form, it will have a value

How can I set a JavaScript variable from a form without a user clicking submit?

I have a form with several different text inputs. The value of each corresponds to a unique JavaScript variable. The variable is then written out on the page.
Is there a way to update the variable automatically so the user doesn't have to hit submit and reload the page each time? I have heard AJAX could be a potential answer to this, but I am unfamiliar with it. If that route is best, are there certain scripts that would be recommended?
You might want to look into jQuery as a good first start into javscript in the browser. For instance, you could write:
<form id="form-id">
<input id="first" type="text" />
<input id="second" type="text" />
<input id="third" type="text" />
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
var first, second, third;
$(function(){
$("#form-id").submit(function(){
first = $("#first").value();
second = $("#second").value();
third = $("#third").value();
return false; //Prevent the form from submitting.
});
});
</script>
http://jquery.com/

Categories