echo Php variable from javascript and javascript is in php function - javascript

I have jQuery function in a PHP function. I have id of a div in a PHP variable. I want to pass this variable to the jQuery code, but it is not working:
public function food(){
echo '<script>
$(document).ready(function () {
$("#$this->ccid #food").show();
});
</script>';
}

Just use string concatenation:
public function food(){
echo '<script>
$(document).ready(function () {
$("#' .$this->ccid. ' #food").show();
});
</script>';
}

Related

How to trigger js function inside php?

if (!$errors) {
//Call Js function without any events
}
I want to call js function once inside if came,How to call js function inside if condition?Is it possible to call js fucntion without any events inside php tag?
with jquery
if (!$errors) {
echo "<script type='text/javascript'>
$(document).ready(function(){
myfunction();
});
</script>";
}
without jquery and wait unlit whole page is loaded
if (!$errors) {
echo "<script type='text/javascript'>
window.onload = function() {
myfunction();
};
</script>";
}
and without Onload
if (!$errors) {
echo "<script type='text/javascript'>myfunction();</script>";
}
You can also use this way :
<?php
// php code
if(condition){
?>
<script type="text/javascript">
jsFunction();
</script>
<?php
}
// php code
?>
You can also use jquery:
if (!$errors) {
echo "<script type='text/javascript'>
$(document).ready(function(){
myfunction();
});
</script>";
}
You could just make an echo and call it between two script tags.
Like :
if (!$errors) {
echo "<script> myfunction(); </script>";
}

How to alert box from ajax php function

I have function frm_trigger_entry_update this is the php function which is run in background i mean this is ajax php function.
In this function i have write some jquery or javascript function which will alert some text message.
In bellow snippet code you can see my function code.
<?php
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
}
?>
I have tried bellow snippet logic but its not work for me mean the alert box is not showing after calling this function.
<?php
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
?>
<script>
jQuery(document).ready(function($){
alert("my message");
});
</script>
<?php
}
?>
So how can alert the box in this php function any one have any idea.
Use JS and Php Separately.
First ajax call from your JS file:
$.ajax({url: "frm_trigger_entry_update function's Url",
success: function(result) {
alert(result);
}
});
In php Function, from where you should send your message:
function frm_trigger_entry_update($atts) {
echo "my message";
}
Consider following is your ajax call
$.ajax({url: "URL to frm_trigger_entry_update",
success: function(result)
{
alert(result);
}
});
Your PHP function
<?php
function frm_trigger_entry_update($atts)
{
echo "my message";
}
?>
Try this:
echo "<script>alert('test');</script>";
Note : The best practice to do this with Ajax because function is in
server side so you should call it to server from Client using Ajax
Create one file eg: "frm_trigger_entry_update.php"
IN "frm_trigger_entry_update.php" put your function
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
echo $atts;
}
// Call to that function
frm_trigger_entry_update("ATTRIBUTE_VALUE");
Write Ajax on your HTML
$.ajax({
type: 'get',
url: 'PATH to frm_trigger_entry_update.php',
success: function(data) {
alert(data);
}
});
You will get output Alert as ATTRIBUTE_VALUE
In your php function you need to return the output :
<?php
function frm_trigger_entry_update($atts)
{
return "<script>
jQuery(document).ready(function($){
alert('my message');
});
</script>";
}
And then where you want to apply this script you can display the output of your function :
<?php
...
$script = frm_trigger_entry_update($ats);
echo $script;
However in my opinion, this is not a good practice, you should put your javascript in a js function, in a js file and include your js file in your document or call it when needed.
Calling a php function via ajax is really impossible because ajax uses a url for php file and not a php function.
Though your code works and will alert if you call it via php.The code below will alert what ever string you put in the parameter of your function.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<?php
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
?>
<script>
jQuery(document).ready(function($){
alert("<?php echo $atts;?>");
});
</script>
<?php
}
//calling the function and passing a simple string
frm_trigger_entry_update("Alert Message");
?>
</body>
</html>

