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
Related
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 3 years ago.
Hello there im learning php also javascript but i dont know how to put a while loop into javascript parameters area.
<script type="text/javascript" src="//....." async="true"></script>
<script type="text/javascript">
window.criteo_q = window.criteo_q || [];
var deviceType = /iPad/.test(navigator.userAgent) ? "t" : /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test(navigator.userAgent) ? "m" : "d";
window.criteo_q.push(
{ event: "setAccount", account: 62056}, // You should never update this line
{ event: "setEmail", email: "##Email Address of user##" }, // Can be an empty string
{ event: "setSiteType", type: deviceType},
{ event: "viewItem", item: "##HERE##" });
</script>
And the goal is output of "##HERE##" needs to be done php while loop from database. (I can give detailed description if asked)
If the file is .php exentension, you could have a <?php echo "JAVASCRIPT CODE" ?> with that you can could an output javascript inputing variables into it, by using concatenating. It is not a good pratice, but would soulve your problem.
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'] ?>
This question already has answers here:
php $_GET and undefined index
(12 answers)
Closed 9 years ago.
I want to open a page with ajax, and ajax to show the link in the browser url. Of course, the page does not reload the whole page, only the content.
Here is my code., but I keep on having an error "Notice: Undefined index: rel in C:\xampp\htdocs\www\html5-history-api\menu1.php on line 2"
I'd like this error to go away...
header.php :
<br>Header Content from header.php</br></br>
<style>
#menu{font-size:20px;}
#content{font-size:30px;}
</style>
<script language="javascript" src="jquery-1.4.4.min.js"></script>
<script>
$(function(){
$("a[rel='tab']").click(function(e){
//e.preventDefault();
/*
if uncomment the above line, html5 nonsupported browers won't change the url but will display the ajax content;
if commented, html5 nonsupported browers will reload the page to the specified link.
*/
//get the link location that was clicked
pageurl = $(this).attr('href');
//to get the ajax content and display in div with id 'content'
$.ajax({url:pageurl+'?rel=tab',success: function(data){
$('#content').html(data);
}});
//to change the browser URL to 'pageurl'
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
return false;
});
});
/* the below code is to override back button to get the ajax content without reload*/
$(window).bind('popstate', function() {
$.ajax({url:location.pathname+'?rel=tab',success: function(data){
$('#content').html(data);
}});
});
</script>
<div id='menu'>
<a rel='tab' href='http://localhost/www/html5-history-api/menu1.php'>menu1</a> |
<a rel='tab' href='http://localhost/www/html5-history-api/menu2.php'>menu2</a> |
<a rel='tab' href='http://localhost/www/html5-history-api/menu3.php'>menu3</a>
</div>
And menu1.php (menu2 and 3 are the same as 1)
<?php
if($_GET['rel']!='tab'){
include 'header.php';
echo "<div id='content'>";
}
?>
menu1 content in menu1.php
<?php
if($_GET['rel']!='tab'){
echo "</div>";
include 'footer.php';
}?>
So yeah, the code works, but I don't like having an error in my code and I don't know what to do.
Thanks.
Trying updating it from:
if($_GET['rel']!='tab'){
To this:
if(isset($_GET['rel']) ? $_GET['rel']!='tab')){
See if that fixes it.
As the warning says, you don't have an array index defined. your code should be
if (isset($_GET['rel']) && ($_GET['rel'] != 'tab')) {
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.
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.');
});