Passing javascript variable defined inside function to jsp without reloading - javascript

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.

Related

How to parse django view object to JavaScript object?

I am trying to build this :
There is a button, and if the button is clicked, it sends some request to django server and server returns some form. After that, put the form into a div.
So, my idea is
make a event handling function to handle button click event
put AJAX operation into the event handling function to send request and put a form into a div
Write view to receive request and return form that fits with request
the AJAX operation at Step 2 put returned form into the div
But the problem is, at step 3, returning form.
The form is
form = EventForm({'date':date(year,month,day)})
Which is a ModelForm based on the model Event.
Because Javascript cannot understand Django form object, I need something to parse it into a javascript object, like <div> node, or just some HTML string, but I don't know :(
I know there is {{ form.as_p }} for template, but I wonder whether there is such functions for python view codes.
With ajax, you won't be able to use django forms.
You would have to hand-craft the html form, and then use ajax to send the request to django, which would respond with a json object (most likely) which you would, again using some javascript running on the browser, be able to render on your html.

Call JS function before redirect

The situation is as follows:
A user on the client side enters some data and presses a command button that initiates an ajax request. Depending on the input data the JSF Bean on the server side redirects to different pages. But in addition the HTML5 features localstorage respectively sessionstorage should be updated on client side, also depending on the results from the bean. For the storage some data entered from the user is necessary, so that the java script for the storage has to be within the original page.
Now, the problem seems to be that after the response the redirect is always done first, before executing the js function and so it is not possible to access the input data from the user on the client side of curse.
I tried things for calling the js function after pressing the command button like "oncomplete" on the client side using callback parameters (redirect takes place earlier) or RequestContext.execute from the server side (but this internally also uses the oncomplete event i think).
One possible solution I could imagine is to use window.onunload on client side and a hidden input formular to get the result of the bean. But isn't there a better solution?
You need to redirect by JS instead of by JSF. Return null from action method so that it returns to the same page and then execute the following script to perform a redirect in JS.
window.location = newURL;
If you use PrimeFaces with JSF then it's possible to make global override of PrimeFaces.ajax.ResponseProcessor.doRedirect in javascript to put any custom code before actual redirect happens:
PrimeFaces.ajax.ResponseProcessor.doRedirect = function(node) {
// <<<< your code is here >>>>
window.location = node.getAttribute('url');
}
Put this code in some js file and include it in all your pages.

JQuery Dialog with content that depends on javascript

I'm the midst of creating a form inside a dialog window with jQuery.
I'm currently loading a partial view with the jQuery load() function, and then adding the view into a jQuery UI dialog box. The partial view contains an html form which also contains a validation script in the markup.
This is how I load the view and instantiate the dialog window:
$('<div/>').load('/controller/view #targetDiv').dialog();
Unfortunately the load() function discards the script inside the partial view. :(
The validation script is important since it makes ajax calls to the form controller on an 'input blur' event, and returns the server side form-validation response. This means that the validation script must run continuously on the form inside the dialog.
The problem is that I load the view but there is no javascript.
Are there any solutions to this problem?
you can save the javascript codes in a separate .js file and use getScript() method.
Load a JavaScript file from the server using a GET HTTP request, then execute it.

how to assign javascript variables to jsp or jstl

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.

How to get the textbox value in PHP?

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.

Categories