How do I receive parameters from jsp from js? - javascript

I am looking for a way to receive parameters sent by .JS as .JSP.
The file receiving the parameter is JSP, but it is the script that must be used.
sendParameter.jsp
<form name="frm" action="adminGNB.jsp" method="POST" >
<div class="form-line">
<input type="text" class="form-control" name="admin_id" id="admin_id" onkeyup="enterkey()">
</div>
<div class="form-line">
<input type="password" class="form-control" name="admin_password" id="admin_password" onkeyup="enterkey()">
</div>
<div class="btn-wrap">
<button type="button" class="btn btn-block btn-primary" id="login" onClick="loginbutton()">login</button>
</div>
</form>
receive Parameter.jsp
<%
request.setCharacterEncoding("UTF-8");
String name = (String)request.getSession().getAttribute("admin_id");
System.out.println("admin_id :" + name);
%>
<script>
$(function(){
changeYourPassword();
latestNoticeCheck();
});
....
</script>
I'm using Java to receive parameters sent by JSP, as you can see by the code. But where I have to use the parameters in <script>. How can I get the parameters?
I don't use the GET method.
getParameter("admin_id") is don't work
Thanks in advance

this code should be working .....
<script>
$(function(){
var myVar = '<%= request.getSession().getAttribute("admin_id") %>';
changeYourPassword();
latestNoticeCheck();
});
....
</script>

Related

Submitting form on same loaded page if the page is loaded via ajax

So i have a PHP page that is loaded via AJAX using the .load method.
This is then displayed on my page, however i need to be able to submit the form that is located on my second page that im loading using .load, submit this form and process the data on the same page without it reloading.
I'm very new to javascript and AJAX so i've no idea if i'm evening doing this correct using .load()
Any help would be appreciated.
page 1 is the following:
<button onclick="Test()" type="button">Load Content</button>
<div class="box" id="box" name="box">
</div>
<script>
function Test(id) {
$( "#box" ).load( "test.php?id=" + id );
}
</script>
The second page which is test.php houses the following
<form id="enrolemployee" action="" method="post">
<div class="form-group">
<label>Test</label>
<input type="text" class="form-control" id="Test" name="Test" placeholder="Test">
</div>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<button type="submit" class="btn btn-outline-custom">Submit</button>
</form>
Now to submit the form on test.php i use a custom token class to generate a random hash per form submit so i can validate the data on the same page and process it through my database.
is this possible to do? or do i need to have my form then post the data through a different page?
Basically i need the entire thing to stay on PAGE 1, process the form like it normally should whilst the persons page does not reload and is always static on page 1, this should never redirect to test.php
Thankyou.
You can do it like
index.php
<button onclick="Test('10')" type="button">Load Content</button>
<div class="box" id="box" name="box">
</div>
<script src="//code.jquery.com/jquery-latest.js"></script>
<script>
function Test(id) {
$("#box").load("test.php?id=" + id);
}
</script>
test.php
<form id="enrolemployee" action="" method="post">
<div class="form-group">
<label>Test</label>
<input type="text" class="form-control" id="Test" name="Test" placeholder="Test">
</div>
<input type="hidden" name="token" value="">
<button type="submit" class="btn btn-outline-custom">Submit</button>
</form>
<script>
$("#enrolemployee").on('click', e => {
// Avoid reloading page
e.preventDefault();
console.log(<?php echo $_GET['id']; ?>);
})
</script>

Adding a request variable in a Django template

