Creating an Authentication Platform, POST between JQuery? - php

Okay, so this is a proof of concept project that I want to do. I have one page that is checking for the authentication status though some sort of POST response in JQuery. Here's that code:
$('#submit').click(function(){
window.open('http://authorize.openauth.tk/','title', 'width=360, height=240');
return false;
$.ajax({
type: POST,
url: "http://authorize.openauth.tk/",
success: function(data){
if(data == '1'){
$('h1').fadeToggle(function(){
$('#authStat').css('color', 'green').html("Authenticated!");
$('h1').fadeToggle();
});
}
else{
$('h1').fadeToggle(function(){
$('#authStat').css('color', 'red').html("Not authenticated.");
$('h1').fadeToggle();
});
}}
});
});
When I click Login, it opens the login window, and that it. In Firebug, I don't see a POST request to "authorize.openauth.tk". Now, I know that the above code is NOT doing what I described. What code should I use? My main goal: Click a button, open a connection to a page, open that page in a separate window, and then having the page in the window report back to the main page.
Here's the code for "the page in the window":
var referrer = document.referrer;
$('#submit').click(function(){
$.ajax({
type: 'POST',
url: "auth.php",
data: $('form').serialize(),
success: function(data) {
if(data == "0"){
$('h1').html("Wrong Username/Password");
}
else{
$.post(referrer, data);
}
}
});
alert(referrer);
});

Related

Laravel redirection through ajax request from blade to controller

I have a blade file which allows a user to delete certain items by sending an ajax request to the controller.
If all validation on the controller end passes and the item is successfully deleted, I would like for the user to be redirected back to the same page with the page being refreshed however the page isn't being refreshed and nothing seems to be happening
Blade file
$.ajax({
type: "post",
url: "{{env('APP_URL')}}/ticket-dashboard/updateTicket",
dataType:'json',
data: {"option":option, "status":status,"ticket_id":manual_ticket_id,'completed_id':'{{$user}}',"latest_ticket_log_id":latest_ticket_log_id,_token: '{{csrf_token()}}'},
success: function (data) {
console.log('-------');
console.log(data);
if(data['updated']){
alert("The selected task was updated and page has to be refreshed before attempting to apply action to ticket again");
}
else {
//Page should have reloaded on controller side
}
}
})
Controller file
if($validationPasses){
return redirect()->route('ticket_dashboard');
}
if($validationPasses){
return response->json(["status" => "redirect", "url" => "ticket_dashboard"]);
}
/////// javascript ////////////
$.ajax({
type: "post",
url: "{{env('APP_URL')}}/ticket-dashboard/updateTicket",
dataType:'json',
data: {"option":option, "status":status,"ticket_id":manual_ticket_id,'completed_id':'{{$user}}',"latest_ticket_log_id":latest_ticket_log_id,_token: '{{csrf_token()}}'},
success: function (data) {
console.log('-------');
console.log(data);
if(data['updated']){
alert("The selected task was updated and page has to be refreshed before attempting to apply action to ticket again");
}
else {
if(data.status === "redirect"){
window.location.href = data.url;
}
}
}
})
You need tp response to ajax then you can redirect.

Keeping a window toggled open to display and view error messages AJAX, JQuery, PHP

