Page Redirect using jquery - php

i am using jquery for login it is working fine but i have to redirect the login page on index.php page if success==1 else it will show the error. But it does not redirect on index page and remains on same login page.Can anyone please help me.This is my code
<script src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#form_login').submit(function() {
var uname= $('#username').val(); alert(uname);
var password=$('#password').val(); alert(password);
$.ajax({
type: "POST",
url: "http://localhost/shop/admin/match_login.php",
data: {uname: uname, password:password},
success:function(data){
if(data==1){ alert("right");
var url='http://localhost/shop/admin/index.php';
$(location).attr('href',url);
}else{ alert("wrong");
$('#login-msg').html("Wrong User Name/Password");
}
} //call function after success
});
return false;
});
});
</script>
session_start();
require_once("../utils/config.php");
require_once("../utils/functions.php");
require_once("../utils/dbClass.php");
require_once("../utils/database.php");
$objNEWDB = new NEWMySQL;
$host= $_SERVER['HTTP_HOST'];
$url=$_SERVER['SCRIPT_URI'];
$hostnew=substr($url,0,-16);
$user_name= $_POST['uname'];
$password= $_POST['password'];
$SQL = "Select * from admin where UserName ='".$user_name."' and Password='".$password."'";
$rsUser = $objNEWDB->select($SQL); //print_r($rsUser);
//$res=mysql_fetch_array($rsUser);
if(sizeof($rsUser)==1 && $rsUser[0]['Status']=='1')
{
$_SESSION["session_admID"] = $rsUser[0]['AdminID'];
}
echo $size= sizeof($rsUser);

try
document.location = url;.
or
document.location.href = url;

change
$(location).attr('href',url);
to
location = url

You can use simply window.location = "http://localhost/shop/admin/index.php";

// similar behavior as an HTTP redirect
window.location.replace("http://localhost/shop/admin/index.php");
// similar behavior as clicking on a link
window.location.href = "http://localhost/shop/admin/index.php";

You can try
if(data==1) { window.location = "Page Url"; }
OR
if(data==1) {window.location.href = "Page Url"; }
Also check whether the if loop is satisfied or not

Related

redirect to page in php using jquery

I am trying to redirect to same page in php using jquery as below:
echo "<script>
var url = window.location.href + '?a=eud';
alert(url);
$(location).attr('href',url);
</script>";
When I execute above, I am getting alert message, but its not redirecting.
Sorry if its a dumb query, but i am new to php and having basic knowledge on jquery.
You were almost there, you just have to get the PHP to echo the actual redirect command:
echo"
<script>
var url = window.location.href + '?a=eud';
location.href = url;
</script>";
You can use
window.location = url;
try this
echo "<script>
var url = window.location.href + '?a=eud';
window.location = url;
</script>";
echo"<script>
var url = window.location.href + '?a=eud';
window.location.replace(url);
</script>";

Retrieving page with anchor AJAX not working in IE

We released a site a couple of weeks ago and we are now getting complains that the website doesn't work in Internet Explorer 6-10.
Im using jQuery version: 1.11.0
We are running AJAX to load each page with an anchor. For example. www.site.com/#page2
Im trying to debug the code and see where it fails but I have no idea what is wrong.
The problem people are having is simple, the page doesn't load. All other code except the page call is working. So basically the page is empty.
This is a simplified version of the code:
function retrieve_page(link, lang){
event.preventDefault();
var id = link;
if(id){
document.location = "#"+id;
} else {
document.location = "#hem";
}
if(window.location.hash.substring(1) != "undefined") {
var page = window.location.hash.substring(1);
} else {
var page = "hem";
}
var content;
var request = $.ajax({
type: "GET",
url: "ajax/retriever.php",
data: { page:page },
success: function(data) {
content = data;
}
});
request.done( function () {
$('#page').html(content);
});
}
and this is the ajax/retriever.php file
<?php
$page = $_GET["page"];
$path = "../pages/".$page.".php";
$config = "../core/config.php";
if(!file_exists($path)){
require_once $config;
echo "<div id='no_page'><h1 style='display: block'>".$lang["ERROR"]."</h1><a href='index.php'><h2>".$lang["ERROR_RETURN"]."</h2></a></div>";
exit;
} else {
require_once $config;
require_once $path;
exit;
}
?>
The AJAX function call is example:
<a onclick="retrieve_page('page1', 'sv')">Page1</a>
Thanks in advance!
Can't see how i missed it. Well i solved the issue with simply removing event.preventDefault();

reading facebook token in url, storing in cookie, echoing in file

I have a multi -part process
step 1:on page.php I evoke a facebook pop-up authentication window with the URI going to page2.php
https://facebook.com/dialog/oauth?client_id=ID&redirect_uri=http://domain.com/page2.php?-Your+Special+Token+1170-&type=user_agent&fbconnect=1&scope=publish_stream
on page2.php I want to process this token by reading it from the url and storing it as a cookie
<?php
session_start();
if ( empty($tkn) ) { ?>
<script>
(function () {
try {
var q = location.href.split('#');
var a = q[1];
var q2 = a.split('=');
var a2 = q2[1];
var q3 = a2.split('&');
var a3 = q3[0];
setTimeout(function () {
top.location.replace('http://domain.com/cookie.php?tkn=' + a3);
}, 200);
} catch (e) {
top.location.replace('http://domain.com/cookie.php?retry=1&tk=broken_' + encodeURI(e.message));
}
})()
</script><? } else {
print $_GET['tkn'];
}
$_SESSION['tkn'] = $tkn;
$tkn = $_POST["tkn"];
if(isset($tkn)) {
setcookie("tkn", $tkn);
echo "success";
}
?>
page.php detects the cookie and echoes out the token $tkn and reloads the page
<?php
session_start();
?>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$.post('page2.php',{tkn: tkn}, function(data){
if(data=='success'){
location.reload(true)
}
});
</script>
<?php echo $tkn; ?>
the problem is I cant get any of this to work. The popup goes into an infinite error loop and nothing is refreshed or echoed.

