Embedding Javascript in PHP With Brackets - php

Alright so I've been trying to echo some JavaScript for the fancybox window to popup if a user logs in or out. This is what I have so far, but it keeps throwing a server error.
<link rel="stylesheet" type="text/css" href="scripts/fancybox/jquery.fancybox.css" />
<script type="text/javascript" src="scripts/fancybox/jquery.fancybox.js"></script>
<?php
$msg = $_GET['msg'];
if(isset($msg['0'])); {
echo '
<script type="text/javascript">
$.fancybox.open([
{
href : \'m.php?=0\',
title : \'Successfully Logged In!\'
}
], {
padding : 0
});
</script>';
}
else if (isset($msg['1'])); {
echo '
<script type="text/javascript">
$.fancybox.open([
{
href : \'m.php?=1\',
title : \'Successfully Logged Out!\'
}
], {
padding : 0
});
</script>';
}
?>
What am I doing wrong?

The problem isn't with the brackets. PHP won't echo out multiple lines of code. In order to do what you're trying to do you need to set up a variable and then echo that out:
$myVar = "";
$myVar .= "<script src='text/javascript'>";
$myVar .= "blah blah blah...";
echo $myVar;

Don't echo out the script, do something like this.
<?php
$msg = $_GET['msg'];
if(isset($msg['0'])); { ?>
<script>
//JS
</script>
<?php } ?>

