php & javascript open window function - php

I try to use window.open but so far it doesn't work
<script type="text" language="javascript">
function win1(j){
window.open("ctry.php?j=" + j,"Window1","menubar=no,width=460,height=360,toolbar=no");
}
</script>
<?php
for($j = 1; $j <= 2; $j++){
?>
<a href="javascript:win1('<?php echo $j;?>')"
onMouseOver="self.status='Open A Window'; return true;"><b>Open Window</b></a>
<?php
}
?>
When I click on the link nothing happened and on the console i get this error:
Uncaught ReferenceError: win1 is not defined
Do you know what can i do?
Thanks

use this
<script type="text/javascript" language="javascript">
function win1(j){
window.open("ctry.php?j=" + j,"Window1","menubar=no,width=460,height=360,toolbar=no");
}
</script>
<?php
for($j = 1; $j <= 2; $j++){
?>
<b>Open Window</b>
<?php
}
?>

Replace <script type="text" language="javascript"> with <script type="text/javascript" language="javascript">

Are you putting this code inside body tag or inside head? Cause you cannot put html in head

Your JS action is to be on onclick action, not on href, try this elow code
<a href="#" onclick="win1('<?php echo $j;?>')"
onMouseOver="self.status='Open A Window'; return true;"><b>Open Window</b></a>

Related

How do I remove javascript tag 'type="text/javascrip"' on html code in PHP

