Form not getting submitted using jquery - javascript

I am trying to validate user using oAuth 2.0 which works fine and am obtaining user information from google using jquery which I want to send to the next page. So to do that, I am making form programmatically using jQuery and submitting using same. But unfortunately, form is not getting submitted. I have seen various answers on stack-overflow and have tried to implement using that things but none of them is working.
Here is my code..
customButtonRender.js
function onSuccess(googleUser){
console.log('Logged in as '+googleUser.getBasicProfile().getName());
var profile = googleUser.getBasicProfile();
submit_form(profile);
}
var submit_form = function(profile){
console.log(profile.getId());
console.log(profile.getName());
console.log(profile.getImageUrl());
console.log(profile.getEmail());
var user_url = "localhost:8080/NUConnect_JSP/jsp/home.jsp";
var val_form = $('<form data-ajax="false" method="POST" id="val_form" action="' + user_url + '">'+
'<input type="text" name="user_name" value="' + profile.getName() + '">'+
'<input type="text" name="user_email" value="' + profile.getEmail() + '">'+
'<input type="submit" name="user_submit" id="user_submit">'+
'</form>');
$('body').append(val_form);
// $("#val_form").submit();
// $("#val_form")[0].submit();
// $("#user_submit").click();
document.getElementById("val_form").submit();
console.log("After submit");
};
I have included customButtonRender.js file in head section.
As you can see I have tried various methods to submit form written in comments but none of them worked.
All the logs are printed successfully.

Do one thing if your data is not so confidential then pass that information in your url like this
window.location.href = "localhost:8080/NUConnect_JSP/jsp/home.jsp?user_name="+profile.getName()+"&user_email="+ profile.getEmail();

Related

Remove parameters before sending FORM POST

I have page with one form made in C# on the page there are several HTML elements. On button click I am using jQuery and add hidden fields than I submit form to external domain. Now the issue is, that it is sending all the parameters which I don't need. Is there any way that I can send only parameter that I need. I want to send parameter only in body.
Is there way to clear form parameter before adding parameter
jQuery Code
$("#aspnetForm").attr("action", "www.example.com");
$("#aspnetForm").attr("method", "post");
var params = a.split('?')[1].split('&'); /*custom string with key/value and sperated with & */
$.each(params, function (index) {
var paramsV = params[index].split('=');
$("#aspnetForm").append('<input type="hidden" name="' + paramsV[0] + '" value="' + paramsV[1] + '" /> ');
});
$("#aspnetForm").submit();
There more than 50 HTML element are added dynamically in my form and at each load it have different id. So I cannot disable those element in Jquery or javascript
I am using SharePoint 2013, it have its own master page where it add lot of elements which I don't have control, so I cannot individually disable them.
What I did is I created dynamically form add required parameter and then submit it.
Hope it can help somebody
$("#frm").remove(); /** remove extra frm before creating it **/
var frm = $('<form id="frm" action="' + _url + '" method="POST"></form>');
var params = a.split('?')[1].split('&'); /*custom string with key/value and sperated with & */
$.each(params, function (index) {
var paramsV = params[index].split('=');
frm.append('<input type="hidden" name="' + paramsV[0] + '" value="' + paramsV[1] + '" /> ');
});
frm.appendTo(document.body).submit();

submit to a spring controller from a javascript code