jquery + php - Call Function From Another Javascript Wrapper

How could I call function in different jQuery wrapper like this:
<?php
if($trigger != ""){
?>
<script type="text/javascript">
jQuery(document).ready(function($){
var delay = 5000;
setTimeout(rmv, delay); // call the function here
});
</script>
<?php
}
if($triggered != ""){
?>
<script type="text/javascript">
jQuery(document).ready(function($){
function rmv(){ // function to be called
$(".adt_front_wrapper<?php echo $id; ?>").remove();
}
});
</script>
<?php
}
?>
I am trying to call a function which should be separated from the caller like the rmv function in the example code above. How could it be done?
You would need to put that function into a higher (shared) scope. For this example, you could use the window object itself.
window.rmv = function() {
$(".adt_front_wrapper<?php echo $id; ?>").remove();
};
In order to make that work, the declaration and definition of rmv needs to happen beforehand of course.

How to render PHP output to initialise a variable in JavaScript?

I wan to call a PHP function in JavaScript code that echoes out a value:
<?php
function ex()
{
$num=10;
echo($num);
}
echo '<script type="text/javascript">
function sam_click(clicked)
{
var x="<?php ex(); ?>";
alert(x);
return false;
}
</script>'?>
However, it doesn't work. Can you help?
Try this:
<?php
function ex()
{
$num=10;
echo($num);
}
echo '<script type="text/javascript">
function sam_click(clicked)
{
var x="' . ex() . '>";
alert(x);
return false;
}
</script>'?>
Just try with:
...
var x="' . ex() . '";
...
Try echoing only what you need
<?php
function ex()
{
$num=10;
echo($num);
}
?>
<script type="text/javascript">
function sam_click(clicked)
{
var x="<?php ex(); ?>";
alert(x);
return false;
}
</script>
function ex()
{
$num=10;
echo $num;
return $num; // if you want to get JS alert
}
echo '<script type="text/javascript">
function sam_click(clicked)
{
var x='.ex().';
alert(x);
return false;
}
sam_click(); // if you want to get JS alert
</script>';
instead of echoing the $num, return it as a result and then append it in the script part.
<?php
function ex()
{
$num=10;
return ($num);
}
echo '<script type="text/javascript">
function sam_click(clicked)
{
var x="' . ex() . '";
alert(x);
return false;
}
</script>'?>
another solution with heredoc and nowdoc:
<?php
// helper to call functions as nowdoc
// example: {$fn( function() )}
function fn($functionCall)
{
return $functionCall;
}
$fn = 'fn';
function ex()
{
return 20;
}
$js = <<<EOT
<script type="text/javascript">
function sam_click(clicked)
{
var x="{$fn(ex())}";
alert(x);
return false;
}
</script>
EOT;
echo $js;
Find more detail on PHPDoc:
Nowdoc (PHP5.3+)
Heredoc
Anonymous Functions (PHP5.3+)

PHP isset variable than jquery and javascript should work

I want to know if my php variable does not exists, then I want to execute my javascript.
For example-
<?php
if(!isset($_REQUEST['myVar'])){
?>
<script>
alert('variable not exist');
</script>
<?php
}
?>
Is this right way to use javascript code in php extension file
I know all other answers solve your issue but i prefer it to do this way
<script>
<?php
$isset = !isset($_POST['myVar']) ? 'true' : 'false';
echo "var isset = $isset;";
?>
if(isset) {
alert('variable not exist');
}
</script>
when php render your page it will give this output
<script>
var isset = true;
if(isset) {
alert('variable not exist');
}
</script>
Do it like this and it will work:
if (!isset($_REQUEST['myVar'])) {
echo "<script>alert('variable not exist');</script>";
}
you can try writing this piece of code where you want the script to be placed:
<?php
if (!isset($_REQUEST['myVar'])) {
echo '<script>
alert("variable not exist");
</script>';
}
?>

Categories