How to trigger a javascript function when servlet forward action - javascript

I have a jsp page in my project. In this jsp page, I have inputs that are filling by javascript function. When the servlet post action happens, this input's values are disappears. I want to fill these input again when the servlet forwarded the jsp page. So, is it possible to trigger a javascript function from servlet?
My JSP page codes
<div style="margin-left:7%; margin-right:10%; border-style:groove;">
<div style="margin-right:65&; margin-left:5px;">
<label for="salesOrder">Sales Order</label> <input id="sOrder" style="margin-left:58px;" onkeyup="this.value = this.value.toUpperCase();" onselect="salesOrder();fill_table();" value="${salesorder}">  <label style="margin-left:75px;">Count:</label> <input type="text" id="count" size="3" style="margin-top:3px;"><br>
<label>Ind. Date:</label>  <input type="text" id="induction" style="margin-left:59px;" size="15"><br>
<label>Customer/Operator:</label> <textarea id="customer" rows="1" cols="10" wrap="soft"></textarea> <textarea id="operator" rows="1" cols="10" wrap="soft"></textarea><br>
<label>Removal Reason:</label> <textarea rows="2" cols="30" wrap="soft" id="rem_reason" style="margin-left:10px; height:50px; text-size:10px;"></textarea>
</div>
<div style="margin-left:45%; margin-top:-130px;">
<input type="checkbox" name="v2500" value="V2500-A5" onclick="check_v25()"> <label for="v2500">V2500-A5</label><br>
<input type="checkbox" name="7b" value="CFM56-7B" onclick="check_7b()"> <label for="7b">CFM56-7B</label><br>
<input type="checkbox" name="5c" value="CFM56-5C" onclick="check_5c()"> <label for="5c">CFM56-5C</label><br>
<input type="checkbox" name="3" value="CFM56-3" onclick="check_3()"> <label for="3">CFM56-3</label>
</div>
<div style="margin-left:65%; margin-top:-130px;">
<label>TSN:</label>  <input type="text" id="tsn" style="margin-left:1px;"><br>
<label>CSN:</label>  <input type="text" id="csn" style="margin-left:-0.5px;"><br>
<label>TSLSV:</label>  <input type="text" id="tslsv" style="margin-left:-1px;"><br>
<label>CSLSV:</label>  <input type="text" id="cslsv" style="margin-left:-3px;">
</div>
</div>
My JavaScript function
<script>
function salesOrder(event,ui){
$("#sOrder").val(ui.item.label);
var salesOrder=$("#sOrder").val();
var URL_PREFIX="http://ptrisd01:8983/solr/SalesOrderCore/select?q=STRSO:";
var URL=URL_PREFIX+salesOrder;
$.ajax({
url : URL,
dataType : 'json',
type:'get',
json : 'json.wrf',
success:function(data){
console.log("success");
var docs = JSON.stringify(data.response.docs);
var jsonData = JSON.parse(docs);
document.getElementById("induction").value=jsonData[0].STRINDDATE;
document.getElementById("customer").value=jsonData[0].STRCUSTOMER;
document.getElementById("operator").value=jsonData[0].STROPERATOR;
document.getElementById("rem_reason").value=jsonData[0].STRREMREASON;
document.getElementById("tsn").value=jsonData[0].STRTSN;
document.getElementById("csn").value=jsonData[0].STRCSN;
document.getElementById("tslsv").value=jsonData[0].STRTSLSV;
document.getElementById("cslsv").value=jsonData[0].STRCSLSV;
}
});
};
</script>
A part of my servlet codes
request.setAttribute("salesorder", salesOrder_hidden);
PrintWriter out = response.getWriter();
out.println("<script type=\"text/javascript\">");
//out.println("alert('Saving completed...');");
//out.println("location='upload.jsp';");
out.println("salesOrder();");
out.println("</script>");
request.getRequestDispatcher("upload.jsp").forward(request, response);
Also, I'm using JQuery Autocomplete for sOrder input.