I am trying to submit a form to a controller from a javascript but the form button is not showing.
This is my attempt
for (var i = 0; i < json.length; i++) {
users += '<strong>' + json[i].email + '</strong><br/>' + '<br/>' + json[i].tok + '<hr><br/>';
//trying to hit to the controller from here but it is not working
//trying to hit to the controller from here but it is not working
'<form method="post" action="searchAdmin" novalidate="novalidate">'
'<input type="email" value="json[i].email" name="searchName" id="searchName"/>'
//on clicking the fetch button let it submit to the controller
'<input type="submit" value="FETCH" />'
'</form>'
Kindly assist!
user+='<form met....
because you have used ; on the first line so you need to append this string
Could you try making the form a single string? Try like below;
//trying to hit to the controller from here but it is not working
'<form method="post" action="searchAdmin" novalidate="novalidate"><input type="email" value="json[i].email" name="searchName" id="searchName"/><button type="submit">FETCH</button></form>'

Cancel a form to be submitted, do not change the data of the entity

I have a div
<div id="updateChoice%s" class="text-center"></div>
which shows a the following upon a "updateText" button is clicked
function updateText(urlsafe) {
document.getElementById("updateChoice"+urlsafe).innerHTML +=
'<br><br><form id="dynForm" action="/updateText" method="post" >' +
'<textarea name="newContent" rows="5" cols="80"></textarea>' +
'<input type="hidden" name="update" value=' + urlsafe + '>' + '<br><br>' +
'<input class="choice" type="submit" value="Submit"></input> <button class="choice" onclick="cancelUpdate('+ urlsafe +')">Cancel</button>' +
'</form>';
}
I want to cancel the updating of the text but it doesn't work unless "newContent" is empty, here is /updateText file and cancelUpdate(urlsafe)
/updateText:
class UpdateText(webapp2.RequestHandler):
def post(self):
greeting_key = ndb.Key(urlsafe=self.request.get('update'))
event = greeting_key.get();
if(self.request.get('newContent') != ''):
event.content = self.request.get('newContent');
event.put();
self.redirect('/events');
cancelUpdate(urlsafe):
function cancelUpdate(urlsafe) {
document.getElementByName("newContent").innerHTML="";
document.getElementById("dynForm").submit();
}
any ideas?
The /updateText handler remains on not updating the text if upon submission it is empty, so do not change anything:
In the cancelUpdate(urlsafe) function, use this aproach:
function cancelUpdate(urlsafe) {
document.getElementById("updateChoice"+urlsafe).innerHTML = "";
}
Add \' to the parameter urlsafe as you pass it and not ". So here is correct javascript function call to div#updateChoice%s is:
function updateText(urlsafe) {
document.getElementById("updateChoice"+urlsafe).innerHTML =
'<br><br><form id="dynForm" action="/updateText" method="post" >' +
'<textarea name="newContent" rows="5" cols="80"></textarea>' +
'<input type="hidden" name="update" value=' + urlsafe + '>' + '<br><br>' +
'<input class="choice" type="submit" value="Submit"></input><button type="button" class="choice" onClick="cancelUpdate(\''+ urlsafe +'\')">Cancel</button>' +
'</form>';
}
Your question is very difficult to understand (What do you mean by "cancel the updating of the text"? What did you expect to happen? What exactly "doesn't work"?).
Also, there are multiple issues with the code you provided (none of which have to do anything with GAE, Python or webapp2), here are a few that might be helpful to resolve your issue:
1) Your cancelUpdate function accepts a urlsafe parameter but is not using it.
2) In your cancelUpdate function you're using a non-existing function getElementByName, you probably meant to use its plural version getElementsByName()
3)
On the same document.getElementByName("newContent").innerHTML=""; line you are retreiving an element with the name newContent which is a <textarea> element and are attempting to empty its innerHTML property, but a <textarea> doesn't have one. If you want to empty a <textarea>'s contents - you need to do document.getElementByName("newContent").value=""; instead.
Since your cancelUpdate function has an unused parameter urlsafe and although it's not clear what your issue is but since in your updateText function you are creating an element with id="updateChoice"+urlsafe and assuming that when you hit cancel - you also want to destroy that form, then your cancelUpdate would look as simple as this:
function cancelUpdate(urlsafe) {
document.getElementById("updateChoice"+urlsafe).innerHTML = "";
}
4)
Your "Cancel" button doesn't have a type specified which means it will default to submit even when you don't want to submit the form so you need to add type="button" to prevent the submission.
You are calling document.getElementById("dynForm").submit(); in your cancelUpdate function which in the combination with the previous issue will submit your form twice.

JSF param POST parameter not retrieving

I've been looking an answer, but I don't find anything.
I got a JSF page with a button. This button call a JavaScript function :
function postAdd(id) {
var quant = document.getElementById("hell:quantite");
$('<form action="addto.xhtml" method="POST">' +
'<input type="hidden" name="productkey" value="' + id + '">' +
'<input type="hidden" name="howmany" value="' + quant.value + '">' +
'</form>').submit();
}
This works fine, I've tested it.
But in my addto.xhtml page, when I call param.productkey and param.howmany, I got nothing.
I used this method on other page and works fine but here, it doesn't work...
Any idea ?
Thanks.
The answer was to add to form inside the JSF page directly and get all parameters with #{function(param[form:idOfInput]}

Is this is the right way of storing cookie in javascript

login form code snippet from html file
<form action="/phpScripts/login.php" method="post" onsubmit="getCookie()">
User/E-mail <input id="loginname" type="text" name="uid" class="inpun"><br>
Password <input id="loginpass" type="text" name="pass" class="inppass"><br>
<input type="submit" name="login" value="login" ><br>
</form>
getCookie function code from js file
function getCookie()
{
var user=document.getElementById('loginname').value;
var expires=new Date();
expires.setFullYear(expires.getFullYear()+1);
document.cookie=encodeURIComponent("name")+ "="+encodeURIComponent(user)+ "; expires=" + expires.toTimeString()+ "; domain=.xyz.com";
alert(document.cookie);
}
login.php (form action) do the database query stuff and checks for authentication and if all is well it stores session variables(username in the login form) and redirect to the home page. I want to store username in javascript cookies also so that i can load user specific data like users profile pic from the folders created in the root document directory at the time of registering (signup) for example:-
$(document).ready(function(){
var usr=document.cookie;
/*location=url(data/abc/abc.jpg)*/
location="url(data/" + usr + "/" + usr +".jpg" + ")";
$(div#profilepic).css({'background-image':location, 'width':'180px', 'height': '180px'});
});
please correct me if I am wrong in the jquery code above. Also is there any way to store php $_SESSION data in javascript variable?

Categories