my script generates the image using php/html. once it generates, i want to display the image without reloading the page.
I call the javascript(to change the image) function within the PHP itself. but i want to know how to pass new image name in this java script?
$newpath as src variable
Here is the code:
$newpath="output/".$outputfilename;
echo '<script type="text/javascript">',
'document.getElementById("latestimage").src="<newpath>";',
'</script>';
You should concatenate your variable when echo, like:
$newpath="output/".$outputfilename;
echo '<script type="text/javascript">
document.getElementById("latestimage").src="' . $newpath . '";
</script>';
Related
Ive struggled with a bug which I can simplify to this:-
if I set a variable $test in php and try to access it in javascript it sometimes works, but often doesnt
Im using alert to display the data in javascript... alert( php tag echo $test; close php tag ) if it doesn't show below
<script>
alert(<?php echo $test; ?>);
</script>
example, if in php I set $test="hello"; it does not work. Nothing!
but if I set $test=time(); it does work
why cant I set a simple string and access it in javascript? Its strange it can access a more complicated timestamp but not a string!!
You need to tell JavaScript that you want to alert a string.
alert('<?php echo $test; ?>');
Notice the quotes wrapped around the PHP statement.
You're missing out "".Try this instead.
<script>
alert("<?=$test?>");
</script>
You need to add quotes
alert('<?php echo $test; ?>');
I want to call a PHP function passing an argument on clicking a hyperlink (text with <a> tag) on the same page, i.e. href='#' onclick='loadpic($id).
Where $id is the variable to be passed to PHP function.
You can do that by ajax easily. Create a loadpic function in javascript instead and then pass $id to the php file via ajax and do something when you get the result.
Your javascript file should look like this-
(make sure you've inserted a link for jquery in your index file)
function loadpic(id_param){
$.post("filename.php",
{
id: id_param
},
function(data){
alert("Data received:" + data);
});
};
The data parameter in function(data) would be the value that you've echoed in the php file.
If you want to use onclick() method in your HTML element then you have to write your method is javascript and make an AJAX call to PHP. THis is one simple way to do this using php and javascript. And for this please see javascript and ajax with PHP.
But if you dont want to use javascript then here is a simple way and very simple code.
<?php
if(isset($_GET['myMethod'])){
myMethod($_GET['id']);
}
function myMethod($id){
echo $id."<br>";
}
?>
<?php
$myId = 4;
?>
<a href="index.php?id=<?php echo $myId ?>&myMethod" >MyLink</a>
Note: Copy this code and paste into a file called index.php because notice the hyper link tag and its attribute href="index.php".
Also note that you can not directly call php method using js like onclick() from your HTML tag. You should use AJAX or something. But using above demo code you can easily call myMethod() which is a PHP method using the hyper link tag.
In your case:
<?php
if(isset($_GET['call_loadpic'])){
loadpic($_GET['id']);
}
function loadpic($id){
echo "My loadpic method has been called! and the id is".$id."<br />";
}
?>
<!-- your other codes -->
<?php
$id = 4;
?>
<a href="this-same-file-name.php?id=<?php echo $id ?>&call_loadpic" >MyLink</a>
This is because your php function is in the same page and you want to call that function using same file and hyperlink in the same file. (According to your question)
You still will have to make javascript handle the exception because php has no onclick.
onClick=\"loadpic("'.$id.'");"\
//this will only send a php variable javascript will still have to loadpic();
My js code re-loads the comments.php in every 3 seconds .
I have included comments.php in index.php and it does get all the variables from index.php but when js reloads comments.php it does not get the variable and shows error .
First time it works cause it's included with php . After the re-load it's not included anymore .
How can i include comments.php in index.php inside js.
index.php
<?php
if (isset($_GET['slug'])){
$page_id = $_GET['slug'];
}
?>
<div id="comments">
<script type="text/javascript" >
$(document).ready(function(){
setInterval(function(){
$('#comments').load('comments.php')
},3000);
});
// when loading comments.php with js $page_id is not gotten and showing error
</script>
<?php include("comments.php") ; ?>
// first time shows this and comments.php does get the varible $page_id
</div>
comments.php
<?php
$page_id = $_GET['slug'];
$get_com = " SELECT * FROM `comments` where post_id='$page_id' ORDER BY `comment_id` DESC ";
$run_com = mysql_query($get_com);
?>
When you include("comments.php") it inherits the variables from index.php. When you reload with load('comments.php') you are doing it via HTTP and there is no GET variable slug because you are not passing it:
$('#comments').load('comments.php?slug=<?=urlencode($page_id)?>')
The simplest way to do this is to simply echo the PHP variable to JavaScript:
var page_id = '<? echo $page_id ?>';
var get_com = '<? echo $get_com ?>';
var run_com = '<? echo $run_com ?>';
Assuming those variables are populated by the time the PHP is finished, JavaScript will be able to make use of them.
In a php file I have tags with dynamic id's.
Example:
<ul id="<?php echo 'ul_'.$xx; ?>">
<li id="test_li">...</li>
</ul>
In a separate JavaScript file I am wanting to use the load function and target that div.
$('<?php echo 'ul_'.$xx; ?>').load('../includes/myphpfile.inc.php #test_li');
My question is, how could I load that php within a JavaScript file?
Notes:- The reason I have it set up this way is due to looping multiple ul's. I just want my ajax file to refresh the li's within the specific ul I am inside of.
Edit: If I use classes instead of Id's it adds my newly inserted rows into all of the ul's with the same class. If I use a plain Id it only works on the first ul. Further explaining why I need to target the way I am.
Unable to find a solution using the path I was on, I looked for alternate methods. Worked out I could add the event in the php file on the submit form button click.
The reason I wasn't using this method at first was because the code was running before it submitted to the DB and therefore was always behind in relation to the new data being submitted.
To my surprise I fixed this by just duplicating the line of code.
<script>
$("<?php echo '#enter_submit_button'.$linkid ?>").click(function(){
/*1*/
$('#<?php echo 'ul__'.$linkid; ?>').load('../includes/pursuit.inc.php <?php echo '#li__'.$linkid; ?>');
$("<?php echo '#insert_new'.$linkid; ?>").fadeOut();
/*2 with delay*/
$('#<?php echo 'ul__'.$linkid; ?>').delay( 800 ).load('../includes/pursuit.inc.php <?php echo '#li__'.$linkid; ?>');
});
</script>
I honestly couldn't say exactly why this works but the logic behind trying the method came from hoping that the double load would pick up the latest submission.
The simplest answer would be to put your JavaScript code into the PHP file inside
<script></script> tags, i.e, just before closing your <body> tag, do this:
<script type="text/javascript">
$('<?php echo 'ul_'.$xx; ?>').load('../includes/myphpfile.inc.php #test_li');
</script>
But if you must use an external js file, then you could do this:
Create a (.php) file and write:
<?php
header("Content-type: application/javascript");
?>
// your js code with php inside (as you would do in HTML)
var othervar='<?php echo $phpVar; ?>';
//<?php //some php ;?>
Then, in your main (html/php) file, put in:
<script src='myjs.php' type='application/javascript'></script>
Hope that solves it.
I use a simple PHP script placed on each page of my websites to track some stats.
<?php
echo( "<img src='http://maindomain.com/stats.php?var1=" . #$_GET['var1'] . "&var2=" . #$_GET['var2'] . "&r=" . #$_SERVER['HTTP_REFERER'] . "' width='1' height='1' border='0' />" );
?>
This script works fine, once placed on domain1.com and domain2.com the file in maindomain.com/stats.php must validate the GET and register the stats.
The problem is that i also have domain3.com and this site is not PHP but simple HTML.
So i'm looking for a way to change the script in javascript, something like this:
<script src="http://maindomain.com/stat.php?var1=XXX&var2=YYY&r=HTTP_REFERER"></script>
I think this is possibile but i don't know how to pass the GET vars with js.
You could try using CSS instead of JavaScript.
If you create your stylesheet as such; style.css.php, you can execute php code in the stylesheet, and let the rest of the stylesheet just be as it already is.
Something like:
<?php
include('stats.php')
?>
html {
color: #000
}