assigning javascript variable to jsp in the same page - javascript

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

Related

Why is this $_GET empty after form is submitted by GET, and why is a "?" appended to URL on form submission

Here is an SSCCE demonstrating my problem. At the top of the page, the $_GET is printed. When the page is first loaded, it is understandable that an empty array Array() will be printed.
But the form's action attribute has an empty value, which means the page is submitted to itself. The value of method is GET. So the form is submitted by GET.
So when the form is submitted to the page itself by GET, now the $_GET array printed at the top of the page must NOT be empty. Oh, but it is! And that is the question.
When the form is submitted,
the $_GET array printed at the top of the page is empty. Array ()
A ? is appended to the URL. For example, before the form submission, the URL of the page was http://localhost/Test/index.php. After the form is submitted, the page reloads and the URL becomes http://localhost/Test/index.php?
The question is why? and how do I fix this.
<?php
print_r($_GET);//check
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.1/css/materialize.min.css"/>
<script src="<script
src="http://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.1/js/materialize.min.js"></script>
</head>
<body>
<form action="" method="get">
<div class="input-field">
<input type="text" id="nameInput" class="validate" />
<label for="nameInput">Name: </label>
</div>
<button type="submit" class="btn waves-light waves-effect">Submit Form</button>
</form>
</body>
</html>
The $_GET array printed at the top of the page is empty.
This is because you need to provide a name attribute on the input for it to be added to the key/value pair data sent in the request:
<input type="text" id="nameInput" class="validate" name="foo" />
A ? is appended to the URL. For example, before the form submission, the URL of the page was http://localhost/Test/index.php. After the form is submitted, the page reloads and the URL becomes http://localhost/Test/index.php?
That's because that is how a GET request works; the information is appended to the URL as application/x-www-form-urlencoded data.
Once you send the request with the name attribute added to the input, the URL will become:
http://localhost/Test/index.php?foo=[value]
The $_GET is empty because your input have no name.
The "?" is put by default when you send a form by GET : http://url.com?param1=value1&param2=value2&...
Is there some kind of syntax error of your code?
<script src="<script
src="http://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>"></script>
Please check you syntax first.

insert javascript value into form field

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' ;

How do I use the <form> tag to send data to a javascript function?

I was testing and trying to make an little form that when the user entered their name, it would take that name and display it on to the screen.
<html>
<head>
<center><h1>Test-Page</h1></center>
</head>
<body>
<div class="someRandomStuff">
<h2 id="testingID">What is your first name?</h2>
<form name="input" action="login.js" method="post">
Name: <input type="text" name="userID"/>
<input type="submit" value="Submit"/>
</form>
</div>
</body>
Here is the js file
function displaySystem(name) {
document.getElementById("testingID").innerHTML("Ah, hello there" + name)
}
I know that I could probably do this in one HTML file, however I want to try and make the js and HTML separate. ANY help is appreciated.
You don't send data to a JavaScript function, but a JavaScript function can retrieve form data.
For example, and input of type text can be retrieved using its value property:
var input = document.getElementById("userID");
var value = input.value;
I know that I could probably do this in one HTML file, however I want
to try and make the js and HTML separate.
Nice step. In fact, there's no practical difference in terms of retrieving form data or manipulating the document from an inline script or a script that's included using a <script src=... element. The main difference is a script embedded in the HTML document won't be cached, while a one included as a separate file will be cached (obviously, there're other reasons if we talk about good separation of concerns!).
use onkeypress event on textbox and pass this to display that value and use that parameter in function to display it
<div class="someRandomStuff">
<h2 id="testingID">What is your first name?</h2>
<form name="input" action="login.js" method="post">
Name: <input type="text" name="userID" onkeypress="displaySystem(this)"/>
<input type="submit" value="Submit"/>
</form>
</div>
//javascript function
function displaySystem(name) {
document.getElementById("testingID").innerHTML("Ah, hello there" + name.value)
}

How to append dynamic text to url in PHP?

I have a html form which method type is post, now i want to append text to my website url in PHP. The text changes dynamically.
current url :
www.example.com/index.php
expected url :
www.example.com/index.php/hotels-in-bangalore
(or)
www.example.com/index.php?qry=hotels-in-bangalore
I just want to append text, other than that every thing should remain same. Thank you.
Im not sure why you try to accomplish, but here are few solutions:
put url in form action:
<form action="index.php?qry=hotels-in-bangalore" method="post">
or you can do it in php
if (!empty($_POST['name']) {//put some logic here
header("Location: index.php?qry=hotels-in-bangalore");
exit;
}
You need to use JavaScript if you want to redirect to correct page after submitting immediately.
So it will be something like this:
<form>
<input type="text" name="tbSearch" id="tbSearch" />
<input type="submit" id="submitSearch" value="Submit" />
</form>
<script type="text/javascript">
$("#submitSearch").click(function() {
window.location.href="index.php?qry=" + encodeURIComponent($("#tbSearch").val());
});
</script>

how can we provide different submit buttons in jsp, and execute respective page of button.?

I want to know how can we have different buttons in jsp, and how to submit the respective page for the button we pressed.
I have tried fallowing code :
<html>
<head> </head>
<body>
<%-- scriptlet here --%>
<%
if(request.getParameter("btnSubmit_1")!=null)
{
// means submit_1 is pressed.
// in java script we do give action as fallows
/**document.frm2.action="sub-faconline.jsp";
//document.frm2.target="right_f";
//document.frm2.submit();****
my question how can we do above java script code in scriplet in jsp
}
else if(request.getParameter("btnSubmit_2")!=null)
{
// i want to give some jsp page name here for action.
}
%>
<form name="frm2" method="post">
<input type="button" id="btnSubmit_1" name="btnSubmit_1" value="btnSubmit_1" />
<input type="button" id="btnSubmit_2" name="btnSubmit_2" value="btnSubmit_2"/>
</form>
</body>
</html>
You have to modify your html to this:
<form name="frm2" method="post" action="myJsp.jsp">
<input type="button" id="btnSubmit_1" name="btnSubmit_1" onClick="submitForm('btnSubmit_1'); value="btnSubmit_1"" />
<input type="button" id="btnSubmit_2" name="btnSubmit_2" onClick="submitForm('btnSubmit_2'); value="btnSubmit_2"/>
<input type="hidden" id="btnPressed" name="btnPressed" value=""/>
</form>
Javascript:
function submitForm(elem){
document.getElementById("btnPressed").value=elem;
document.frm2.submit();
}
And then in your jsp you can get the button value that was pressed using:
request.getParameter("btnPressed");
Explanation: Create an invisible input in the form that will hold the value of the btn. Then attach onclick functionality to each of the buttons that will set the value of the hidden input. And then submit the form to the form's action URL. Then in that url, you can get the hidden input's value(that contains the value of the button that was pressed)

Categories