On my page
I have one textbox and one button.
-- on the button click event. -- one function will be called.
Now,
How to get that value of textbox and store in PHP variable? This should be done in that function which we will call on button click.
You'll have to know that Javascript and PHP run on two different servers. So they can not communicate directly.
You'll basicly have two options:
POST the value
Make a form with a post action, submit the form, and you can access the value in PHP with the $_POST array
Disadvantage is that the browser leaves the page to submit the form, but it doesn't require javascript.
Use AJAX
Get the value in Javascript, make an XMLHTTPRequest to the server, and you can access the variable too via $_GET or $_POST, depending on how you sent the value.
When your page is displayed, your PHP code has already been executed.
What you want to do is to learn AJAX, i.e. JS functions that call external PHP scripts.
Related
I want to load a bootstrap modal. The contents of that modal are dynamic based on onclick function on button.For this ,onclick of button, javascript function with one parameter is called.
Function sets $('#modalId').show();
modal should contain content fetched from database based on parameter passed to javascript funtion. So for this I want to transfer the passed parameter from js to jsp of modal body. How to access that parameter which was passed on click.
The jsp code is evaluated on server side before resulting html code is sent to the browser, while js code is evaluated on client side in the browser. So there is no way to pass parameters to jsp code directly.
But of course there is a way to solve your problem. You need to make an ajax request in your javascript code when user clicks on that button. You can pass the parameter in your ajax request which might simply load another jsp which can access the request parameters, perform any database queries you like and return some html.
On client side (in javascript code) you simply need to place the returned html code in your modal dialog or wherever you like and display the results.
I need to download a file from a server and to get a pop-up 'save as' box on the client. I can't do this in Ajax, so I create a hidden form in the client JavaScript, and I submit the form in the JS when a button is clicked. The server gets a POST, and it sends the file back as an attachment, and the client produces the 'save' box.
So far, so good, except that there are two problems here:
1 - I want the JS to delete the newly-created form when the user has completed the download. I suppose I could just leave a useless hidden form in the DOM, but it's not ideal. The problem is that form.submit() executes asynchronously, so I don't know when to delete the form - I can't simply do it after executing form.submit()
2 - Sometimes the user actually needs to download two files. This code doesn't work:
form1.submit(); // download file 1
form2.submit(); // download file 2
The client only executes/completes one of the submits - I can do both by putting an alert between the two, for example, but I need to do it properly.
If I was doing this with Ajax, I'd just make the calls synchronous, but I can't find a way to do this with form submission. Ideally I'd like an attribute to make the submit synchronous (something like .setAttribute('async', false), which doesn't work).
Any ideas? Or another way to download two files with two save-as dialogs?
A great trick is to use a cookie. The trick works like this:
Create your temporary form, and add two extra fields that you populate, one with a cookie name and the other with some unique value (could be random, could be a timestamp or a counter; doesn't really matter).
Submit the form.
At the server, the code should do whatever it normally does to create the download. It should also create a new cookie with the given name and value. Return the response.
Back at the client, right after submitting the form, start an interval timer to check (every 100ms or so) to see if there's a cookie with the chosen name and chosen unique value. As soon as you see the cookie, you know that the form response has arrived!
As to downloading two files, I don't think there are any provisions in HTTP for two attachments. You can of course return something like a zip file.
I am using html5, javascript and JSP for my project. I want to know if there is some method that i can used to execute a query from my servlet without actually posting back the page. i know it can be done in ASP.net but i do n't how it can be be done in java script and JSP. Actually i have a dynamic webpage displaying data from server.what i want is that in a click event of button i want to execute a query form server and update it on the page. i know i can submit the form but it will submit the page which i want to avoid.Any suggestion......
regards
nquazi
You can use an AJAX request to submit inputs and get back an output without reloading that page. Here is a previous stackoverflow answer that shows you how to do a HTTP GET request.
HTTP GET request in JavaScript?
You will then need to process your inputs, run the query, and send back an output on the backend server.
I want a scenario in which I will set some value of a hidden field in a particular page.
Then that page is submitted on server (form submit). Now, i redirect on another page and there I again try to retrieve the value which I set previously. But I am not getting there the value which was set, instead i get the default value which I provided in html page itself. (Hidden field is in header page which is common for all the pages in my web app).
i tried a dummy application in which i am getting the value of hidden field even after loading/refreshing the page once i set it.
When you redirected your user to another page, it became reloaded. Unless you chose to set a value to your form (by javascript for instance), the value of the form is the default one.
The value you "set previously" wasn't definitely associated to the input because everytime you reload the page, your server will generate again the HTML and the default values and your browser will display this HTML.
This behavior is normal.
Besides, if you want to keep the values of the form while submitting it, you can use AJAX submitting.
The other answers here are factually correct (that HTML doesn't normally do what you're asking it to do), but there are a few things you can do to make it work.
First, how things usually work: In order for the second page to get the proper value of the hidden field, you would process it in the server-side component. It sounds like you are redirecting to a new page in the server-side handler. The best way to make this work is to have that server-side handler process the value and attach it to the redirect as a parameter (likely attached to the querystring). Then have some server-side code generate the second page, which would process the querystring parameter.
Here's the work-around for pure-HTML/javascript implementation:
If you can't or won't have a server-side process to generate the second page, you could pull it out of the querystring using Javascript (just search for 'getting querystring variables in javascript').
If you use javascript, it could be feasible (though probably not advisable) to have the first form go directly to the second page by setting it as the form's action with a method of 'GET'. It's definitely better to include a server-side handler though.
What your trying to do is impossible through regular HTML since HTML is stateless. What you want is to put your values in a session or in a cookie and this way you can plant it on every page that is loaded.This cannot be done by default.
You're mis-understanding how HTTP works - it is stateless.
This means that every single page you request is completely separate to previous pages. Which is the reason your hidden textbox is being set back to default.
You have to explicitly set the value server side prior to it being sent to the client.
Can anybody tell me how to assign javascript variables to jsp request or to jsp session.
I am doing something like this
Here deletedRows is a hidden field.
var del=45;
document.getElementById("deletedRows").value=del
alert(document.getElementById("deletedRows").value);
<%String del_values = request.getParameter("deletedRows");%>
<%request.getSession().setAttribute("del_rows", del_values);%>
I don't get the value of del in my servlet.
JSP gets compiled on the server. All the client gets is the "output" of the JSP: the HTML, CSS and Javascript.
The Javascript gets executed after this. Meaning everything in the JSP has become HTML et all when the javascript executes. You way want to think this as the Java/JSP part has "completed" and now the HTML/Javascript part takes over.
Now you want to pass on some value calculated/manipulated via Javascript back to the server. (I think this is what you mean when you say "assingn javascript variables to jsp request or to jsp session"
For this you have to submit the page to the server, and these values should be part of the form that is being submitted.
You may already have these values in some HTML elements (like a <input> or <select>), if not you can create hidden elements and populate these with the values before submitting the <form>.
In the code you have provided, you are populating the hidden field correctly, but you have to retrieve the value in the servlet, not in the JSP itself. Also, make sure that the hidden field in in a <form> and that form is submitted.
Once the form is submitted (to a servlet) the values can be retrieved in the servlet via request.getParameter.
There are few other mechanisms to send a value to the server, using a URL parameter or via Asynchronous (AJAX) requests, but I am not sure whether you are looking at these also.
Any form fields, including hidden fields, that are submitted from the browser will be accessible in your JSP using request.getParameter("fieldname");. Query-string parameters may be accessed the same way.
Make sure that your form fields have a name attribute specified because it is that name (not the id attribute) that becomes the parameter name in your server-side code.
What you've already done in the little bit of code shown in your question, i.e., set the hidden form field to have the value of a JavaScript variable, should allow that value to be submitted and then accessed in the server-side code. But it's hard to see why it is not working without seeing at least some of your form HTML, particularly the definition of the hidden field. It would also help to see how that is being submitted. (I'm assuming it is being submitted: if you are trying to make all of that code run just on the server it won't work, because the JavaScript is treated as document content by the server, it isn't executed. Again, I can't really tell how you're using that code without seeing more of the surrounding JSP.)
UPDATE: I see that your code has been formatted since I started typing my answer. You aren't expecting all five lines to run on the server are you? The JavaScript code only runs on the client browser after the page is rendered. The Java code in between <% %> is executed on the server before the page gets to the browser and so can't access JavaScript at all. Anything not in the <% %> tags is simply sent to the browser as is - the servlet doesn't interact with it as such.