Javascript event not working - javascript

I have this php code below
<?php foreach($items in $item) : ?>
<a class="btn">$item</a>
<?php endforeach; ?>
and this javascript below
$('.btn').click(function() {
console.log('hello');
});
it should work when I click the <a> tag but it's not working.
So, help me please. (Sorry for bad English)

foreach($items in $item) this is wrong. php foreach uses as not in
http://php.net/manual/en/control-structures.foreach.php

Use either
<?php foreach($items as $item) :
echo "<a class='btn'>".$item."</a>"
endforeach; ?>
or
<?php foreach($items as $item):?>
<a class='btn'><?php echo $item>?></a>"
<?php endforeach; ?>
$item must be within <?php ... ?> or it's meaningless to php

Did you add jQuery library link into it? because you used "$".
Just declare jquery library before your script. It should works
https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
p/s: also fix php syntax as Ramanlfc mentioned above

Related

How do I add a php variable to a onclick function argument?

Beginner JS here.
I am trying to add a PHP variable to a Javascript onclick function. I converted the PHP variable to a JS variable just fine. However, when adding the JS variable to the function I'm not receiving the desired output. What am I doing wrong?
<script>
js_logo_number = "<?php echo $logo_number; ?>";
</script>
<img src='<?php echo $image1[0]; ?>' onclick="openModal();currentSlide(js_logo_number)">
You can do like this :
<img src='<?php echo $image1[0]; ?>' onclick="openModal();currentSlide(<?php echo $logo_number; ?>)">
This code works for me. Be sure you use a let or const (or the outdated var) prefix to declare js_logo_number a new variable. Also make sure that $logo_number is set like this:
<script>
let js_logo_number = <?php isset($logo_number)?$logo_number:null; ?>;
</script>
Then make sure you convert it to an integer in Javascript, if that's how you want to use it:
function currentSlide(num){
console.log('currentSlide fired', parseInt(num));
}
let js_logo_number = "5";
function openModal(){
console.log('openModal fired');
}
function currentSlide(num){
console.log('currentSlide fired', parseInt(num));
}
<img src='https://placekitten.com/200/200' onclick="openModal();currentSlide(js_logo_number)">
When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.
See: PHP tags
1) echo
<?php echo 'if you want to serve PHP code in XHTML or XML documents, use these tags'; ?>
Solution:
<img src='<?php echo $image_src ?>'
onclick="openModal();currentSlide(<?php echo $logo_number ?>)">
2) Short echo
You can use the short echo tag to <?= 'print this string' ?>.
It's equivalent to <?php echo 'print this string' ?>.
Solution:
<img src='<?= $image_src ?>'
onclick="openModal();currentSlide(<?= $logo_number ?>)">
3) If short_open_tag is enabled
<? echo 'this code is within short tags, but will only work '
.'if short_open_tag is enabled'; ?>
Solution:
<img src='<? echo $image_src ?>'
onclick="openModal();currentSlide(<? echo $logo_number ?>)">
See: PHP tags

Cannot echo .load function

For some reason this code doesn't work. Adding \ to the front of the $ doesn't change anything. Can anyone see the problem?
$commentid = 2; //for example
echo "<script>";
echo "<div id = 'commentinput".$commentid."'>";
echo "</div>";
echo "$('#commentinput".$commentid."').load('editcomments2.php')";
echo "</script>";
Do it like this:
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';
I'd suggest not using echo to output HTML (in most cases anyway), but closing the PHP tag instead.
This becomes (edited with a loop example, see comments below):
<?php while ($commentid = $dbObject->dbMethod()): ?>
<div id="commentinput<?= $commentid ?>">
</div>
<script>
$('#commentinput<?= $commentid ?>').load('editcomments2.php');
</script>
<?php endwhile ?>
Note that in this case, the <script> portion should probably be outside of the loop, with a jQuery selector matching all the comment inputs.
So besides all of the other answers, I found another solution to!
When echoing out js code, like
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';
//and
echo "alert('stuff')";
it might then print it out like: $("#commentinput2").load("editcomments2.php")alert('stuff') and the code will get confused. So lastly I need to added a semicolon to prevent the program from reading all my code as just 1 line:
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';

