How get input value in scriplet on jsp - javascript

I have html code:
<input type="text" name="username" id="username">
How can I get username's value in my scriplet ?
<% String username= request.getParameter("username");
%>
But it doesn't work for me
How can I solve this?

First make sure that requestObject is only available when you send the requestto the server or Submit the form .
on Client side pass Some action inside form tag and mapped into XML file
<form action="Submit.do">
from server side like servlet/Strtus/JSP you can access requestobject using
<% request.getParameter("inputid");%>
if you calling Same page after Submitting the data then on scriptlet check for request object if it null then don't perform any action else get the object

Related

Sending data to php script without ajax

I am required to send data from an html page, via input elements to a php script and I cannot use ajax for some reason. How to accomplish this?
My code is something like this:
HTML:
First Name : <input type"text" name="first_name">
<br><br>
Last Name : <input type"text" name="last_name">
<br><br>
<button id="submit_btn">Submit</button>
Now, I want the javascript to be something like :
function redirect_from_here() {
close(); //close the current window
window.location='./phpfile.php'; //load the new page which will process the data sent to it.
}
My question is how do I send the value in the input elements in the HTML portion, as data to be processed, to the php script (phpfile.php in this case).
Please note that I prefer not to use html form for doing the job.
You are using HTML form as there are input fields in you code.
Inputs are part of a form and should be wrapped by a form element
Why using JS for submitting the form when you can use <form action='script.php'.. for that?
I suggest that you revise your requirements instead of trying to come up with a hackish way of how to send the data..
Just submit a form with action="phpfile.php"
if not interested with html,
then in the window location bind the values as a GET form method do

confusing syntax in JSP portal

JSP code in Liferay portal..
<portlet:actionURL name="addDetails" var="addDetailsURL" />
where from var gets value ?
var just exposes the action URL value through a variable named addDetailsURL. This is so that you can use it conveniently in a link as so:
Link
is used to perform any action(Action Phase) i.e user submitting form and controller(processAction) processes that request.
i.e
<form action="<%= addDetailsURL %>" method="POST" name="form">
// input fields which will get submitted to controller
</form>
from pageContext.
ActionURL.toString() is assigned to addDetailsURL and added to pageContext

php form : twig parameter from javascript or textarea html

i have a textarea defined by an id
<textarea id='vegetable'> Tomato </textarea>
i have a button
<button type="button" onclick="MyFunction()">generate vegetable</button>
which trig a javascript in order to modify the content of the textarea
<script>
function MyFunction()
{
document.getElementById("vegetable").innerHTML = VegetableNameGenerator();
}
</script>
The problem is this php action :
<form action="{{ path('mypath', { 'myparam': ??? }) }}" method="post" >
<input type="submit" />
</form>
??? must be the content of the textarea (which is also known by the javascript code) but i don't know how to access it in twig.
I guess there are several way of doing that : jquery, dom, global variable twig... any syntax example would be great.
This is impossible in the way you're describing it.
When a request is made to the server, twig renders the page, then it is sent to the browser, and then javascript can run.
There are options for how to make this work:
Create a controller action which returns the rendered path when you call it using a GET request with the provided parameter. Then create an event listener on the submit button that blocks the submit process until it has retrieved the route via AJAX and modified the form's action attribute.
Redirect from your controller to the path you want displayed. myparam will be included in the post data, so you can redirect from your controller action after you've handled the form.
$this->redirect($this->get('router')
->generate('mypath',
array('myparam'=>
$this->getRequest()->get('myparam')
),
true);

Is there a way to pass a ServletRequest parameter to JavaScript?

Is there any way to pass the value of a parameter from a Servlet to JavaScript ?
the following didn't work.
var val = req.getParameter('valid');
This won't work because the ServletRequest only exists at the server and your JavaScript is running at the browser. It is easy to get the difference confused, because the code that runs on the server and the code that runs in the browser are very often both written in the same file (someServlet.java or somePage.jsp), so you have to remember how everything will sit at runtime.
What you can do, as a way of passing information that is retrieved from the request in the servlet to the JavaScript, is embed the data in the structure of the page on the server side. The page and its structure are then passed to the browser and the JavaScript has access to the full page structure on the client side. So you put something like this in the servlet:
<form name="data" action="" >
<input type="hidden" id="parmEmpId"
value='<%= request.getParameter( "EMPLOYEE_NUMBER" ) %>' />
<input type="hidden" id="parmServerName"
value="<%= request.getServerName() %>" />
</form>
And then, in your JavaScript, you can pull the data from the page:
var employeeId = $("#parmEmpId").val(); //Using jQuery
var server = $("#parmServerName").val(); //Using jQuery

How can I pass parameters to a new page from Javascript?

I have the following code:
$('#CreateButton').click(function (event) {
var datastoreValue = $("#SelectedDatastore").val();
window.location = "/Q/Create?datastore=" + datastoreValue;
});
It works good but I would like to have the datastore=XYZ hidden from my user. Is there another way that I can pass this information so I would be able to read it in my MVC controller. Currently I read it like this:
public ActionResult Create(string datastore)
{
When inside this method the value of datastore is available to me.
Surround you code with a form tag with a method set to POST. Also add HttpPost attribute above your Create action. This will post the data to your action without client seeing the query string value. If you are posting to another action that specify that in your action attribute inside form. For example,
<% using (Html.BeginForm("Create", "MyController", null, FormMethod.Post)) { %>
<%: Html.Label("Data store value: ")%>
<%: Html.TextBox("datastore") %>
<input type="submit" value="Submit" />
<% } %>
Your Create action will pick this up and do the rest.
Hope this helps,
Huske
You could put the datastore value in a cookie to hide it from the user, though cookies are best used for settings that you want to last awhile, not settings that are meant just for one page.
Or, you could use a Post and put the create information in a hidden form and submit that.
Or, you could use an Ajax call to issue the create and then change the page yourself afterwards.
Otherwise, the query parameter like you are using is a standard way of specifying page-level parameters for a new page.
If you stay on the same website, you can post your data to your controller; it will pick up the data from the post collection and your url won't display the query string parameters.
Just wrap your form with the control whose id is SelectedDataStore in a tag with the action set to your url and the method set to post
Here is one quick example with a submit button:
<FORM action="/Q/Create" method="post">
<INPUT id="SelectedDataStore" />
<INPUT type="submit" value="Send">
</FORM>

Categories

Resources