Strange Ajax/jQuery/PHP login issue

I have a dynamic login header. 2 links, login / register and profile / logout.
I have a php class function that was being used to check if logged in and displaying relevant links, it worked fine.
I then moved to an ajax login as I didn't want a page refresh and the login box drops down and rolls back up. Again, it works fine.
I've noticed a slight issue, by slight I mean very irritating :)
Once logged in, Every single page refresh on new page shows a flicker where 'profile' becomes 'login' and then flickers back again. It only happens when the page is loading and doesn't last long but it's not very nice.
Could someone help me solve it please? I'm pretty new to Ajax/jQuery and spent ages wiht the help of some guys in here getting the ajax/jquery part functional in the first place.
this is script that toggles the login divs
<script>
window.onload = function(){
$(function() {
var loggedIn = <?php echo json_encode($general->loggedIn()); ?>;
$("#loggedIn").toggle(loggedIn);
$("#loggedOut").toggle(!loggedIn);
});
}
</script>
Thanks
EDIT: Ajax
function validLogin(){
$('#error').hide();
var username = $('#username').val();
var password = $('#password').val();
if(username == ""){
$('input#username').focus();
return false;
}
if(password == ""){
$('input#password').focus();
return false;
}
var params = {username: username, password: password};
var url = "../loginProcessAjax.php";
$("#statusLogin").show();
$.ajax({
type: 'POST',
url: url,
data: params,
dataType: 'json',
beforeSend: function() {
document.getElementById("statusLogin").innerHTML= '<img src="../images/loginLoading.gif" /> checking...' ;
},
success: function(data) {
$("#statusLogin").hide();
if(data.success == true){
$('#loggedIn').show();
$('#loginContent').slideToggle();
$('#loggedOut').hide();
}else{
// alert("data.message... " + data.message);//undefined
$("#error").show().html(data.message);
}
},
error: function( error ) {
console.log(error);
}
});
}
Use PHP to hide the unwanted element by doing the following
<?php
$loggedIn = $general->loggedIn();
?>
... Some HTML
<div>
<div id="loggedIn" <?php echo ( $loggedIn ? '' : 'style="display: none;"' ); ?>>
.... Logged in stuff
</div>
<div id="loggedOut" <?php echo ( !$loggedIn ? '' : 'style="display: none;"' ); ?>>
.... Logged Out Stuff
</div>
</div>
<script>
var loggedIn = <?php echo json_encode($loggedIn); ?>;
$('#loginForm').submit(function() {
... Handle form submit
... When ajax returns true or false we can set loggedIn and then toggle the containers
});
</script>
// CSS-Stylesheet
#loggedIn,
#loggedOut {display: none}
<script>
$(document).ready(function() {
var loggedIn = <?php echo json_encode($general->loggedIn()); ?>;
if (loggedIn == true) { // i can just guess here...
$("#loggedIn").show();
}
else {
$("#loggedOut").show();
}
});
</script>
Three possible solutions:
If the script element is placed inside the body, move
it to head element.
Use the following script instead:
$(document).ready(function () {
'use strict';
var loggedIn = <?php echo json_encode($general->loggedIn()); ?>;
$('#loggedIn').toggle(loggedIn);
$('#loggedOut').toggle(!loggedIn);
});
Hide both links in the "logged in" div using $('#loggedIn
a).hide(); and then, show them on the window.onload event using
$('#loggedIn a).show();. A bit dirty, bit it may work.

Two functions (Javascript and AJAX) dont work together

Im trying to send some data to the server through AJAX with the value i get from a JS variable.
Code:
<script type="text/javascript">
var url;
function aplicarFoto(_src) {
url = _src;
var fotosel = document.getElementById("fotosel");
fotosel.src = 'fotos/'+_src;
}
function guardarCambios() {
$.post("guardarCambios.php",
{url: url},
function(response) {
alert(response);
if (response == "NoUsuario") {
window.location = "../login.php";
} else {
alert("correcto");
}
}
alert(url);
}
</script>
The idea is update the user picture with the url i get from aplicarFoto(_src) with the variable url .
The first function (aplicarFoto(_src)) alone works correctly, but when i put the another function (guardarCambios()), the first function doesnt work, therefore the second neither! I dont know why, but it just happens when using ajax functions because i did a test with an alert(url) (sunrrounding the rest of code with comments) in the second function and both work correctly!
Some guess? Thank you!
Your script alone has syntax errors.
<script type="text/javascript">
var url;
function aplicarFoto(_src) {
url = _src;
var fotosel = document.getElementById("fotosel");
fotosel.src = 'fotos/' + _src;
}
function guardarCambios() {
$.post("guardarCambios.php", {
url: url
}, function (response) {
alert(response);
if (response == "NoUsuario") {
window.location = "../login.php";
} else {
alert("correcto");
}
alert(url);
}
);
}
</script>

Categories