Actually I'm trying to redirect to the index after logging in with ajax button
but what is happening is strange , header is not taking me to the index.php !
i just get the HTML code of the index.php in my login.php page !!
I'm using Smarty to separate the html from php code , and I don't know how to fix the problem !
click the login and get the HTML of the index in login page !
why ?
in login.php page
if ($_POST['action'] == 'login') {
$_SESSION['username'] = $username;
header("Location: index.php");
exit();
}
the ajax function :
function ajax3(Div_Submit,Div_Response,SuccessScript,Data_Submit)
{
var Type = 1;
var Aj_type = "POST";
var post_data = '';
if(!Data_Submit)
post_data = $('#'+Div_Submit+' *').serialize();
else
post_data = Data_Submit;
$.ajax({
type: Aj_type,
url: document.URL,
data: post_data,
dataType: "text",
beforeSend: function(xhr)
{
$.blockUI({ message: '<img src="images/loading.gif" />',
css: { border: 'none', backgroundColor: 'transparent' } });
xhr.setRequestHeader("Ajax-Request", "true");
},
success: function(response) {
$('#'+Div_Response).html(response+'<script>'+SuccessScript+'</script>');
},
complete: function(){
$.unblockUI();
}
});
return false;
}
the html button call :
<div class="buttons" >
<a class="positive" onclick="$('#login_action').val('login');ajax3('login_form','login_form','','')">
<img src="images/checkbox_checked.png" alt=""/>
<font size="3px"> login </font>
</a>
</div>
That wouldn't work as what you expected.
It should be something like this.
if ($_POST['action'] == 'login') {
$_SESSION['username'] = $username;
echo "success";
}
On your javascript/jquery file.
have something like this
jQuery.ajax({
url: "login.php",
success:function(data){
if(data == "success") window.location = "index.php";
}
});
With AJAX, this is the expected output. What happens is that the AJAX request itself is being redirected to index.php, which I what is then outputted and returned to that request.
If you want to redirect to index.php from AJAX, you would need to exit with a specific code, or a JSON-encoded value and catch that in the AJAX result itself.
Example:
if ($_POST['action'] == 'login') {
$_SESSION['username'] = $username;
echo json_encode(array('redirect' => 'index.php'));
exit;
}
JavaScript:
$.ajax(
url: 'your-url',
success: function(data) {
if (data.redirect) {
document.location.href = data.redirect;
}
}
);
u can get success response whenever login credentials are appropriate , at that moment change the window location using javascript or jquery..
Related
i have problem with redirect page and query update. If i clicked button, there is an windows confirmation, if i click yes, i call ajax to send data to my php page, and my php page do a query. After success, i want redirect to another page. This is my html code
$(document).on("click", "#tombolvalidasi", function() {
var pil = <?php echo $_GET['pilih']; ?>;
var r = confirm("Validasi Pesanan?");
if (r == true)
{
$.ajax({
type: "GET",
url: host+'/skripsi3/web/validasipesan.php',
data: { "id": pil},
success: function (data){
if (data == "success") {
window.location = "pesanvalid.php?pilih="+pil;
}
},
error: function (e) {
alert('Fail');
}
});
}
else
{
}
});
and this is my php code
<?php
session_start();
include "connectdb.php";
$idacara = mysql_real_escape_string($_GET["id"]);
$cons = "Accepted";
$query="UPDATE `preorder` SET `statuspesanan`='$cons' WHERE `id_acara`='$idacara'";
$result = mysql_query($query);
echo "success";
?>
Hope someone can tell me what's wrong with my code. Thank You Guys and
Have a nice day!! Cheers!!
I want to create jquery ajax success function based resp from logout.php. If users not sign in,It redirect bring them to login page.,but it's not working as I'm expected,no event occur when user is logged or not.So, what is wrong with my ajax function?
logout.php
<?php
if( !$user->is_logged ) {
echo 'true';
}else{
echo 'false';
}
?>
jquery function in index.html
$(function(){
$.ajax({
type: 'GET',
url: "logout.php",
success: function(resp){
if(resp =='true'){
document.location = '../login_test.html';
}
if(resp=='false'){
alert('U are logged');
}
}
});
});
Change some errors and made it to better view:
PHP:
<?php
header('Content-Type: application/json');
if( !$user->is_logged ) {
echo json_encode(array('status' => 'ok'));
}else{
echo json_encode(array('status' => 'bad'));
}
?>
Javascript:
$(function(){
$.ajax({
type: 'GET',
url: "logout.php",
success: function(resp){
var state = JSON.parse(resp).status
if(state == 'ok'){
document.location.href = '/login_test.html';
}
else{
alert('U are logged');
}
}
});
});
If you have no alert, and have no redirect - you have not .success callback, and problem one level above your condition. In that case show us your errors from js-dev-console from browser.
i want to logout on clicking this button
html input
<input type="button" value="logout" id="top-btn-logout" onclick="test()">
my js file
function test()
{
$("#top-btn-logout").click(function() {
$.ajax({
url: 'http://localhost/New%20folder/vigcheck.php?action=logout',
success: function(data){
alert(data);
//location.reload();
//window.location.href = data;
}
});
});
}
check.php file.
<?php
if(isset($_GET['action']) == "logout" ){
//$_SESSION = array();
session_destroy();
session_unset();
echo "logout";exit;
}?>
First use a proper get request using jquery ajax try
$.ajax({
url: 'http://localhost/New%20folder/vigcheck.php',
type: 'get',
data:{action:'logout'},
success: function(data){
alert(data);
//location.reload();
//window.location.href = data;
}
});
then check.php file. (you are combining conditions to check use both conditions separate)
<?php
if(isset($_GET['action']) && $_GET['action'] == "logout" ){
//$_SESSION = array();
session_destroy();
session_unset();
echo "logout";exit;
}?>
if(isset($_GET['action']) == "logout" ){
This is incorrect because isset will return true and true != 'logout' so make it as
if(isset($_GET['action'])){
if($_GET['action']=="logout";
//do whatever you wanna do
}
You need to check for returned data from check.php file on success of ajax request . After that redirect the user to your login page.
Try this it will work :
JS File :
function test()
{
$("#top-btn-logout").click(function() {
$.ajax({
url: 'http://localhost/New%20folder/vigcheck.php?action=logout',
success: function(data){
alert(data);
//location.reload();
//window.location.href = data;
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (XMLHttpRequest.status === 401) {
location.href = 'index.php';
}
}
});
});
}
check.php :
<?php
if(isset($_GET['action']) == "logout" ){
header("HTTP/1.1 401 Unauthorized");
}
?>
I have all my webpages loaded with ajax, I'm not sure how to include POST data from the loaded page. The goal is to have a registration form and post that data to another php file and have it processed there.
ajaxloader.js
$(document).ready(function(){
checkURL();
$('ul li a').click(function (e){
checkURL(this.hash);
});
setInterval("checkURL()",250);
loadPage('#Home');
});
var lasturl="";
function checkURL(hash)
{
if(!hash) hash=window.location.hash;
if(hash != lasturl)
{
lasturl=hash;
loadPage(hash);
}
}
function loadPage(url)
{
url=url.replace('#','');
$.ajax({
type: "POST",
url: "load_page.php",
data: 'page='+url,
success: function(msg){
if(parseInt(msg)!=0)
{
$('#pageContent').html(msg);
}
}
});
}
load_page.php
if(!$_POST['page']) die("0");
$page = $_POST['page'];
if(file_exists($page.'.php'))
echo include($page.'.php');
else echo 'There is no such page!';
?>
Any ideas?
So your load_page.php woudl look like
if(!$_POST['page']) die("0");
$page = $_POST['page'];
ob_start();
if(file_exists($page.'.php'))
include($page.'.php');
else echo 'There is no such page!';
$content = ob_get_clean();
echo $content;
This should do the job. HOWEVER, you can't just pass the whole website like that, you need to base64_encode() it make sure everything is ok. And then take a look at this
http://phpjs.org/functions/base64_decode/
I am using this tutorial http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_slide_toggle
Here is the code without the style.
<script type="text/javascript">
$(document).ready(function()
{
$(".flip").click(function()
{
var panel = "open";
$(".panel").slideToggle("slow");
});
});
</script>
How would I go about saving the state of this so if I refreshed the page it would remain open or closed. I imagine a php session would be the correct way, but how do I write that in the javascript?
In JS:
var readWirite='write'; //or 'read'
$.ajax({
type: "POST",
url: "myPhpFile.php",
data: "panel="+panel+"&readWrite="+readWrite;
success: function(msg){
if(msg == '1'){
alert('Horay panel saved!');
} else {
$('#panelId').html(msg); //Write saved panel back to html
}
}
});
In myPhpFile.php:
<?php
if(!isset($_SESSION)) session_start();
if(isset($_POST['readWrite']) && isset($_POST['panel'])){
if($_POST['readWrite'] == 'write'){
$result = '0';
if($_SESSION['panel'] = $_POST['panel']) $result = '1';
echo $result;
} else if($_POST['readWrite'] == 'read') {
echo $_SESSION['panel'];
}
}
?>