I am having an issue trying to keep a cretin window toggled opened after AJAX retrieves data, to show a proper message to denote weather or not the user has logged in or not.
My Javascript is as follows:
function validLogin(){
var username=$('#username').val();
var password=$('#password').val();
var dataString = 'username='+ username + '&password='+ password;
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="images/loading.gif" width="32px" height="32px" />');
$.ajax({
type: "POST",
url: "login.php",
data: dataString,
cache: true,
success: function(result){
var result=trim(result);
$("#flash").hide();
if(result=='correct'){
$("fieldset#signin_menu").show(); // <-- This will not work!
}else{
$("#errorMessage").html(result);
}
}
After I get the informatoin the window should stay open; however, the page refreshes and the window closes. This is an onclick event once you press "Sign In". A window will pop down. Here is the code for that window popping down:
$(document).ready(function() {
$(".signin").click(function(e) {
e.preventDefault();
$("fieldset#signin_menu").toggle();
$(".signin").toggleClass("menu-open");
});
$("fieldset#signin_menu").mouseup(function() {
return false
});
$(document).mouseup(function(e) {
if($(e.target).parent("a.signin").length==0) {
$(".signin").removeClass("menu-open");
$("fieldset#signin_menu").hide();
}
});
Is there a better work around for this? Am I doing this wrong?
use this jquery instead
$(document).ready(function() {
$("fieldset#signin_menu").show(function() {
$(".signin").toggleClass("menu-open");
});
});
$("fieldset#signin_menu").mouseup(function() {
return false
});
$(document).mouseup(function(e) {
if($(e.target).parent("a.signin").length==0) {
$(".signin").removeClass("menu-open");
$("fieldset#signin_menu").hide();
}
});

Get only part of the message and reload only one div

this is an ajax method that inserts the data into a db and should supposedly display the new content.
<script type = "text/javascript">
$(document).ready(function() {
$('#submit').live('click', function(eve) {
eve.preventDefault() ;
var form_data = {
title: $('#title').val()
};
$.ajax({
url: "http://localhost/ci/index.php/chat/comment",
type: 'POST',
data: form_data,
success: function(msg) {
alert(msg);
}
});
});
});
</script>
However in my /chat/comment, i am loading the view again, i.e, user submits a comment, load the view again and the comment should be there. My response from server is the view's HTML. However the view comes with all the divs and there are many of them. I need to retrieve only part of the div, say, #commentspace from the ajax on success.
Look at the jQuery $.load() function?
Example
Inside "firstpage.html"
$('#content').load('secondpage.html #content');

Why doesn't this jQuery AJAX work?

I don't understand why this form wont send.
The coding is very long and I don't think it would be good idea to post all of it here.
Link to form here
Link to Script here
I just tried loading the PHP file in browser and it wouldn't work. I will save it as a .txt file so you can view it. It will be # ......multiform/post.txt
On top of that, how would I send the entire form instead of stating all the values for the 'data' bit:
$.ajax({
type: 'POST',
url: 'post.php',
data: {subject:options.subject, name:$(this_id_prefix+'#adycustlname').val(), email:$(this_id_prefix+'#email').val(), message:$(this_id_prefix+'#zip').val()},
success: function(data){
Also ignore the success: function(data){ section, this form is going to be put in a slide panel so that is for what the slider should do after. I have a feeling it is wrong, but
the main issue is to get the form to send.
EDIT
$('#submit_seventh').click(function(){
//send information to server
$.ajax({
type: 'POST',
url: 'post.php',
data: $('#bob').serialize(),
success: function(data){
$(this_id_prefix+'#loading').css({display:'none'});
if( data == 'success') {
$(this_id_prefix+'#callback').show().append(options.recievedMsg);
if(options.hideOnSubmit == true) {
//hide the tab after successful submition if requested
$(this_id_prefix+'#contactForm').animate({dummy:1}, 2000).animate({"marginLeft": "-=450px"}, "slow");
$(this_id_prefix+'div#contactable_inner').animate({dummy:1}, 2000).animate({"marginLeft": "-=447px"}, "slow").animate({"marginLeft": "+=5px"}, "fast");
$(this_id_prefix+'#overlay').css({display: 'none'});
}
} else {
$(this_id_prefix+'#callback').show().append(options.notRecievedMsg);
setTimeout(function(){
$(this_id_prefix+'.holder').show();
$(this_id_prefix+'#callback').hide().html('');
},2000);
}
},
error:function(){
$(this_id_prefix+'#loading').css({display:'none'});
$(this_id_prefix+'#callback').show().append(options.notRecievedMsg);
}
});
alert('Data sent');
});
} else return false;
alert('Fail');
});
^^^^^ Why wont the above code work? ^^^^^
At the end of your script you have to replace
$('#submit_fourth').click(function(){
//send information to server
alert('Data sent');
});
with the correct URL to send to.

Forms/PHP/Ajax load?

I'm currently learning PHP. I've made a simple script # http://hash.techho.me, only thing is, I want the form to submit then load the results via AJAX, without the user leaving the page. Possible?
post the form using ajax
$.ajax({
url:'yoururl',
data:$("form").serialize(),
type:'POST',
success:function(data){
alert("success");
},
error:function(jxhr){
alert(jxhr.responseText);
}
});
jQuery.ajax() – jQuery API
Posting to the same page should do the trick. No need to use ajax for that
> <?php
>
> //do stuff with $_POST
> ?>
>
> <html> <body> <form method="post">
>
> <?php echo $result ?>
>
> </form>
> </body>
Fike
use ajax for this, lets suppose try this one for your practice
var string = $("#string").val();
var dataString = 'string=' + string ;
if(string==''){
alert('enter any string');
}
else{
$.ajax({
type: "POST",
url: "path of php file",
data: dataString,
suceess: function(){
//do something
},
error: function(){
//do something
}
});
}
You can use jQuery or Prototype JS libraries to make an easy AJAX call. Example using jQuery would be:
$.ajax({
url:'hashed.php',
data:$("form").serialize(),
type:'POST',
success: function(data){
$('hashmd5').html(data.md5);
$('hashsha1').html(data.sha1);
},
error: function(jxhr){
alert(jxhr.responseText);
}
});
Don't use the same id value in HTML, never ever. They must be unique to correct perform JavaScript functions on elements.
yes it is possible. Write a javascript function that would trigger on submit, disable the submit button so user couldn't press it again, and finally request the server via ajax. on successful response update the content. Something like following in Jquery
$('.form-submit').click(function(event)) {
event.preventDefault();
if(form is valid and not empty) {
$.ajax({
type: "POST",
url: "path to script that will handle insetion",
data: "data from form", //like ({username : $('#username').val()}),
suceess: function(data){
//update the content or what. data is the response got from server. you can also do like this to show feedback etc...
$('.feedback').html("Data has been saved successfully");
},
error: function(){
$('.feedback').html("Data couldn't be saved");
}
});
}
}

Categories