How to pass value from php to javascript [duplicate] - php

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.

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

jquery event does not work on ajax result like table [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 6 years ago.
I created a table with radio button using ajax request. I want to get value from radio button by click event. But jquery event doesn't work on radio button.
my created table by ajax below
I have called this ajax result by bellow code
<code>
<script type="text/javascript">
$(document).ready(function(){
$(".todayTask").click(function(){
var employee = '<?php echo trim($userID);?>';
$.ajax({
url: "ajax_call.php",
type:"POST",
data:{userID:employee},
success: function(result){
$('#taskList').html(result);
}});
});
});
</script>
</code>
Now I want to get value from radio button which stay in ajax result...
by below code but does not work...
<code>
<script type="text/javascript">
$(document).ready(function(){
$("#s").click(function(){
var status_val = $(this).val();
alert(status_val);
});
});
</script>
</code>
Since your radio buttons are loaded via jquery, you need to use on event:
$(document).ready(function(){
$('#taskList').on("click","#s", function(){
var status_val = $(this).val();
alert(status_val);
});
});
You need to also use a class ".s" instead of an ID "#s", because an ID needs to be unique, here is an example:
$('#taskList').on("click",".s", function(){
Bind your click event using on:
$("table").on("click","#s" ,function(){
var status_val = $(this).val();
alert(status_val);
});
Note: ID must me unique. So, either use classes or make sure you have unique id

Set Session inside the javascript [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I want to set session inside the javascript. But all the time this function return false value. Please tell me the error of this code.
<script>
function myFunction(){
var r=confirm("Would you like to add another item ?");
if (r==true) {
alert('ok');
var varname = '<?php echo $_SESSION["redirect"]=1; ?>';
}
if(r==false) {
alert('bad');
var varname = '<?php echo $_SESSION["redirect"]=2; ?>';
}
}
</script>
You can't evaluate PHP on the client. Do an AJAX request to the server setting the session variable instead.
Javascript runs on the client side, so you'll need to perform an AJAX call to a server side script that will change the sessions value.
Something in the lines of:
function changeSession()
{
$.ajax({
url: /change_session.php?value=1&key=redirect,
dataType:"html"
});
}
And in change_session.php:
<?php $_SESSION[$_REQUEST['key']]=$_REQUEST['value'] ?>

How to change jQuery progressbar status on changed PHP variable? [duplicate]

This question already has answers here:
How to update jQuery Progress bar according to a value at the session variable in PHP
(2 answers)
Closed 9 years ago.
Idea is to update progressbar value when PHP variable changes:
<script>
$(function() {
$( "#progressbar" ).progressbar({
value: <?php echo $_SESSION['value'] ?>
});
});
</script>
I tried:
<?php
$_SESSION['value'] = 10;
sleep(2);
$_SESSION['value'] = 30;
sleep(2);
$_SESSION['value'] = 50;
sleep(2);
$_SESSION['value'] = 70;
sleep(2);
$_SESSION['value'] = 90;
?>
but progressbar is set to last variable 90 only. I want it to continuously update for five times as the value of the variable changes.
Is it possible to do this with jQuery and PHP?
This is possible but only using AJAX as javascript doesn't have access to the PHP session.
The javascript only runs once the PHP file has finished executing which is why you're only seeing it update with the last value.
JS:
setInterval(function() {
$.get("currentProgress.php", function(data) {
$("#progressbar").progressbar({
value: data
});
});
}, 1000); // updates every second
PHP file:
<?php
// work out the current progress of whatever you're checking on
echo $value;
?>
Update: Maybe this is more what you're after: http://w3shaman.com/article/php-progress-bar-script - it can be easily modified to include the jQuery progress bar.
$_SESSION exists only on the server-side, so before your browser even receives the response, it will go through all the five stages. The value that will be transmitted is 90.
If you want to monitor a server-side process you will have to use a different approach, e.g. via ajax.

Calling a jquery function from php [duplicate]

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.');
});

Categories