Run jQuery function in PHP Function File - php

How to add jQuery function in PHP function File?
Example :
<script type="text/javascript" src="assets/js/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/jquery.livequery.js"></script>
<script type="text/javascript" src="assets/js/jquery.timeago.js"></script>
<?php
class db{
public $db;
function get_news()
{
bla bla
}
}
When run the code, the jQuery is not working. Please help.
Thank you

You can't run jQuery function in PHP. You can run them from HTML. First close php:
?>
<script>
function get_news() { blabla; }
</script>
<?php
If this is not what you're looking for then there is no way for you to do what you're looking for.

This is a sample code,
<script type="text/javascript" src="assets/js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
alert("Test");
});
</script>
<?php
echo "PHP code here";
?>

You can Echo jQuery function call to run it
<?php
echo "<script type=\"text/javascript\">
$(document).ready(function() {
alert(\"Test\");
});
</script>";
?>

Related

jQuery load with php $_GET didn't work

i want to send Get when the link clicked to receive the result in php code
and this what happened
this in php file working perfect
<html>
<a class="cc" href="#">click me</a>
<script src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".cc").click(function () {
$(".body").load('page.php ')
})});
</script>
<section class="body"></section>
</html>
but when i put GET to the href didn't work just Flashing content
<html>
<a class="cc" href="?id=1">click me</a>
<script src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".cc").click(function () {
$(".body").load('page.php ')
})});
</script>
<section class="body"></section>
</html>
In the second case, you really need to stop the <a> from following the link.
$(document).ready(function() {
$(".cc").click(function(e) {
e.preventDefault();
$(".body").load('page.php');
});
});

how to call a jQuery function in php

if($this->db->insert(table,$insert_array))
{?>
<script type="text/javascript">
$(function() {
$('#div1').show().delay(15000).fadeOut('fast');
})
</script>
<?php
}
I want to call this function $('#div1').show().delay(15000).fadeOut('fast'); if my if statement is true. Kindly help
Try like that:
if($this->db->insert(TBL_CAR,$insert_sql_array))
{?>
<script type="text/javascript">
$(function() {
$('#div1').show().delay(15000).fadeOut('fast');
})
</script>
<? } ?>
I will advise you write something like this:
<?php if($this->db->insert(TBL_CAR,$insert_sql_array)) :?>
<script type="text/javascript">
$(function() {
$('#div1').show().delay(15000).fadeOut('fast');
})
</script>
<?php endif ;?>
This should work perfectly well for you. I have implemented something of such on several ocassion
Try this
<?php
if($this->db->insert(TBL_CAR,$insert_sql_array)) {
?>
<script type="text/javascript">
jQuery('#div1').show().delay(15000).fadeOut('fast');
</script>
<?php
}
?>

Pass JQuery variables to php

I want to pass jQuery variable to php file ,
this is my_js.js file
function updates() {
$.getJSON("php/fetch.php", function(data) {
$.each(data.result, function(){
var s_id = this['sender_id'];
});
});
}
and i want to display it here in php file,
<html>
<head><title>Pass jquery var to php file</title>
</head>
<body>
<div> <?php echo $s_id ; ?> </div>
<script type="text/javascript" src="my_js.js"></script>
<script type="text/javascript" src="jquery.js"></script>
</body>
</html>
You might consider using jQuery to set the s_id value in the page.
You can modify your HTML as follows (the important part being the added span element):
<html>
<head><title>Pass jquery var to php file</title>
</head>
<body>
<div><span id="s_id"></span></div>
<script type="text/javascript" src="my_js.js"></script>
<script type="text/javascript" src="jquery.js"></script>
</body>
</html>
And you can use jQuery in your getJSON callback to set the value as follows to find your added span element and modify its text:
function updates() {
$.getJSON("php/fetch.php", function(data) {
$.each(data.result, function(){
$("#s_id").text(this['sender_id']);
});
});
}

How to put HTML header into a PHP variable

I would like to get my whole site's header into a variable.
So, for example, I would like to put these three lines into a single variable:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
And I'm writing a function for it:
function set_header(){
//This is where i would like to set the variable for the 3 lines I mentioned earlier
}
function set_header(){
echo '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>';
}
set_header();
OR
function set_header(){
return '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>';
}
echo set_header();
function set_header(){
$header = '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>';
return $header;
}
echo set_header();

Zend and Jquery, how to write in zend

In header.php file , below is used
<script type="text/javascript" src="http://example.com/library/includes/jQuery142.js">
</script> // jquery
<script type="text/javascript" src="http://example.com/library/includes/jqueryautoiframe.js">
</script> // plugin iFrame Sizing
in view file below code, How do i put below in to zend ?I used below code but not working.
<iframe src="blabla"></iframe>
<script>
$('iframe').iframeAutoHeight(); //iframeAutoHeight is the function in jqueryautoiframe.js
</script>
You need to wait until the document is ready before you can use any js
try this:
<script>
$(function() {
$('iframe').iframeAutoHeight(); //iframeAutoHeight is the function in jqueryautoiframe.js
});
</script>
try
<iframe src="http://google.com"></iframe>
<script type="text/javascript">
$('iframe').iframeAutoHeight(); //iframeAutoHeight is the function in jqueryautoiframe.js
</script>
Your iframe source is 'blabla' did you put it intentionally ??
try
<?php
$this->headScript()
->appendScript(
<<<'BODY'
$('iframe').iframeAutoHeight();
});
BODY
);
?>

Categories