What Swati said. Looks like you don't know about sessions yet. If you do it with Java, create Session at the servlet.
HttpSession session = request.getSession();
session.setAttribute("attributeName", value);
response.sendRedirect("your.jsp");

Related

web data capture .NET server

I'm trying to do a data extraction from a .NET web server but I cannot send the data correctly.
If I post the data with a normal html form I can show the correct page with the information I need.
The html form contains valid fields as __VIEWSTATE, __EVENTVALIDATION and other fields to query the server.
This form works fine but I cannot capture any data.
If I try to send the same data (with the POST method) using javascript with ActiveXObject("Msxml2.XMLHTTP.3.0"); or using Server.CreateObject("MSXML2.ServerXMLHTTP") in classic ASP, I receive in return from the server the message:
''The state information is invalid for this page and might be corrupted.,, or the correct page w/o the result of the query (page empty).
I think the problem is in the header, so I've tried to send in many formats but w/o success.
Any help ?
the following is the code that correctly send data
<form name="aspnetForm" method="post" action="http://geon....neNew.aspx" id="aspnetForm">
<input type="text" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="text" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />`enter code here`
<input type="text" name="__VIEWSTATE" id="__VIEWSTATE" value="/--------- here the key --"/>
<input type="text" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/--------- here the key --" />
<br>
DUG:
<input name="ctl00$ContentPlaceHolder1$txtDUG" type="text" id="ctl00_ContentPlaceHolder1_txtDUG" <td id="ctl00_ContentPlaceHolder1_TableCell3">
<br>
Denomin.:
<input name="ctl00$ContentPlaceHolder1$txtDenominazione" type="text" value="indipendenza" id="ctl00_ContentPlaceHolder1_txtDenominazione" >
<br>
Civico:
<input name="ctl00$ContentPlaceHolder1$txtCivico" type="text" value="34" id="ctl00_ContentPlaceHolder1_txtCivico" />
<br>
Espon:
<input name="ctl00$ContentPlaceHolder1$txtEsponente" type="text" id="ctl00_ContentPlaceHolder1_txtEsponente" >
<br>
CAP:
<input name="ctl00$ContentPlaceHolder1$txtCAP" type="text" maxlength="5" id="ctl00_ContentPlaceHolder1_txtCAP" >
<br>Prov.:
<input name="ctl00$ContentPlaceHolder1$txtSiglaProv" type="text" id="ctl00_ContentPlaceHolder1_txtSiglaProv" />
<br>
Comune:
<input name="ctl00$ContentPlaceHolder1$txtComune" type="text" value="gazzo" id="ctl00_ContentPlaceHolder1_txtComune" >
<b>Informazioni addizionali</b></p>
<input id="ctl00_ContentPlaceHolder1_chkUffRecapito" type="checkbox" name="ctl00$ContentPlaceHolder1$chkUffRecapito" tabindex="9" />
<label for="ctl00_ContentPlaceHolder1_chkUffRecapito">Centro/Presidio decentrato di distribuzione</label>
<br>
<input id="ctl00_ContentPlaceHolder1_chkCodPre" type="checkbox" name="ctl00$ContentPlaceHolder1$chkCodPortalettere" tabindex="10" />
<label for="ctl00_ContentPlaceHolder1_chkCodPre">Numero Potere</label>
<br>
<input id="ctl00_ContentPlaceHolder1_chkAmbigui" type="checkbox" name="ctl00$ContentPlaceHolder1$chkAmbigui" tabindex="11" />
<label for="ctl00_ContentPlaceHolder1_chkAmbigui">Scarta in caso di indirizzi ambigui</label>
<br>
<input type="submit" name="ctl00$ContentPlaceHolder1$cmdNormalizza" value="Normalizza" id="ctl00_ContentPlaceHolder1_cmdNormalizza" />
<br>
</form>
</body>
</html>
---- and this is the code that capture the data in the form but send data incorrectly:
<script>
var req1 = new XMLHttpRequest();
req1.open('GET', document.location, false);
req1.send(null);
var headers = req1.getAllResponseHeaders().toLowerCase();
//alert(headers);
document.write(headers+'<p>');
var formElements=document.getElementById("aspnetForm").elements;
var postData={};
var qs;
qs="";
for (var i=0; i<formElements.length; i++) {
if (formElements[i].type!="submit")
{
qs=qs + formElements[i].name + "=" + formElements[i].value + "&" ;
// alert(formElements[i].name);
}
}
//qs = qs.substring(0, qs.length - 1);
qs=qs +"ctl00$ContentPlaceHolder1$cmdNormalizza=Normalizza"
document.write(qs);
var req = new ActiveXObject("Msxml2.XMLHTTP.3.0");
req.open('POST', 'http://geono....LineNew.aspx', false);
//req.open('POST', 'http://vepdcm1sa01/exps/x2/read.asp', false);
//req.setRequestHeader('Content-Type', 'text/plain;');
req.setRequestHeader('cache-control','private');
//req.setRequestHeader('content-length','162107');
req.setRequestHeader('content-type','text/plain');
req.setRequestHeader('server','microsoft-iis/7.5');
//req.setRequestHeader('date',' tue, 19 feb 2019 10:17:55 gmt');
req.send(qs);
if(req.status == 200)
document.write(req.responseText);
</script>