if(isset($msg['0'])); {
Semicolon there? I think not. Besides is message an array? Maybe you wanted to do:
$msg = isset($_GET['msg']) ? $_GET['msg'] : false;
if($msg === '0') {

Related

load() isn't working within PHP echo

I have the following snippet on my index page so that all links clicked on within the navbar will load within the 'content' div:
$(document).ready(function()
{
$("a.link").click(function() {
return false;
});
$("#navbar a").click(function(){
var a_href = $(this).attr('href');
$("#content").load(a_href);
});
});
This snippet works fine... But upon loading the page, I check with PHP to see if the index page has an argument to direct it to a specific page, which doesn't seem to work:
<?php
if(array_key_exists('page',$_GET)){
echo "<script>$('#content').load('" . $_GET['page'] . "', 'content')</script>";
} else {
echo "<script>$('#content').load('home.php')</script>";
}
?>
It may be a simple case of "it's staring you in the face but you've been looking at it for too long", but I can't seem to get the PHP snippet to work.
EDIT: I wrapped it in document.ready(), and it doesn't seem to have had an effect:
echo "
<script>
$(document).ready(function()
{
$('#content').load('home.php');
});
</script>";
Try this
<?php
echo "<script>"
echo "$(document).ready(function () {";
if(array_key_exists('page',$_GET)){
echo "$('#content').load('" . $_GET['page'] . "', 'content');";
} else {
echo "$('#content').load('home.php');";
}
echo "});";
echo "</script>";
?>

How can I change innerHTML with PHP?

I want to change the innerHTML with PHP code. But I cannot get it to work, and I do not understand why. I want it to change some text on the page but from another file. And so I thought that I could use this:
document.getElementById ("page"). innerHTML = "<? php echo $ home?>";
But it does not work.
Here is my code:
<?php
$home = file_get_contents("home.php");
?>
<script type="text/javascript">
function ChangePage(page)
{
if(page == "home")
{
document.getElementById("page").innerHTML = "<?php echo $home ?";
}
}
</script>
There are many small typos. Try removing the space between $ and 'home' and before 'php'. This is the right statement:
document.getElementById ("page"). innerHTML = "<?php echo $home?>";
Also, where's your closing php tag?
<?php
$home = file_get_contents("home.php");
?>
<script type="text/javascript">
function ChangePage(page)
{
if(page == "home")
{
document.getElementById("page").innerHTML = "<?php echo $home; ?>";
}
}
</script>
Although this is a bad practice. Why would you want to do this instead of simply loading the php in the right place? Also, you do realize that 'page' should be the id of a pre-existing div in your html, right? Something like this would be better:
<html>
<body>
<div id = "page">
<?php echo file_get_contents("home.php"); ?>
</div>
</body>
</html>

using javascript to disable link in php script

Hello I am trying to disable a link in my PHP script using a JS function. I am not sure why is its not working. Any input appreciated..
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function disable()
{ document.getElementById("perfumes_web_link").innerHTML='<http://www.google.com" rel="external"">Perfumes';
}
function enable()
{ document.getElementById("perfumes_web_link").innerHTML='<a href="http://www.google.com" rel="external"">Perfumes<\/a>';
}
</script>
</head>
<?php
$disable_link = 0 ;
echo '<h1>JavaScript to disable link if set to zero</h1>';
echo '<li><span id="perfumes_web_link"></span><br>'."\n";
echo '<span class="perfumes_browser"><small>(Internet Explorer v6 or higher req.) < /small></span></li>';
if ($disable_link = 1)
{echo '<script type="text/javascript">disable();</script>';
}
elseif ($disable_link = 0)
{
echo '<script type="text/javascript">enable();</script>';
}
?>
</html>
So what am i doing wrong? Should i use if/else statement in the JS function itself or should i use true/false instead of 0 and 1.
Should i use something else like jQuery or Ajax..
Thanks
since you are comparing, use == instead of =,
like, change
if ($disable_link = 1)
to
if ($disable_link == 1)
OR you can do:
if ($disable_link == 1) {
echo '<script type="text/javascript">disable();</script>';
}
else {
echo '<script type="text/javascript">enable();</script>';
}
If you mean to open the link in new window, use target="_blank" instead of rel="external", like:
change
<a href="http://www.google.com" rel="external"">Perfumes<\/a>
to
Perfumes

how to return javascript in a php class file ?

I want the following javascript code to be echoed in a PHP class file. How I can achieve that?
echo '<script language="JavaScript" type="text/javascript">'."\n";
echo 'var parDoc = window.parent.document;';
if ($result == 'OK') {
echo 'parDoc.getElementById("picture_error").innerHTML = "";';
}
else {
echo "parDoc.getElementById('picture_error').innerHTML = '".$result_msg."';";
}
if($filename != '') {
echo "parDoc.getElementById('picture_preview').innerHTML = '<img src=\'$preview_url$filename\' id=\'preview_picture_tag\' name=\'preview_picture_tag\' width=\'60\' />';";
}
echo "\n".'</script>';
exit(); // do not go futher
<?php
?>
<script language="JavaScript" type="text/javascript">
var parDoc = window.parent.document;
<?php
if ($result == 'OK') {
?>
parDoc.getElementById("picture_error").innerHTML = "";
<?php
}
else {
?>
parDoc.getElementById("picture_error").innerHTML = "<?php echo $result_msg;?>";
<?php
}
if($filename != '') {
?>
parDoc.getElementById("picture_preview").innerHTML = '<img
src=\'<?php echo $preview_url.$filename;?>\'
id=\'preview_picture_tag\'
name=\'preview_picture_tag\'
width=\'60\' />';
<?php
}
?>
</script>
exit(); // do not go futher
Surround it with the PHP code sequence relevant to your installation: <? ?> or <?php ?> depending on your configuration.

PHP if/else statement

How can I write the following statement in PHP:
If body ID = "home" then insert some html, e.g.
<h1>I am home!</h1>
Otherwise, insert this html:
<p>I'm not home.</p>
Doing it with native PHP templating:
<?php if ($bodyID==='home') { ?>
<h1>I am home!</h1>
<?php } else { ?>
<p>I'm not home!</p>
<?php } ?>
You can try using this :
$html = '';
if ( $body_id === 'home' )
{
$html .= '<h1>I am home!</h1>';
}
else
{
$html .= '<p>I\'m not home.</p>';
}
echo $html;
This will echo the html code depending on the $body_id variable and what it contains.
You can use a switch command like so:
switch($body)
{
case 'home': //$body == 'home' ?
echo '<h1>I am home!</h1>';
break;
case 'not_home':
default:
echo '<p>I'm not home.</p>';
break;
}
The default means that if $body does not match any case values, then that will be used, the default is optional.
Another way is as you say, if/else statements, but if within template / view pages you should try and use like so:
<?php if ($body == 'home'):?>
<h1>I am home!</h1>
<?php else:?>
<p>I'm not home!</p>
<?php endif; ?>
Assuming $bodyID is a variable:
<?php
if ($bodyID==='home') {
echo "<h1>I am home!</h1>";}
else {
echo "<p>I'm not home!</p>";}
?>
Personally I think that the best way to do that without refreshing and without having to set a variable (like $body or something like that) is to use a javascript code, this because "communications" between JS & PHP is a one-way communication.
<script language="javascript">
<!--
if( document.body.id === "home" ){
window.document.write("<h1>I am home!</h1>") ;
}
else{
window.document.write("<p>I'm not home!</p>") ;
}
-->
</script>
otherwise you can build a form and then take the body.id value using $_GET function... It always depends on what you've to do after you now body.id value.
Hope this will be usefull & clear.
you can try in the following way:
$body_id = "home";
if ($body_id == "home") {
echo "I am home!";
} else {
echo "I am not home!";
}
or
$body_id = "home";
if (strcmp($body_id, "home") !== 0) {
echo 'I am not home!';
}
else {
echo 'I am home!';
}
Reference:
https://www.geeksforgeeks.org/string-comparison-using-vs-strcmp-in-php/

Categories