Hi I content type 'site_location', /js/site_location.js, and this is on Drupal 7:
I want to run the JS file only on the 'site_location' nodes, but it also runs when I go to the edit page too. Is there any way of not running the javascript when I edit a node of content type 'site_location'?
Here is what I have:
function porto_preprocess_page(&$vars, $hook) {
if($vars['node']->type === "site_location"){
drupal_add_js($theme_path . '/js/site_location.js' , 'file');
}
}
Just messing around I found a solution.
arg(2) is 'edit' on the edit page and empty on the view page. Therefore I ended up with:
if($vars['node']->type === "site_location" && arg(2) == ''){
I still wonder though if there are other approaches to this.
Something like this should do the trick:
if ($vars['node']->type === "site_location" && path_is_admin(current_path())) {
drupal_add_js($theme_path . '/js/site_location.js' , 'file');
}
Related
I"m loading a file in start of PHP. I have declared some activity and assign them to buttons which are aligned left and right side of HTML document. On click of each button will included some Other PHP file but I want to remove the previous loaded on startup file before including any other file.
Here is my code
<?php
// php file on startup of file
include("dashboard.php");
$activity = $_REQUEST['activity'];
if($activity) {
if($activity == 'addMember'){
include("addMember.php");
remove("dashboard.php");
}
if($activity == 'dashboard'){
}
if($activity == 'issueBooks'){
include("issueBooks.php");
}
if($activity == 'returnBooks'){
include("returnBooks.php");
}
<?php
I tried with
if($activity == 'addMember'){
include("addMember.php");
remove("dashboard.php");
}
But since I"m very new to PHP, it didn't work as expected.
Any Help anyone.
That isn't how include works. When you include a file, the contents of that file are executed at that point in the code. There's no straightforward way to undo that.
It looks like you want the dashboard activity to be the default view if no activity has been selected yet. To do that, you can just include it only when there is no activity value in request, or when the dashboard activity has been specifically requested.
if (empty($_REQUEST['activity']) || $_REQUEST['activity'] == 'dashboard') {
include 'dashboard.php';
}
I am trying to create a function that checks part of the URL on a page and then either shows or hides a nav bar based on the results. I can get the function to work if it checks for only one string but adding different strings in an OR statement doesn't do anything.
Below is an example of the function working with a single condition...
<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $SERVER['REQUEST_URI'];
if (strpos($url,'/login') == false){ ?>
<nav>Navbar</nav>
<?php } ?>
This correctly prevents the navbar from showing up on the "login" page. But if I also want the navbar to not show up on the "register" page and do something like this...
<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $SERVER['REQUEST_URI'];
if (strpos($url,'/login') == false || strpos($url,'/register') == false){ ?>
<nav>Navbar</nav>
<?php } ?>
...the functionality stops working entirely and the navbar shows up on all pages.
So how can I refine my OR statement so that it works properly? Thanks!
Logic flaw. Change the or condition to and
if(strpos($url,'/login')==false || strpos($url,'/register')==false)
Should be
if(strpos($url,'/login')===false && strpos($url,'/register')===false)
You want a && ... not a ||. Because you want the $url to not contain "/login" AND "/register". Otherwise it'll always be true.
try with () like this:
if ( (strpos($url,'/login') === false) || (strpos($url,'/register') === false) ){ ?>
I am working with WordPress plugin, and I am doing following.
Create a new button in comments.
Click here
Now on magic.php I am calling update_comment_meta.
$decode_str=base64_decode($_GET['en']);
if(substr($decode_str, 0,4) != "http"){
$decode_str = "http://".$decode_str;
}
if(is_numeric($_GET['cid']) && is_numeric($_GET['pid']) ){
update_comment_meta( $_GET['pid'] , 'pinned', $_GET['cid'] );
}
header("Location: ".$decode_str);
?>
It givens me error, update_comment_meta is not defined.
What I think is I might need to load wp-load.php, because magic.php all alone doesn't know it is a part of wordpress.
I don't want to do AJAX. I have also read loading wp-load.php within your file is not a good idea.
Thanks.
THE PROBLEM
The problem I've having is I can't override the current page with include.
if ($LS::getUser('clan') || isset($_GET['clan']) && !isset($_GET['search'])) {
include("res/templates/clan-overview.php");
} else {
include("res/templates/clan-search.php");
}
I don't get why I can't change the page with clan-search.php because I said that if $_GET['search'] is NOT set execute the else statement. But for some weird reason I'm still getting true even though I said that if the user has a CLAN || $_GET['clan'] AND $_GET['search'].
What I've Tried
That code I put at the top is actually the modified version of my original code which I thought would fix the problem. If you are interested in looking at the original code which I don't think will help you here it is:
if ($LS::getUser('clan') || isset($_GET['clan'])) {
include("res/templates/clan-overview.php");
} else if (isset($_GET['search'])) {
include("res/templates/clan-search.php");
}
#Drew pierce gave you first response as you need to wrap the OR parts as a group. try this:
if (($LS::getUser('clan') || isset($_GET['clan'])) && !isset($_GET['search'])) {
I've been trying to get user input from a Javascript prompt into a PHP function and have run into a lot a walls doing so. I am at a loss trying jQuery's $.post method- as the PHP simply does not want to execute, not sure why.
Anyways, here is a gist of what I am doing at the moment:
1 A project and its data are loaded from a database- this info is displayed in a table.
2 All data in the table is editable via Javascript prompt(), the code I am using for this is below:
<div id="lvl3"><?php echo $fetchdata['name']; ?></div>
The above work as such: lvl3 is a tag for font styling; blank href to make it act as a link; popupprompt is the prompt function I made, it takes one argument, the 'type' or what is being edited (1 for project name, 2 for project description, ect); return false so the page doesn't reload; php echo to display project data in the table.
3 Once the user clicks the object above- it executes a javascript function called popupprompt taking an argument of 'type', or what project info is being changed. the code for this function is below:
function popupprompt(type) {
switch(type)
{
case 1:
var name = prompt("Project Name:", "");
if (name != null && name != "")
{
//Change Project Name
var getname = name;
var gettype = type;
$.post("edit.php", { type: gettype, name: getname });
} else if (name == "") {
senderror("Please enter a valid Project Name");
} else {
//Prompt canceled
sendnotification('Canceled my ass!');
}
break;
case 2:
//Description?
case 3:
//Version?
case 4:
//Release?
default:
alert("There was an error processing your request.");
break;
} }
Th issue I am having in this function is that nothing in edit.php is executed- and i haven't the slightest clue why. Also, i've had to change the brackets around so it shows properly in the code box- so don't mind those.
4 Anyways, now user input is posted to edit.php- which doesn't work, but i'll post it anyways:
<?php
$type = $_POST['type'];
$name = $_POST['name'];
switch($type) {
case 1:
dbconnect();
$urlext = geturlext();
$authenticated = isauthenticated();
if ($authenticated == false)
{
echo("<script>senderror('Access denied');</script>");
} else {
//Escape and trim input
$input = trim($input);
$input = mysql_real_escape_string($input);
$update = "UPDATE 'projects' SET 'name' = '$input' WHERE 'name' = '$urlext'";
$updatequery = mysql_query($update) or die(mysql_error());
echo("<script>sendnotification('Project Name updated');</script>");
}
break;
default:
break;
}
?>
Again had to move some brackets around. But anyways- this function is supposed to update the data in the database- however, it instead does nothing. I've placed alerts in the beginning and they are never called.
Anyways, long story short- if you know what i'm doing wrong please enlighten me, also, if there is a better way to do this- please let me know!
I appreciate all help,
Thanks
Figured it out after a good week of setting it aside. Took a long look at it in firebug, rooted out a typo that was causing most of my issues and also a MySQL syntax error.
I don't know why, but my custom error reporting system does not work when called from a file in this manner. It might be another thing i'm overlooking but firebug sure helped.
Thanks to those who tried to help <3