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 !
Related
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>
With my code I can change my text by click:
$('.SeeMore').click(function(){
var $this = $(this);
$this.toggleClass('SeeMore');
if($this.hasClass('SeeMore')){
$this.text('+ more');
} else {
$this.text('- less');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="SeeMore" style="cursor:pointer">+ more</li>
This is working well so far. But because I have different language versions of my page I want to exchange the words with php:
<script>
$('.SeeMore').click(function(){
var more = <?php echo $lang['MORE']; ?>
var less = <?php echo $lang['LESS']; ?>
var $this = $(this);
$this.toggleClass('SeeMore');
if($this.hasClass('SeeMore')){
$this.text(more);
} else {
$this.text(less);
}
});</script>
Unfortunately now my code is not working anymore and I do not know why.
There are two things that is missed by you-
1. Semi-colon at the end of the statement.
2. Double / single quotation around the PHP statement inside the jQuery.
You need to use ", Ex: var more = "<?php echo $lang['MORE']; ?>";
You forgot the semicolons and quotes:
var more = "<?php echo $lang['MORE']; ?>";
var less = "<?php echo $lang['LESS']; ?>";
writing code-generating-code is hard and can be confusing. Better use a decent template system (e.g. Smarty).
You are forgetting the quotes:
var more = "<?php echo $lang['MORE']; ?>";
var less = "<?php echo $lang['LESS']; ?>";
Use data attributes
html:
<li class="SeeMore" data-less="<?php echo $lang['LESS']; ?>" data-more="<?php echo $lang['MORE']; ?>" style="cursor:pointer">+ more</li>
js:
$('.SeeMore').click(function(){
var more = $(this).attr('data-more');
var less = $(this).attr('data-less');
var $this = $(this);
$this.toggleClass('SeeMore');
if($this.hasClass('SeeMore')){
$this.text(more);
} else {
$this.text(less);
}
});
PS: don't forget to wrap you click event in a document ready statement
You can use your php variables inside jquery with the following code:
token = "<?php echo $_SESSION['token']; ?>"
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>";
How can I declare a global variable in a PHP file and access directly in jQuery?
HTML
<script type="text/javascript">
var date_selected = <?php echo $from; ?>;
</script>
JS
$(function() {
alert("dsdsadasdas" + date_selected);
var date = date_selected;
var startDate;
var endDate;
});
Because on my test its not working.
You are missing the quotes
var date_selected = '<?php echo $a; ?>';
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