Calling a jquery function from php [duplicate] - php

This question already has answers here:
How to call a JavaScript function from PHP?
(12 answers)
Closed 8 years ago.
I've got a jQuery function that triggers when the selected option from a select element changes, its a function that is applied to the select element when it have a specific class:
<script type="text/javascript">
$(document).ready(function()
{
$('.acompanante').on('change', function(event)
{
//my code
}
}
</script>
And this is my select element code:
<select class="acompanante" name="acompanante_anterior" id="<?php echo "$i" ?>">
<option value="0" ><?php echo $txt_nuevo_acompanante;?></option>
....
</select>
How do I call the jQuery .change() event of the select element from PHP?

You can't call it from PHP and you don't need it. jQuery doesn't see the PHP, but only the rendered HTML.
So you can just bind it as normal, in this case use the class.
$('.acompanante').change(function(){
this // the dropdown which changed
});
Within that, you can use $(this) to see which dropdown it is.
Want to call javascript from PHP, let PHP render the JavaScript to do so:
<script>
<?
echo 'javascriptFunction();';
?>
</script>

You want to execute your function on page load.
<script type="text/javascript">
$(document).ready(function()
{
$('.acompanante').on('change', fillDropdown);
fillDropdown.call($('.acompanante'));
});
function fillDropdown(event)
{
}
</script>
Some trickery is involved to make this be the jquery object, which is the default behaviour for jQuery. That is what the fn.Call does.

With change():
$('.acompanante').change(function() {
alert('change() called.');
});

Related

PHP how to get values from javascript into php [duplicate]

This question already has answers here:
How to get JavaScript variable value in PHP
(10 answers)
Closed 6 years ago.
<button onclick=run(4)>
clickme
</button>
<script>
function run(a){
alert(a);
<?php
if('<script>document.write(a)</script>'==4){
echo 'runing php';
echo '<script>alert(a);</script>';
}
?>}
</script>
I'm new to php,i'm not able to get how the process is going on here.here i want to get value from javascript into the php variable...
this should be your html
<button onclick=run(4)>
clickme
</button>
<script>
then in you js file use jquery like
$(document).ready(function(e){
function run(val){
$.post('yourfilename.php',{yourvariable:val},function(response){
alert(response); // with alert you can see the feeback from php.
});
}
});
in php file get the value of jquery like this
$value = $_POST['yourvariable'];
echo $value; //this value will be return to your ajax call as response

How to pass value from php to javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
pass php variable value to javascript
I want to create a progress bar, the value of the progress will be coming from PHP.
<div class="prog" id="progressbar"></div>
<label id="amount">
<?php echo $cast; ?></label>
I have this kind of javascript.
<script>
$(function() {
$( "#progressbar" ).progressbar({
value: 0
});
});
</script>
How can I throw the value $cast in my script?
<script>
$(function() {
$( "#progressbar" ).progressbar({
value: <?php echo intval($cast);?>
});
});
</script>
Javascript is run in Client (browser), while PHP is run on server. You can't cast varble from PHP to javascript like that.
With print progress:
<?php echo $cast; ?>
it just display the value of current run time, it is not updated.
If you have a link to check value of progress bar, just use ajax POST or GET to make change.

Little correction required in SELECT in HTML using Javascript/jQuery

my select option in HTML is,
<select name="id" id="id" style="width:400px;" onChange="loadAjax(this.value);">
<select name="name" id="name" style="width:400px;">
Which loads the ajax function (using javascript) when the value is change in SELECT (html).
But I am in different requirement, that when this select input loads, it should call the loadAjax function directly, and on change as well.
How it could be possible?
I tried some things like window.onload, onload, but nothing worked.
Given that you've tagged your question with "jquery", I'd suggest a jQuery solution:
$(document).ready(function() {
$("#id").change(function() {
loadAjax($(this).val());
}).change();
});
This defines the change handler and then immediately triggers it. (You would remove the inline onclick from your html.)
Or without jQuery perhaps something like this:
window.onload = function() {
loadAjax(document.getElementById("id").value);
};
window.onload = function() {
loadAjax(document.getElementById("id").value);
};
you can also bind change event to #id element.
$(document).ready(function() {
$("#id").bind("change",function() {
loadAjax($(this).val());
});
});

Pass PHP variables to Javascript/jquery [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
So far I know two ways to pass php variables to javascript.
One is by using
<script>
$(document).ready(function()
phpvalue=$("#hiddeninput").val()
})
</script>
<input type="hidden" id="hiddeninput" value="phpvalue">
And the other one is by having for example a button and using onclick
<script>
function clickf(phpvalue) {
}
</script>
<input type="submit" onclick="clickf(<?php echo phpvalue ?>)">
All of them work great but:
Is there any other way that I'm missing?
Which one is the "best"?
Any chance that I can use inside the script or the external js ?
<script>
$(document).ready(function()
var phpvalue = <?php echo $var; ?>
});
</script>
Like others already answered, just put a PHP tag whereever you would place the JS value, like var foo = <?= $php_value ?>; or var foo = <?php echo $php_value ?>;.
If you want to use this in an external JavaScript file, you have to make sure .js files get parsed by PHP. If this is not an option for you (for example, your host doesn't allow it), I suggest you set the values in a <script> tag inside your <head> and then just reference thos variables from your external JavaScript. In that case, I would strongly suggest you namespace them like var app_vars = { foo: <?= $bar ?> } in order to not clutter the global object.
Another way would be to retreive the values via Ajax. But the viability of this approach depends on your use case.
And another hint: if you want to pass multiple variables or arrays, there's JSON baked into PHP since version 5.2:
<?php
$my_complex_var = array(
'array' => array('foo', 'bar'),
'val2' => 'hello world'
);
?>
<script>
var my_complex_js_var = <?= json_encode($my_complex_var) ?>
</script>
<script>
var javascript_variable = <?php echo $php_variable; ?>;
</script>
Instead of assigning values to hidden inputs, you could just generate JavaScript directly:
$script = <<<EOT
<script>
var phpvalue = $phpvalue;
</script>
EOT;
echo $script;
for the top example you do not need to use val you could use any attr you like.
For example
phpvalue="myvalue"
then within jquery
$("#hiddeninput").attr("phpvalue");

How to Bind the Onclick event In Jquery in correspondance to Javascript

I Have a select button in php code which Passes the Dynamic generated Row value and How do i do this jquery?
PHP code
<input type ="button" onclick="select(add,'<?php echo $_POST['somevale=ue']?>')"
Javascript
function select (fnname, val){
//Val changes every time
}
How do i acheive the same in Jquery in Putting the click event to the button and passing the Dynamic value and Defining the Function in Jquery?
Use the click handler inside your document ready. Note that add must be a valid function for this to work available in the current scope.
$(function() {
$('input').click(function() {
select(add, 'value you need to pass');
});
function add() {
..
}
});
You can use HTML5 data-attributes here (they don't cause issues in HTML4), like this:
<input type="button" class="add" data-value="<?php echo $_POST['somevalue']?>" />
Then you can fetch it in your function using .attr(), like this:
$(function() {
$(".add").click(function() {
var value = $(this).attr("data-value");
select('add', value); //call original function
//or, put that function's content in here
});
});
Give it a try here

Categories