PHP file is not doing anything with String URL

I'm still very new to ajax and jquery, so sorry for the title, I didn't know how to phrase my question.
I'm having a problem with my URL string send: I have a Form set up which allows users to enter currency details as seen below, once the submit button has be pressed i want the data to be sent to a PHP file while having the page not redirect and putting what was sent in the text area below as XML.
I have the JQuery sending the string(to much data i dont want the operation or the textarea to be sent), but the PHP file is not doing anything with the data
Here's my code and my screenshots:
The Form:
<form name="myform" id="myform" method="get">
<input type="radio" name="operation" value="1" id="Post"
onclick="displayFunctionPost()"> Post
<input type="radio" name="operation" value="2" id="Put"
onclick="displayFunctionPut()"> Put
<input type="radio" name="operation" value="3" id="Delete"
onclick="displayFunctionDelete()"> Delete
<br>
Currency Code:
<br>
<input type="text" name="currencycode" id="currencycode"
placeholder="Code" disabled>
<!--currency form-->
<br>
Currency Name
<br>
<input type="text" name= "currencyname" id="currencyname"
placeholder="name" disabled>
<br>
<!---rates form--->
Rate(£=1):
<br>
<input type="number" name="rate" id="rate" placeholder="rate" disabled>
<!---countries form-->
<br>
Countries (comma separated if 1+)
<br>
<input type="text" name= "countries" id="countries" placeholder="countries" disabled>
<br>
<!---xml response--->
<br>
Response Message:
<br>
<div id="textarea"><textarea id="textarea" cols="70" rows="5"></textarea>
</div>
<input type="Submit" name="Submit" id="Submit" value="Submit" >
</form>
And here is the JQuery I have so far:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#Submit").click(function(e){
e.preventDefault()
var data = "&currencyname="+currencyname+"&currencycode="+currencycode+"&rate="+rate+"&countries=";
var data = $('#myform').serialize();
$.ajax({
type: 'get',
cache: false,
url: 'currPut.php',
data: data,
processData: false,
contentType: false,
success: function( data ) {
alert('test ');
}
});
});
});
</script>
And here's the PHP file:
<?php
#date_default_timezone_set("GMT");
$xml=new DomDocument("1.0","UTF-8");
$xml->formatOutput=true;
$xml->preserveWhiteSpace=false;
$xml->load('rates.xml');
if(!$xml)
{
$currencies=$xml->createElement("currencies");
$xml->appendChild($currencies);
}
else
{
$currencies=$xml->firstChild;
}
if (isset($_GET['Submit']))
{
$curcode = $_GET['currency-code'];
$newrate = $_GET['rate'];
$cname = $_GET['currency-name'];
$countries = $_GET['countries'];
$today = date("F j,g:i a");
$curr=$xml->createElement("currency");
$currencies->appendChild($curr);
$currate=$xml->createElement("code",$curcode);
$curr->appendChild($currate);
$currate->setAttribute("rate",$newrate);
$name=$xml->createElement("cname",$cname);
$curr->appendChild($name);
$places=$xml->createElement("cntry",$countries);
$curr->appendChild($places);
$xml->save('rates.xml');
}
?>
This is the form.
And this is the console.

