redirect to page in php using jquery - php

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>";

Related

Passing sql string variable into javascript function

I am trying to pass a string from a query into a javascript function.
An integer will pass into the function but string will not.
echo "<a href='#' onclick='delete_game({$title});'
class='btn'>Delete</a>";
<script type='text/javascript'>
function delete_game(title){
var answer = confirm('Really?');
if(answer){
window.location = 'delete.php?id=' + title;
}
}
</script>
I expected the javascript function to be executed, but instead nothing happens.
Why don't you use ajax for this? As mentioned in comments mix PHP/JS isn't good.
In your HTML, you can do something like
I'm assuming that you are using Blade.
Delete Game
Then in your javascript, you do this using jQuery:
function deleteGame(title){
var answer = confirm('Really?');
if(answer){
$.ajax({
url : "your-php-file.php",
type : 'post',
data : {
title : title
}
})
.done(function(msg){
$("#result").html(msg);
})
.fail(function(error){
console.log(error);
});
}
}
In your PHP you process receiving the data from post $_POST
$title = $_POST['title'];
You can understand better the Ajax function of jQuery here.
A few things:
I would change window.location to window.location.href
Change your echo to:
echo "Delete";
Check if $title is set
var_dump($title);
If you'd like to make it a bit cleaner and are prepared to use jQuery:
Delete
<script type='text/javascript'>
$(document).on('click', '#delete', function () {
var title = $(this).data('title');
var answer = confirm('Really?');
if (answer){
window.location.href = 'delete.php?id=' + title;
}
});
</script>

Page Redirect using jquery

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

Use a PHP variable inside a jquery click()?

I have this,
$(document).ready(function(){
$('#link').click(function(){
var user_login = <?php $base_url; ?>;
window.location = user_login + '/login';
});
});
note that $base_url is global variable. This works in FF but not on Chrome and IE. Thanks.
You need to add quotes around the php tags to designate that the value of $base_url is a string:
$(document).ready(function(){
$('#link').click(function(){
var user_login = "<?php echo $base_url; ?>";
window.location = user_login + '/login';
});
});
That way, when the browser gets this block, it will look something like:
$(document).ready(function(){
$('#link').click(function(){
var user_login = "http://www.example.com"; // after php is executed
window.location = user_login + '/login';
});
});
You need to echo the variable, not just simply have it in a code block. And for safety, to prevent introducing JS syntax errors, you should json-encode the value:
var login = <?php echo json_encode($base_url); ?>;
See below, note the quotes around the PHP echo
$(document).ready(function(){
$('#link').click(function(){
var login = '<?php echo $base_url; ?>';
window.location = user_login + '/login';
});
});
I suppose its a PHP file, you need to surround your variable contents by quotes to javascript
$(document).ready(function(){
$('#link').click(function(){
var login = <?php '\''.$base_url.'\''; ?>;
window.location = user_login + '/login';
});
});
Run this script in a PHP file, not in a .js file, and don't forget tho ECHO your variable and add quotes (in javascript), as said in other answers !

Pass PHP Variable into Java Script window.location

I am trying to pass a php variable into a java script window.location that returns a user to the current list view after deleting an item from the database. I can't seem to get the syntax correct.
Code:
function confirmation(a) {
var currString = "<? echo $currString ?>";
var answer = confirm("Are you sure you want to delete this item?")
if (answer){
alert("The item has been deleted")
window.location = "list.php?s='. $currString .'&=delete=true&id=" + a;
}
else{
alert("The item has not been deleted")
}
Try this:
function confirmation(a) {
var currString = "<?php echo $currString ?>";
var answer = confirm("Are you sure you want to delete this item?");
if (answer){
alert("The item has been deleted")
window.location = "list.php?s=" + currString + "&=delete=true&id=" + a;
}
else{
alert("The item has not been deleted");
}
you are pasing php variable to JS variable
var currString = "";
and in window.location you are passing again php variable which is wrong,
so do it like this
window.location = "list.php?s=" + currString + "&=delete=true&id=" + a;
The syntax issue was solved by other answers, but you need to take care of an additional issue: URI encoding your variable when you use it in the URL:
window.location = "list.php?s="
+ encodeURIComponent(currString)
+ "&=delete=true&id=" + a;
or else you will run into problems of your variable contains characters like &.
echo "<script>alert('System info has been Save')</script>";
echo "<script>window.location='customer_detail.php?
customer_id=".$customer_id."'</script>";

JavaScript Redirection

Here is the code I have,
<script language="JavaScript">
var url = "http://localhost:8888/uploads/"+<? $name ?>+"/output.txt";
setTimeout("top.location.href = url",1000);
</script>
$name is a PHP variable which has the name of which directory I'm trying to redirect to.
Needless to say, it isn't working. What's wrong with it? I don't know a lot about javascript so I probably did something stupid
Thanks
If your mixing PHP with JavaScript, it's always advisable to check the output being sent to the browser: right click on your website and click view source!
JavaScript doesn't care whether the content being sent to it is static HTML, from a Database or generated by PHP. If its in the output, it'll parse it.
If you'd have done that, you'd notice that your not echo'ing the $name variable.
<script language="JavaScript">
var url = "http://localhost:8888/uploads/"+<? echo $name ?>+"/output.txt";
setTimeout("top.location.href = url",1000);
</script>
But that'd give you
<script language="JavaScript">
var url = "http://localhost:8888/uploads/"+ foo +"/output.txt";
setTimeout("top.location.href = url",1000);
</script>
Which isn't valid JavaScript, as foo is now a JS variable, not a string.
So you should have:
<script language="JavaScript">
var url = "http://localhost:8888/uploads/<? echo $name ?>/output.txt";
setTimeout("top.location.href = url",1000);
</script>
Furthermore, passing a string to setTimeout (or setInterval) is not recommend; for the same reasons against using eval(), so you should end up with something like this instead:
<script language="JavaScript">
var url = "http://localhost:8888/uploads/<? echo $name ?>/output.txt";
setTimeout(function () {
top.location.href = url
},1000);
</script>
Pass a function, not a string, to setTimeout.
var url = "http://localhost:8888/uploads/<?= $name ?>/output.txt";
setTimeout(function ()
{
top.location.href = url;
}, 1000);
I am assuming that the $name is properly filled in by PHP.
Try this:
setTimeout(function() { top.location.href = url; }, 1000);
Try like this:
var url = "http://localhost:8888/uploads/"+<? $name ?>+"/output.txt";
setTimeout(function() {
top.location.href = url;
}, 1000);
<?php echo "var url = 'http://localhost:8888/uploads/$name/output.txt'"; ?>
Remove pluses and braces - just put variable in url.
Also use Firebug to troubleshoot your JS code: http://getfirebug.com/
Use 'echo' or the slightly unreadable php short code <?= to echo out a variable.
<script language="JavaScript">
var url = "http://localhost:8888/uploads/<?php echo $name; ?>/output.txt";
setTimeout(function(){
top.location = url;
}, 1000);
</script>
I know I've created another closure, but feel it's more readable.
It should be:
window.location.href = url

Categories