I wish to remove 'type="text/javascrip" inside this code :
<img id="banlink" class="w-100" src="block_images_9.jpg" alt="Homepage Banner"/>
<script type="text/javascript" src="epto.min.js"></script>
<script>
$(window).ready( function() {
var m = document.createElement("div");
....
How to make it in PHP ?
Thanks for your help.
Your question is very unclear, I do not fully understand what you want to do. Please elaborate.
You can have javascript inside of php as follows:
<?php
echo '<script>
$(window).ready( function() {
var m = document.createElement("div");
....
</script>';
?>
OR
<?php
echo '<script type="text/javascript" src="epto.min.js"></script>';
?>
You can also reference your javascript files as
<script src="epto.min.js"></script>
without the type="text/javascript" as stated in your question.

php jquery-load more data inside a loop

I have the posts inside a while and post replays inside another while
I want to add a load more link or button for replays.
I have this codes but just first load more works:
<style> #myList li{ display:none;list-style: none;}</style>
<?php $conn=mysqli_connect("localhost","root","","dbace");
$psql="SELECT * FROM tbl_users_posts limit 20";
$qry=mysqli_query($conn,$psql);?>
<ul id="myList">
<?php while($fql=mysqli_fetch_assoc($qry)){
$pst=$fql['post'];
$pid=$fql['id'];
echo $pid." ".$pst;?><br>
<?php $rpql="SELECT * FROM re_pst WHERE subid='$pid'";
$qrep=mysqli_query($conn,$rpql);
while($qrw=mysqli_fetch_assoc($qrep)){
$remsg=$qrw['remsg'];?>
<li>
<?php echo $remsg;?><br>
<?php }?><br><br>
</li>
<div id="loadMore">load more</div>
<?php }?>
</ul>
and the jquery:
<script type="text/javascript" src="_js/jqmin.js"></script>
<script type="text/javascript">
$(document).ready(function () {
size_li = $("#myList li").size();
x=3;
$('#myList li:lt('+x+')').show();
$('#loadMore').click(function () {
x= (x+2 <= size_li) ? x+2 : size_li;
$('#myList li:lt('+x+')').show();
});
});
</script>
How can I fix that?
Thanks
Can I do it somehow by give number to posts and jquery command? some thing like this:
for ($i = 0; $i < X; $i++) {
Your problem is to you using id attribute for event. The value of id attribute must be unique. Instead of you can use class attribute.
The following code may be solution:
$('.loadMore').on('click', function(){
$(this).parent('li').show();
})
Can you try using "loadMore" button by class attribute with above code?

php for loop and jQuery show/hide method to display number of divs correlated to number of buttons

I've created some kind of loop with PHP and want to be able to display the number of divs based on the number of buttons pressed.
<?php for ($x = 1; $x <= 6; $x++) { ?>
<button type="button">
<span><?php echo $x; ?></span>
</button>
<?php } ?>
This outputs 6 buttons, then I want to output div:
<?php for ($x = 1; $x <= 6; $x++) { ?>
<div class="some_div"><?php echo $x; ?></div>
<?php } ?>
Thanks!
Its simple in jquery.
Steps to follow :
1) First you downnlaod jquery latest version library file
2) Include the file in head tag on the top
3) Next copy and paste the below code
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
jquery
$(function() {
$('.some_div').on('click', function() {
console.log($(this).text()); // get the number you clicked
$('.some_div').show();
$(this).hide();
});
});
You won't be able to do this purely with PHP, as you're intending. PHP executes on the server, so by the time the user is clicking buttons, PHP is done executing.
You'll need to use JavaScript for this (or jQuery, as you have the question tagged).
To do this in jQuery, you can do something like this:
<?php for ($x = 1; $x <= 6; $x++): ?>
<button type="button">
<span><?= $x ?></span>
</button>
<?php endfor; ?>
<div id="container">
</div>
<script type="text/javascript">
$('button').click(function() {
var selected = !$(this).data('selected');
$(this).data('selected', selected);
if (selected) {
$('#container').append('<div class="some_div"></div>');
} else {
$('#container').find('.some_div').last().remove();
}
});
</script>
Or, if your buttons are tied directly to a specific div:
<?php for ($x = 1; $x <= 6; $x++): ?>
<button type="button" data-index="<?= $x ?>">
<span><?= $x ?></span>
</button>
<?php endfor; ?>
<?php for ($x = 1; $x <= 6; $x++): ?>
<div class="some_div" id="someDiv<?= $x ?>"></div>
<?php endfor; ?>
<script type="text/javascript">
$('button').click(function() {
var selected = !$(this).data('selected');
$(this).data('selected', selected);
var index = $(this).data('index');
$('#someDiv' + index).toggle(selected);
});
</script>

use dynamic div on animated collapse

I'm using the animated collapse JS library here:
http://www.dynamicdrive.com/dynamicindex17/animatedcollapse.htm
and i'm trying to use it for dynamic div's however it's not toggling. Any ideas?
<head>
<script type="text/javascript" src="includes/js/animatedcollapse.js"></script>
</head>
<body>
<?PHP
for ($i = 1; $i <= 5; $i++) { ?>
<script type="text/javascript">
animatedcollapse.addDiv('location-<?PHP echo $i; ?>', 'fade=1')
</script>
<div id="location-<?PHP echo $i; ?>">
CLOSE
TEST
</div>
TOGGLE
<?PHP } ?>
<script type="text/javascript">
animatedcollapse.ontoggle=function($, divobj, state){}
animatedcollapse.init()
</script>
I may be wrong, but it doesn't seem necessary to use PHP just to create an incrementing variable.
You could just write that same for loop in javascript, inside your script tags: I also think using jQuery's slideToggle might make this easier for you... Maybe something along these lines:
for (var i = 1, i <= 5, i++ ){
$(document).ready(function(){
$('divToBeToggled' + i).click(function(){
$('divToBeRevealed' + i).slideToggle('slow');
});
});
}
Just looking quickly at the link you supplied, doesn't this script depend on jQuery also being present? You don't seem to have that script tag in your head tag.

php echo not working for strtotime function while assigning it to a javascript variable

This works fine
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
var timeLeft = <?php echo '1279'; ?>;
$("#time").text(timeLeft);
});
</script>
</head>
<body>
<div id="time"></div>
</body>
</html>
but this doesn't
<script>
$(document).ready(function(){
var timeLeft = <?php echo strtotime("now"); ?>;
$("#time").text(timeLeft);
});
</script>
It gives error Uncaught SyntaxError: Unexpected token < .
It is a php extension file.
Try this:
<script>
$(document).ready(function(){
var timeLeft = '<?php echo strtotime("now"); ?>';
$("#time").text(timeLeft);
});
</script>
Directly using php in javascript is very dangeorous. Because timetostr may output some warnings about timezone etc and your code will be broken without any visual clue.
In my projects, If I have to use any php output in my javascript, I create hidden divs and access the value from javascript:
<div class='hidden' id='values'>
<div class='val-time'><?php echo strtotime("now"); ?></div>
</div>
<script>
$(document).ready(function(){
var timeLeft = $("#values .val-time").html();
$("#time").text(timeLeft);
});
</script>
So whenever you want to debug your code, simply remove "hidden" class from #values and look at which values are ouputted by PHP.

Categories