I am currently uploading a file to the server in Django as follows (For some unrelated reasons, I cannot use models for this form):
<div class="input-group" style="padding-top: 10px">
<div><input class="form-control" id="filename" value="Select a ZIP file to upload..." disabled></div>
<div class="input-group-btn"><input type="button" id="get_file" class="btn btn-primary" value="Browse"
style="margin-right: 3px;margin-left: 3px"></div>
<div class="input-group-btn"><input type="submit" id="upload"
class="btn btn-success" value="Upload" style="display: none"></div>
<input type="file" id="upload_file" name="upload_file" accept=".zip">
</div>
And then I some JavaScript as:
document.getElementById('get_file').onclick = function() {
document.getElementById('upload_file').click();
};
$('input[type=file]').change(function (e) {
document.getElementById('filename').value = ($(this).val());
document.getElementById('upload').style.display = "inline"
});
document.getElementById('upload').onclick = function() {
};
This works fine and I can upload the file just fine. However, now along with a file, I also want to send a string identifier. I am not sure how I can insert another request parameter on submit from this template?
The Django side looks like:
def upload(request):
"""
if request.method == 'POST' and request.FILES['upload_file']:
# Do things
So I need to add another parameter, so that I can do something like: request.GET.get('identifier') and this identifier key/value is inserted in the template code.
Does adding a hidden input inside the form work?
<input type="hidden" name="identifier" value="..."/>

My ajax code don't send the data to the database

I want to use ajax to add data in the database but nothing happens
i don't know whats wrong is it in the ajax!or in the php
This is part of my code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
$("#commentForm").submit(function(){
var U_N = $('input[name="U_N"]').val();
var I_name = $('input[name="I_name"]').val();
var context = $("#inputmessage").val();
$.ajax({
type: "POST",
cache: false,
url : "aAddComm_Menu.php",
data : {U_N:U_N,I_name:I_name,context:context},
});
});
});//end of document ready function
</script>
this the html form:
<form class="form-horizontal" role="form" id="commentForm" method="POST" action="">
<div class="form-group">
<label for="inputmessage" class="col-sm-2 control-label">comment</label>
<div class="col-sm-10">
<textarea name = "context" class="form-control" id="inputmessage" rows="3" placeholder="Enter your comment here . . "></textarea>
</div>
</div> <!-- End of /.form-group -->
<input type="hidden" value = "$Item['Item_Name']" name="I_name"/>
<input type="hidden" value ="$U_N" name="U_N"/>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button name="SendC" type="submit" class="btn btn-primary" ;>Send</button>
</div>
</div>
</form>
and this is a part of the php page for adding to the database
extract($_POST)
if(isset($context))
{
mysql_query("INSERT INTO menu_commnet VALUES('$I_name','$U_N',NULL,NOW(),'$context')");
}
var U_N = $("input#U_N").val();
var I_name = $("#I_name").val();
var context = $("#context").val();
The jQuery selectors are wrong, you're not selecting anything. The # selector stands for id attribute, but you're trying to select by value of name attribute. You should either add/change id attributes of your input fields, or use
var U_N = $("input[name=U_N]").val();
var I_name = $("input[name=I_name]").val();
var context = $("[name=context]").val();
Tip: next time use developer console inside your browser, you can easily test if selectors are correct. You can type in javascript code as you would inside .js file and test if it's working like you expect.
Also, don't do this in your PHP, use prepared statements.
Did you included you Database conection on your Backend script?
extract($_POST)
'if(isset($context)) {
mysql_query("INSERT INTO menu_commnet VALUES('$I_name','$U_N',NULL,NOW(),'$context')");
}'
i think here you should include you database info, 'include_once("database info.php"); , also make sure you ajax will return a result, and echo php errors, just to be sure its an ajax or php error,
I have few comment regarding your issue :
Replace $("document").ready(function(){ with
$(document).ready(function(){
Before extracting in PHP ,
try var_dump($_POST)
Based on your HTML code use:
var U_N = $('input[name="U_N"]').val();
var I_name = $('input[name="I_name"]').val();
var context = $("#inputmessage").val();
Here is final working example. Just add "ev" argument to submit callback function , then use ev.preventDefault() in it.
$(document).ready(function(){
$("#commentForm").submit(function(ev){
ev.preventDefault();
var U_N = $('input[name="U_N"]').val();
var I_name = $('input[name="I_name"]').val();
var context = $("#inputmessage").val();
$.ajax({
type: "POST",
cache: false,
url : "aAddComm_Menu.php",
data : {U_N:U_N,I_name:I_name,context:context}
});
});
});//end of document ready function
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form class="form-horizontal" role="form" id="commentForm" method="POST" action="">
<div class="form-group">
<label for="inputmessage" class="col-sm-2 control-label">comment</label>
<div class="col-sm-10">
<textarea name="context" class="form-control" id="inputmessage" rows="3"
placeholder="Enter your comment here . . "></textarea>
</div>
</div>
<input type="text" value="" name="I_name"/>
<input type="text" value="" name="U_N"/>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button name="SendC" type="submit" class="btn btn-primary">Send</button>
</div>
</div>
</form>

Razor form on a facebook tab page

I am trying to execute a form inside an Facebook tab. What have I tried among many things:
It seems like Request.HttpMethod == "POST" is not working as usual. #message prints out on pageload.
I have tried to add alert to before return $('form').valid(). This is not being executed.
Any ideas of issue or another workaround.
Code:
#{
var message = string.Empty;
if (Request.HttpMethod == "POST")
{
message = "SUBMITTED";
}
}
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<div class="form">
#if (!string.IsNullOrEmpty(message))
{
<p class="formFinish">
#message
</p>
}
<div class="row">
<label>Name</label>
<input type="text" name="Name" id="Name" class="required" minlength="4" maxlength="30" />
</div>
<div class="newGame row">
<button class="btn">Submit</button>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('form').attr('enctype', "multipart/form-data");
$('form').on('submit', function () {
return $('form').valid()
});
});
</script>
I might be missing something obvious, it doesn't seem that you're sending a post. You've got no actual form there, just a div with the class of "form", and your "Submit" button is just a button that doesn't appear to have any action associated with it. You're going to need to actually post something if you want the page to get a post request.
EDIT: Here's working code, I just had to change your "div class='form'" into an actual form element with method "post". Note that I didn't fix your usage of jQuery validate, but now that the form is actually generating a post request, you should be well on your way.
#{
var message = string.Empty;
if (Request.HttpMethod == "POST")
{
message = "SUBMITTED";
}
}
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<form method="post">
#if (!string.IsNullOrEmpty(message))
{
<p class="formFinish">
#message
</p>
}
<div class="row">
<label>Name</label>
<input type="text" name="Name" id="Name" class="required" minlength="4" maxlength="30" />
</div>
<div class="newGame row">
<input type="submit" class="btn" value="Submit" />
</div>
</form>

How to fill in form field, and submit, using javascript?

If I have an html document whose rough structure is
<html>
<head>
</head>
<body class="bodyclass" id="bodyid">
<div class="headerstuff">..stuff...</div>
<div class = "body">
<form action="http://example.com/login" id="login_form" method="post">
<div class="form_section">You can login here</div>
<div class="form_section">
<input xmlns="http://www.w3.org/1999/xhtml" class="text" id="username"
name="session[username_or_email]" tabindex="1" type="text" value="" />
</div>
<div class="form_section">etc</div>
<div xmlns="http://www.w3.org/1999/xhtml" class="buttons">
<button type="submit" class="" name="" id="go" tabindex="3">Go</button>
<button type="submit" class="" name="cancel"
id="cancel" tabindex="4">Cancel</button>
</div>
</form>
</div>
</body>
</html>
You can see that there is a username field and a Go button. How would I, using Javascript, fill in the username and press Go...?
I would prefer to use plain JS, rather than a library like jQuery.
document.getElementById('username').value="moo"
document.forms[0].submit()
document.getElementById('username').value = 'foo';
document.getElementById('login_form').submit();
You can try something like this:
<script type="text/javascript">
function simulateLogin(userName)
{
var userNameField = document.getElementById("username");
userNameField.value = userName;
var goButton = document.getElementById("go");
goButton.click();
}
simulateLogin("testUser");
</script>
It would be something like:
document.getElementById("username").value="Username";
document.forms[0].submit()
Or similar
edit: you guys are too fast ;)
This method helped me doing this task
document.forms['YourFormNameHere'].elements['NameofFormField'].value = "YourValue"
document.forms['YourFormNameHere'].submit();

Categories

Resources