How I create a Read More button inside a table?

<?php $counter = 0 ?>
<table>
<?php foreach($attributes as $a): ?>
<tr>
<td><?php $a['name']?></td>
<td><?php $a['Price']?></td>
</tr>
<?php $counter++;
if ($counter > 3) ?> <!-- What to do here? -->
<?php endforeach; ?>
</table>
In a PHP Block like this, I would like to got a Read More button after three tr elements, and when I click the button show all the rest of elements...
Something to do with PHP?
No, it has nothing to do with php. You need JavaScript to implement this if you don't want to reload the whole page.
In PHP you could print the short and the long version into separate divs and then use the following JS-Code on the View-More-Button:
document.getElementById("shortId").style.display = "none";
document.getElementById("longId").style.display = "block";

function is not working if I put this into js folder

I have a add.ctp file, where I can add a multiple rows, and if I put addNumber function to app/webroot/js, then its not working, why?
My view file (add.ctp)
<table id="mytable">
<tr id="number0" style="display:none;">
<td><?php echo $this->Form->button(' - ',array('type'=>'button','title'=>'Click Here to remove this number')); ?></td>
<td><?php echo $this->Form->input('lisanumbrid', array ('name'=>'data[Kontaktid][lisanumbrid][0]')) ?></td>
</tr>
<tr id="trAdd"><td> <?php echo $this->Form->button('+',array('type'=>'button','title'=>'Click Here to add another number','onclick'=>'addNumber()')); ?> </td><td></td><td></td><td></td><td></td></tr>
</table>
<?php
echo $this->Form->end('Lisa');
?>
Adding multiple rows function(addNumber.js) is here app/webroot/js:
var lastRow=0;
function addNumber() {
$("#mytable tbody>tr#number0").clone(true).attr('id','lisanumbrid'+lastRow).removeAttr('style').insertBefore("#mytable tbody>tr#trAdd");
$("#lisanumbrid"+lastRow+" button").attr('onclick','removeNumber('+lastRow+')');
$("#lisanumbrid"+lastRow+" input:first").attr('numbrid','data[Lisanumbrid]['+lastRow+'][lisanumbrid]').attr({'id':'numbridlisaNumber'+lastRow,'name': 'data[Kontaktid][lisanumbrid]['+ lastRow +']'});
lastRow++;
}
function removeNumber(x) {
$("#lisanumbrid"+x).remove();
}
Jquery file is in the same folder like a function, and its echoed in C:\wamp\www\cakephp-2.5.6\app\View\Layouts\default.ctp
echo $this->Html->script('jquery-2.1.1.min.js');
If I put this addNumber function into add.ctp file and put into add.ctp
<?php echo $this->Html->script(array('jquery-2.1.1.min.js'));?>
then its working nice, put I dont want these into add.ctp file.
Thanks for helping !
Solution
Added <?php echo $this->Html->script('addNumber'); ?> in to add.ctp file, after </table>
Are you calling addNumber.js in your add.ctp?
You could call this js file in app\View\Layouts\default.ctp and then
echo $this->Html->script(addNumber.js);
or you can create a script tag inside add.ctp calling it:
$.getScript('/epedidos/app/webroot/js/addNumber.js');
By the way, check in your console on firebug (pressing F12 on your keyboard) if the error calling this function is not defined.
You have to add all javascript's files used in your view in the working order using script method of Html object in your header section or at the end of your ctp file.
For example:
I will suppose that you have not added jquery and your addnumber before, so at the end of your ctp file:
<?php echo $this->Html->script(array('jquery','addNumber')); ?>
Regard the order, i.e jquery must be before addNumber and both of them must be in webroot/js/

<?= tags not working in codeigniter version 2.1.0

I am using codeigniter version 2.1.0 the <?= ?> tags not working,
I have applied the tags like this <?=$company['name']?> and i tried <?= $company['name']?> and <?php $company['name']?> none of these are working
The short_open_tag configuration directive is probably set to false on your server.
You also forgot the echo in your last call, it should read:
<?php echo($company['name']); ?>
Since the echo shorthand only works for the short tag, i.e. <?= ... ?> and not for <?php= ... ?>

Categories