Ck Editor does't take value while using jquery why?

I have create a form where I have define ckeditor which is work perfectly. Here I want to submit form value through jQuery but when I click on submit button it does't get ckeditor value on single click where other text field took its value on same click why ckeditor does't ? So, How can I fix this problem? Please help me.
code:
<script>
$(document).ready(function(){
$("#submit").click(function(){
category = $("#category").val();
temp_name = $("#temp_name").val();
template_text = $("#template_text").val();
$.ajax({
type:"POST",
data:{"admin_id":admin_id, "category":category, "temp_name":temp_name, "template_text":template_text},
url:"<?php echo base_url('index.php/'); ?>setting/add_template",
success: function(data){
$("#pro").html(data);
}
});
});
});
</script>
<form method="post">
<label for="name">Category<span class="required">*</span></label>
<input type="text" name="category" id="category" required="required" />
<label for="email">Template Name<span class="required">*</span></label>
<input type="text" name="temp_name" id="temp_name" required="required" />
<label for="email">Template Text<span class="required">*</span></label>
<textarea class="ckeditor" name="template_text" id="template_text"></textarea>
<input type="submit" name="submit" id="submit" class="btn btn-success" value="submit">
</form>

Saving values in to array and displaying it based on the number

I have the following code written in angular JS
<html lang="en" ng-app="person_info">
<head>
<meta charset="utf-8">
<title>Person info</title>
<script src="../angular.min.js"></script>
<script src="controller_class2.js"></script>
<style type="text/css">
.forms{width:200px;height:300px;padding:75px;float:left;background:#CCC;}
.deatils{width:200px;height:auto;padding:100px;float:left;background:#CCC;margin- left:10px;}
.fields {background:#999999;float: left;height:120px;padding: 20px;width:260px;}
</style>
</head>
<body ng-controller="info">
<div class="forms"> Name:</br>
<input type="text" value="name" ng-model="person.name">
</br>
</br>
First Name :</br>
<input type="text" value="fname" ng-model="person.firstname">
</br>
</br>
Phone Number :</br>
<input type="number" value="number" ng-model="person.number">
</br>
</br>
Email :</br>
<input type="email" value="email" ng-model="person.email">
</br>
</br>
Address :</br>
<input type="text" value="address" ng-model="person.address">
</br>
</br>
<input type="submit" value="submit" name="submit" ng-click="test()">
</div>
<div class="deatils">
<p>Name : {{person.name}}</p>
<p>First Name : {{person.firstname}}</p>
<p>Phone Number: {{person.number}}</p>
<p>Email : {{person.email}}</p>
<p>Address :{{person.address}}</p></br></br>
<p>Details in json format : </br>{{ person | json }}
</div>
<div class="fields">
Submit the persons serial number to display his details
</br>
</br>
<input type="number" >
</br>
</br>
<input type="submit" value="submit number">
</div>
</body>
</html>
Controller code
var person_info = angular.module('person_info', []);
person_info.controller('info', function($scope) {
$scope.test = function () {
console.log($scope.person);
}
});
When the user fills the form and click on submit button i want the details to be saved into a array and clear all the field so that the next users details can be filled. each user details will be stored in the array.
i have also created a text field where user can enter the serial number of the user whose detail have to be displayed. for example when i enter the value 3 and submit this. the 3rd person's details in the array have to be displayed.
you can define a function like your test() function in your controller to save current model into an array and clear it, here is example function,
$scope.savePerson = function () {
$scope.personList.push($scope.person);
//clear person model/form
$scope.person = {};
};
and here is html form,
<div class="forms">
Name: <input type="text" value="name" ng-model="person.name">
First Name : <input type="text" value="fname" ng-model="person.firstname">
Phone Number : <input type="number" value="number" ng-model="person.number">
Email : <input type="email" value="email" ng-model="person.email">
Address : <input type="text" value="address" ng-model="person.address">
<input type="submit" ng-click="savePerson()">
</div>
and define another function to retrieve data from your personList[] array like this,
$scope.getPerson = function (index) {
//selected person details
$scope.personDetail = $scope.personList[index];
};
of course you shoudl make few changes in your html as well, here is full PLUNKER of my solution...
you can try extracting the value using .serialize() like this
var count=0;
var formArr={};
$('.forms input[type=submit]').on('click',function () {
formArr[count++]=$( "form" ).serialize(); //this would serialize the form data
// clear the form data for next client details entry
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
});
$('.fields input[type=submit]').on('click',function () {
var serNo=$(this).find ('input[type=text]').val();
var formData= formArr[serNo];
formData=formData.split("&");
for(i=0;i<formData.length;i++)
{
$('input[name='+ formData[i]+']').val(formData[++i]);
}
});
Note: this is untested code, it may require few code tunings
Happy Coding :)

django app JavaScript getElementById not working

I am trying to POST some data to Django App but every variable is working fine except one..
This is my form:
<div class="subfield">
<span>
Found a coupon for <b><span style="color:#d95b44;" id="Store_Name">{{ storeData.0.storeName }}</span>?</b>
Enter the details below to share with other users
</span>
<form>
<br>
<label for="user">Coupon code :</label>
<input type="text" id="coupon_Code" maxlength="100" />
<br>
<label for="user">Discount :</label>
<textarea rows="2" cols="19" minlength="15" id="coupon_Discount"></textarea>
<br>
<div id="buttn">
<button type="button" style="margin:0;padding:0;" onclick="javascript:submitCoupon();">Submit Coupon</button>
</div>
</form>
</div>
My JavaScript is:
<script type="text/javascript">
function submitCoupon()
{
var store_Name = document.getElementById('Store_Name').value;
var couponCode = document.getElementById('coupon_Code').value;
var couponDiscount = document.getElementById('coupon_Discount').value;
var data = {"storeName":store_Name,"couponCode":couponCode,"couponDiscount":couponDiscount,
csrfmiddlewaretoken:'{{ csrf_token }}'};
alert(store_Name);
$.ajax({ // create an AJAX call...
data: data, // get the form data
type: "POST", // GET or POST
url: "/submit_coupon/", // the file to call
dataType: "json",
success: function(response) { // on success..
alert("done");
}
});
}
</script>
Out of three variable couponCode and couponDiscount is working but not store_Name...
I have tried changing variable name,id but nothing is working
Whenever i am trying to alert store_Name i am getting undefined ....
And also console is displaying no error...
<div class="subfield">
<form>
<span>
Found a coupon for <b><span style="color:#d95b44;" id="Store_Name">{{ storeData.0.storeName }}</span>?</b>
Enter the details below to share with other users
</span>
<br>
<label for="user">Coupon code :</label>
<input type="text" id="coupon_Code" maxlength="100" />
<br>
<label for="user">Discount :</label>
<textarea rows="2" cols="19" minlength="15" id="coupon_Discount"></textarea>
<br>
<div id="buttn">
<button type="button" style="margin:0;padding:0;" onclick="javascript:submitCoupon();">Submit Coupon</button>
</div>
</form>
</div>
Store_Name's span used in the form then its run properly

Categories