opening new window in PHP not working - javascript

I am trying to open a new window in PHP by echoing what i need, because im placing information from my database in the URL.
I can get this to work in plain HTML, JS but as soon as i try and echo out the line it doesnt call the function, im sure im missing something obvious.
Got the code from here http://www.quirksmode.org/js/popup.html
Thanks
<script language="javascript" type="text/javascript">
<!--
function popitup(url) {
newwindow=window.open(url,'name','height=400,width=600');
if (window.focus) {newwindow.focus()}
return false;
}
// -->
</script>
<?php
echo "<a href = 'popupex.html' onClick='return popitup(\'popupex.html\')'‌​>234</a>";
?>

You had two hidden characters before your > in )'‌​>234 which could contribute to the problem. (Consult Footnotes).
Plus, I replaced (\'popupex.html\') to (\"popupex.html\") with escaped double quotes. Leaving them as single quotes did not make the window to pop up when testing.
The two hidden characters: ‌​
This works with both hidden characters removed. (tested)
Copy the code below, do not replace with what you already have.
<script language="javascript" type="text/javascript">
<!--
function popitup(url) {
newwindow=window.open(url,'name','height=400,width=600');
if (window.focus) {newwindow.focus()}
return false;
}
// -->
</script>
<?php
echo "<a href = 'popupex.html' onClick='return popitup(\"popupex.html\")'>234</a>";
?>
Footnotes:
Leaving the hidden characters in the code would show up as this in the HTML source:
'??>234

Use this code:
<script language="javascript" type="text/javascript">
<!--
function popitup(url) {
newwindow=window.open(url,'name','height=400,width=600');
if (window.focus) {newwindow.focus()}
return false;
}
// -->
</script>
<?php
echo "<a href = 'popupex.html' onClick='return popitup(\"popupex.html\")'‌​>234</a>";
?>

<?php
if(something): //if you received data from database
//here you can also add value from database
$phpvalue = "data from database";
//you can popup php value or just add your target url instead of $phpvalue
?>
Test
<?php else:
if(SOMETHINGELSE)
?>
<?php endif ?>

Related

php call a js function that is defined on different js file

I am trying to call
this js function that is stored on a file called default.js from a php file.
function myfunction(score) {
console.log("got here");
alert(score);
}
so this is the call for the js function on my php code but it does not work.
<!-- myfunction js file ↓ -->
<script type="text/javascript" src="../js/default.js"></script>
<?php
if ($grade=="100%"){
// not able to call
echo "myfunction($grade)";
}
else{
echo "alert(\"haha\")";
}
?>
Try this you will get the accurate result
<?php
if ($grade == "100%") {
?>
<script>
myfunction('<?=$grade?>');
</script>
<?php
} else {
?>
<script>
alert('in');
</script>
<?php } ?>
you forgot about the <script> tags
change your echo like this:
echo "<script>myfunction('$grade')</script>";
you need to wrap ' around the $grade because you're passing a string
or put <script> before the opening php tag and </script> after the end of php tag
This is your Code.
Define $grade too in PHP or you get a
<b>Warning</b>: Undefined variable $grade in <b>
<script type="text/javascript" src="../js/default.js"></script>
<script>
<?php
$grade = "10%";
if ($grade=="100%"){
echo "myfunction('$grade')";
}
else{
echo 'alert("haha");';
}
?>
</script>
Dont forget the <script></script> for javascript code inside html.

How to refresh a specific div content along with the link id when clicked on a link?

I am trying to refresh the div content by clicking on href link along with the id(named as data), my actual problem is I'm unable to get the id value with in the script because it is passed with in the function. How to pass the data variable in my php to script? Please check my code below and help me out to solve this. img_div is id of my div which is to be refreshed
PHP:
function printSubDir($dir, $path){
global $data;
$data = $path.''.$dir;
echo "<li><span class=\"toggle\"><a href='asset1.php?data=$data' id='refresh'>$dir</a></span>";
createDir($path.$dir."/");
echo "</li>";
}
Script:
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('#refresh').click(function(){
$('#img_div').load('asset1.php #img_div', function() {
});
});
});
</script>
Hi assuming that your variable is $data.You can a php variable in javascript like the following
<?php
$data = "Value of your variable";
?>
<script>
// now a javascript variable data stores the value of your php variable
var data= "<?php echo $data?>";
</script>
I Hope this heps
Echo the id into the function call:
<?php
echo "<a onClick='handleClick($id)'>$dir</a>";
?>

