I have a method Processsots which I am trying to invoke from submit button, But when I diasble by OnClick element. it is not calling the method. Any one help me how to call the method after disabling the submit button as well
<table>
<tr>
<td width=150> </td>
<td width="1999">
<h1 ALIGN='Left'>Please click on the Button to process Sots feed file</h1>
<form name="sotstestorderfoler" >
<br>
<input type="submit" name="username" value="Process Feed File" onclick="this.disabled='disabled';>
</form>
</td>
</tr>
</table>
<%
if ( request.getParameter("username") != null ) {
Processsots();
}
%>
(Code tried with onsubmit)
<form name="sotstestorderfoler" method="post" onsubmit="foo()" >
<script type="text/javascript">
function foo() {
sotstestorderfoler.username.disabled=true; <%Processsots();%> return true;
}
</script>
</form>
I would use the onsubmit of the form to disable your button, like these answers illustrate:
https://stackoverflow.com/a/12944611/2047962
https://stackoverflow.com/a/19696817/2047962
Realize that JSP code is server-side and JavaScript is client-side, so they can't directly interact with each other.
You are mixing JSP code and JavaScript. The JSP code is only executed on the server. When your browser requests your webpage, JSP code is executed in order to build your webpage. That means that it starts writing out things like <script type="text/javascript"> and function foo() into a stream to send back to the browser. When it reaches <%Processsots();%>, it doesn't simply write out that text, it calls your method Processsots(). It will then write out the return value of Processots(). This means that your method is already executed before users even see your page or try to push the submit button.
This is a mistake that many new JSP programmers make; your JSP code cannot be executed while your JavaScript is executing. The only way for JavaScript code to interact with JSP code is via HTTP requests, usually in the form of an AJAX call or a form submit.
Instead of trying to call a JSP method in the JavaScript, you must have the form submit to a Servlet that can then call Processsots() and return the result as an HTTP response.
Related
I'm trying to create a button with an onclick function that activates the imggrabscreen php function. Problem is, I've done several codes and so far the only function that I was able to use was a submit input type in which this refreshes the page. I tried using button as an input type but unfortunately, it does not save any screenshots upon clicking the button. Here's the code that I'm using so far.
if(isset($_POST['btnscreen']))
{
$im = imagegrabscreen();
imagepng($im, "screenshot.png");
}
ob_end_flush();
?>
<form method="post" action="" enctype="multipart/form-data">
<br>
<input type="submit" value="Click to Screenshot" id="btnscreen" name="btnscreen"></center>
<br><br>
</form>
php is parsed and executed server side (pre-processing) so you cannot call any php functions after the page has been sent to the browser. The only way to do this is to make a new request to the server (ajax).
I can't quite grasp what your function does (php cannot make a screenshot of what your browser is displaying as it has no information of how it has been rendered - please note different browsers may display the page differently).
I would try reading up on html5 canvas element which can achieve that (e.g. https://html2canvas.hertzen.com/).
Hope this helps
I've got this research.php file that should show the content of a database table, taking the informations for the query from a javascript form.
I made in javascript the form and disabled the enter key, so that only by hitting the button you can submit the value. The value of the components in the form should be used to make the query. Can I pass the value to a php variable in the same file?
<!DOCTYPE html>
<html>
<head>
<!--
Implemented by Argese Alessandro and Rosso Kevin
The slider is taken from "https://dhtmlx.com/docs/products/dhtmlxSlider/samples/index.html"
Download slider: "https://dhtmlx.com/docs/products/dhtmlxSlider/"
-->
<title>Ricerca diplomati</title>
(...) <!--slider things-->
<style>
(...)
</style>
<script>
var mySlider;
function doOnLoad(){ //slider load
(...)
};
function doOnUnload(){ //slider unload
(...)
};
window.addEventListener('keydown', function (event) {
// if the keyCode is 16 ( shift key was pressed )
if (event.keyCode == 13) {
// prevent default behaviour
event.preventDefault();
return false;
}
});
function interact(form){
(...) //here the value is put in js variables, for a test
});
</script>
</head>
<body onload="doOnLoad();" onunload="doOnUnload();">
<form id="filters" name="filters" action="#" enctype="text/plain">
<table cellspacing="0" cellpadding="0" border="0" class="filters_table">
<tr>
<td>Specializzazione:</td>
<td>
<select name="specialization" class="table">
<option selected value="0">Tutte le specializzazioni</option>
<option>Chimica</option>
<option>Elettrotecnica</option>
<option>Informatica</option>
<option>Meccanica</option>
</select>
</td>
<td></td>
<td></td>
<td>Valutazione finale:</td>
<td><div id="sliderObj"></div></td>
<td><span id="sliderLink"></span><span class="minussign">-</span><span id="sliderLink2"></span></td>
<td></td>
<td></td>
<td>Città :</td>
<td><input type="text" name="city" size="35" maxlength="40" value=""></td>
<td></td>
<td><input type="button" onclick="interact(filters)" value="search" style="background: url(res/magnifying-glass-16x.png); background-repeat: no-repeat; background-position:center; background-color: #0c84e4;width:24px; height:24px;"></td>
</tr>
</table>
</form>
<?php
function viewTable(){
echo "Here the table will be shown";
}
?>
</body>
</html>
In this code the button click just call the interact function, that show an alert window with the value of the variables.
I tried to use ajax but, as far as I understood, it would open a new file.
I've also seen a solved answer in which someone suggested to use the cookies, but that solution really disgusted me.
The most helpful thing I found is here:
How do i store select value into a php variable,
but the POST method refresh the page and the slider crushes.
If you want to pass a variable from JS to PHP you are going to want to use AJAX, it allows you to send and receive info from server running the PHP script.
Your Javascript:
var infobeingsent = "test";
$.ajax({
type: "POST",
url: 'YOURPHPFILE.php',
data: { testVAR : infobeingsent },
success: function(data)
{
alert("data has been sent");
var recievedvar = data;
}
});
Your php file:
if(isset($_POST['testVAR']))
{
$recievedvar = $_POST['testVAR'];
echo "Hey, ive recieved your request";
}
Using AJAX means that your browser never has to come into direct contact with the php file and there's no interruption from the end-users perspective
I think you are a bit confused about the whole work flow between php (server side) and js (client side). OR may be I got your question wrong. But here is a hint on how you can approach this.
You have a server side running php - Great.
You have an html page with js - Awesome.
In the html page, you have a form that you want to run a query based on the user submission:
AJAX is the way. You can do it with pure javascript or a plugin like jquery. On the php side, you can even keep a small, seperate script just to handle this request, run the query and generate the result. Use "onSubmit" for the form to invoke ajax.
I am saving a data with this button
<p><input type="Submit" value="Save" id="Save" /></p>
Is this possible that it also reloads the page at the same time.
I am using this code.
<script type ="text/javascript">
$('#Submit').click(function () {
location.reload();
});
</script>
But it is not working. It only saves the data.
You can do this in two ways:
Forget about JavaScript and make the MVC action return a Redirect response to the page you are coming from.
In stead of type="submit" create a button and attach an event handler which makes an AJAX request and reloads the page when the request has completed. Notice that this solution costs an extra server round trip compared to option 1.
Example for option 1:
return Redirect("Controller", "Action", additionalParamsObject);
I ve written the following for displaying different rows and columns of a DB. and infront of every row, there is a Delete Button.
Onclick of the Delete button should call a set of function/ perform the code written inside Onclick event(check below)..Now what's happening is when the JSP page is loading , its going inside the method. So if there are 3 rows,
it will invoke the method 3 times....HELP ! it should invoke these method only "ON CLICK" of Delete button...
<tbody> <%
int x;
while(rs2.next()){
x=0;
%>
<tr><%
while(x<d)
{
x=x+1;
%>
<td> <%
out.print(rs2.getString(x));%></td>
<% } %>
<td> <input type="button" value="Delete" class="btn" id="DeleteBtn" onclick="<% System.out.print("Delete button"); tableupdateBean.setupdateRow(rs2.getString(x)); tableupdateBean.DeleteRow();%>" /> </td>
</tr>
<% } %>
</tbody>
It looks like your are mixing things between javascript and jsp scriptlet.
onclick is a javascript event that occurs when an element gets clicked, in this case a button. onlick="" should reference a javascript function but in your code sample you have a scriptlet.
Scriptlet are executed when the JSP is invoked, so when the page loads.
onclick is client side event and you can only specify any javascript function which will be called when this event will occur. You can not specify any server side jsp/java function.
You need to construct a delete url with an Unique Id to identify the deletion row from the server side and reload the full page again.
Client Side
<a href="/yoururl?mode=delete&id="+<Unique Id>>Delete Row</a>
Server Side
if("delete".equals(request.getParameter("mode"))
{
String id = request.getParameter("id");
// write code for deletion
}
//redirect to the same page again
If you don't want to reload the full page, then you have to write ajax calls to perform deletion in the server side and use javascript remove the row from the table
I've created a jsp page. In that when i select 1 check-box or both check-box or none, the corresponding text-boxes and list-box must be displayed in the same page.
For that i tried of calling a javascipt function when i click the checkbox. The javascript function contain code to display the textboxes. But it didn't work.
Since I'm doing this project in struts, I don't know how to get check-box value. And calling of JavaScript function works. But didn't enter into jsp code in JavaScript function.
My code is
<tr>
<td>SEJ:</td>
<td>SEJ 1:<html:checkbox property="sej1" value="on" onclick="checkbox_trial()"></html:checkbox></td>
<td>SEJ 2:<html:checkbox property="sej2" value="on" onclick="checkbox_trial()"></html:checkbox></td>
</tr>
<script type="text/javascript">
function checkbox_trial()
{
<% if(request.getParameter("sej1")=="on"){
%>
<tr><td>SEJ1 Age<html:text property="sej1_age"></html:text></td></tr>
<tr><td>SEJ1 DOI<html:text property="sej1_doi"></html:text></td></tr>
<%}
else if(request.getParameter("sej2")=="on"){%>
<tr><td>SEJ2 Age<html:text property="sej2_age"></html:text></td></tr>
<tr><td>SEJ2 DOI<html:text property="sej2_doi"></html:text></td></tr>
<%}
else if((request.getParameter("sej1")=="on")&&(request.getParameter("sej2")=="on")){%>
<tr><td>SEJ1 Age<html:text property="sej1_age"></html:text></td></tr>
<tr><td>SEJ1 DOI<html:text property="sej1_doi"></html:text></td></tr>
<tr><td>SEJ2 Age<html:text property="sej2_age"></html:text></td></tr>
<tr><td>SEJ2 DOI<html:text property="sej2_doi"></html:text></td></tr>
<%}
else{%>
NOTHING <% } %>
}
This is how it works: Java/JSP runs at webserver, produces HTML/CSS/JS, webserver sends it to webbrowser, webbrowser runs HTML/CSS/JS. Not Java/JSP. Rightclick the page in webbrowser and choose View Source. If Java/JSP has done its job right, you shouldn't see any line of it in there.
If you want to invoke Java/JSP code using JavaScript, you've got to invoke another HTTP request to the webserver so that it can execute the Java/JSP code based on the specific request. You can do that by either submitting the form or firing an asynchronous (ajaxical) request.
Given the skills shown as far and the fact that you're using Struts, I think ajax is going to be a bit too complex. I'd suggest to just submit the form on click of the checkbox
<input type="checkbox" name="show" value="true" onclick="submit()" />
and then let JSP conditionally display the input fields (just a JSTL example since I don't do Struts)
<c:if test="${param.show == 'true'}">
<input type="text" />
<select />
</c:if>
Update: you've by the way another major problem in the code. You can't compare string values by == in Java code (you can do so in EL only). In Java code you need to use equals() method. Otherwise they will be compared by reference instead of by value. I'd suggest to learn basic Java as well.