document.title funcion can't set it work with php

I have this line of code
<script type="text/javascript">
document.title = "<?php include('pot1.php');?>"
</script>
My pot1.php gives me an echo with one random number, but that code does not seem to work and I don't know why the echo doesn't appear in my title. Is anything wrong with the code?
pot1.php code:
<?php
#include_once('link1.php');
$cg = fetchinfo("value","info","name","current_game");
$cb = fetchinfo("cost","games","id",$cg);
$cb=round($cb,2);
echo '$'.$cb;
?>
This echo is working and is giving me values because I can see them in one div.
You should just do it like this:
<?php include('pot1.php');?>
Now we know that there is a variable set. Echo that into the title tag.
<script type="text/javascript">
document.title = "<? echo $cb ?>"
</script>
The way you're doing it now... I don't think you can include an entire PHP page and expect to behave like a simple string, even if that's all it returns.
Put the value you want inside a variable in the pot1.php file and use it on the title
<?php include('pot1.php');?>
<script type="text/javascript">
document.title = "<?php echo $yourVar;?>"
</script>

Bootstrap dynamically set modal title in php

I've been trying to change my modal title remotely from php. The title that I want to be set is a variable and has already been assigned but it changes when the user inputs something new.
Here is my php code:
$Studname = $results['Studentname'];
echo '<script type="text/javascript">';
echo 'alert("'.$Msg.$Studname.'")';
echo '</script>';
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#MyAdvertLabel').text(".$Studname.");
});
</script>";
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#myAdvert').modal('show');
});
</script>";
There seems to be no error in the code according to the program i'm using but still when I run the code there is no change in the modal title.
Your PHP will generate a Javascript error:
<?php
$Studname = 'Sample text';
$('#MyAdvertLabel').text(".$Studname.");
?>
Will generate:
$('#MyAdvertLabel').text(sample text);
The contents of the text() function need to be wrapped in quotation marks.
For example:
<?php
$Studname = 'Sample text';
$('#MyAdvertLabel').text('".$Studname."'); // Notice the ' ' wrapping the variable
?>

how to put javascript as function parameter

For some reasons, I have to set page head section from another include page.
First look at inc.php page below:
page: inc.php
<?php
function sethead($javascript){
echo '<html><head>';
echo '<link rel="stylesheet" type="text/css" href="site.css" />';
echo '<script type="text/javascript" src="jquery1.9.1.js"></script>';
echo $javascript;
echo '</head>';
}
?>
The inc.php page contains sethead() function which will set head for each page like this:
page: main.php
<?php
include('inc.php');
$javascript = '<script type="text/javascript" language="javascript">.......</script>';
sethead($javascript);
<body>
Here body codes go here
.....................
.....................
.....................
</body>
</html>
?>
The main.php page could be any page, like secondpage.php, thirdpage.php .... and so on.
But as the javascript for each page might be different, so I have to echo this javascript from the function parameter of sethead($javascript). But I could not find a suitable way to declare this javascript parameter in main.php page before calling the function sethead($javascript) because the javascript code consists of several line of codes which are difficult to put in a variable.
<script type="text/javascript" language="javascript">
document.oncontextmenu = function() {
return false;
}
window.onbeforeunload = function() {
return "Please don't reload this page.";
}
</script>
How can I put the above lines of javascript code in a variable/function parameter ($javascript) in a easy way?
If you just want an easier why to define a multi-line variable, use NOWDOC:
$javascript = <<<'EOD'
<script type="text/javascript" language="javascript">
document.oncontextmenu = function() {
return false;
}
window.onbeforeunload = function() {
return "Please don't reload this page.";
}
</script>
EOD;
Although it might be easier to write the javascript in an external .js-file and pass the filename to include instead.
PHP supports multiline strings, so you can simply white:
$javascript = '
<script type="text/javascript" language="javascript">
document.oncontextmenu = function() {
return false;
}
window.onbeforeunload = function() {
return "Please don\'t reload this page.";
}
</script>
';
And don't forget to escape all ' symbols.
But I think the best decision would be keep js-code in a js-file

Categories