Solved

if statement in php

Posted on 2011-02-23
12
270 Views
Last Modified: 2012-05-11
I need to place an if, then statement in my index.php page so that if the page is /apply-online/ then it invokes some javascript on that index.php

This is a wordpress site and index.php in the root is where I had the script but I need to move it off of that home page and onto an internal page which is /apply-online/

Any ideas?
0
Comment
Question by:COwebmaster
12 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 400 total points
ID: 34964843
By apply-online you mean a web browser address in upper bar ?
0
 

Author Comment

by:COwebmaster
ID: 34964858
yes
0
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 400 total points
ID: 34964900
With that you can get a page address in php
http://www.webcheatsheet.com/PHP/get_current_page_url.php
and so - if the apply-online - depending on the position of it is inside that string, you can invoke a js.
0
 

Author Comment

by:COwebmaster
ID: 34964962
I tried the following and the jquery popup loads on the home page if I set $uri to '/' but not if I set the path to /apply-online/

Any idea why?
<?php
$uri = $_SERVER['REQUEST_URI'];
if ($uri == "/apply-online/") {
      echo "<link rel='stylesheet' href='js/shadowbox/shadowbox.css' type='text/css' media='screen' />
	  <script type='text/javascript' src='js/nav.js'></script>
	  <script type='text/javascript' src='js/nav.js'></script>
	  <script type='text/javascript' src='js/jquery-1.3.2.min.js'></script>
	  <script type='text/javascript' src='js/shadowbox/shadowbox.js'></script>
	  <script type='text/javascript'>
		Shadowbox.init({
        displayNav: true,
        onClose:function(){
                top.location.href='http://domain.com/?s=';
        }
        

});

$(function() {
        if (document.URL == 'http://domain.com/') {
                setTimeout(function() {
                        //Shadowbox.init();
                        Shadowbox.open({
                                player:         'iframe',
                                content:        'test1.cfm',
                                displayNav:     true,
                                width:          '840',
                                height:         '750',
                                modal:          true
                        });
                }, 2000);
        }
});
function getCookieValue1() {
        var openUrl = '';
        var c_name = 'nowUrlIs';
        if (document.cookie.length > 0) {
                var c_start = document.cookie.indexOf(c_name + '=');
                if (c_start != -1) {
                        c_start = c_start + c_name.length + 1;
                        var c_end = document.cookie.indexOf(';', c_start);
                        if (c_end == -1)
                                c_end = document.cookie.length;
                        openUrl = document.cookie.substring(c_start, c_end);

                        setTimeout(function() {
                                //Shadowbox.init();
                                Shadowbox.open({
                                        player:         'iframe',
                                        content:        openUrl,
                                        displayNav:     true,
                                        width:          '840',
                                        height:         '750',
                                        title:          'Please click out side the frame to close it',
                                        modal:          false
                                });
                        }, 2000);
                }
        }
}
</script>";
} else {
     echo $_SERVER['REQUEST_URI'];
}
?>

Open in new window

0
 
LVL 8

Assisted Solution

by:ropenner
ropenner earned 100 total points
ID: 34964985
Something like below perhaps is what you are looking for?


<?PHP
if (strpos($_SERVER{'REQUEST_URI'},'/apply-online/') > -1) {
   print <<< endofjavascript
    <SCRIPT language=javascript>
      var dump;
      location.href='http://google.ca';
   </SCRIPT>
endofjavascript
} else {
print <<< endofhtml
  <HTML>
  <BODY>
hiya
  </BODY>
  </HTML
endofhtml
}
?>
0
 

Author Comment

by:COwebmaster
ID: 34965043
ropenner, my page broke using that.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:COwebmaster
ID: 34965059
I need to remove those double quotes around all of my code because I think it sees it all as a string.  I tried removing the double quotes but it didn't work
0
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 400 total points
ID: 34965093
$uri = $_SERVER['REQUEST_URI'];
would not be /apply-online/
You would have to search for a presence of that string in a full uri.
0
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 400 total points
ID: 34965121
Maybe using substr_ count like
if (substr_count($uri,"/apply-online/") > 0)
rather than
if ($uri == "/apply-online/")
0
 

Author Comment

by:COwebmaster
ID: 34965216
tried that but it didn't work.

So I have which works from the home page and the internal page path /apply-online/ displays at the bottom just fine.  Just can't get that darn popup to show up on that page.
<?php
$uri = $_SERVER['REQUEST_URI'];
if ($uri == "/") {
      echo "<link rel='stylesheet' href='/js/shadowbox/shadowbox.css' type='text/css' media='screen' />
	  <script type='text/javascript' src='/js/nav.js'></script>
	  <script type='text/javascript' src='/js/jquery-1.3.2.min.js'></script>
	  <script type='text/javascript' src='/js/shadowbox/shadowbox.js'></script>
	  <script type='text/javascript'>
		Shadowbox.init({
        displayNav: true,
        onClose:function(){
                top.location.href='http://domain.com/?s=';
        }
        

});

$(function() {
        if (document.URL == 'http://domain.com/') {
                setTimeout(function() {
                        //Shadowbox.init();
                        Shadowbox.open({
                                player:         'iframe',
                                content:        'test1.cfm',
                                displayNav:     true,
                                width:          '840',
                                height:         '750',
                                modal:          true
                        });
                }, 2000);
        }
});

function getCookieValue1() {
        var openUrl = '';
        var c_name = 'nowUrlIs';
        if (document.cookie.length > 0) {
                var c_start = document.cookie.indexOf(c_name + '=');
                if (c_start != -1) {
                        c_start = c_start + c_name.length + 1;
                        var c_end = document.cookie.indexOf(';', c_start);
                        if (c_end == -1)
                                c_end = document.cookie.length;
                        openUrl = document.cookie.substring(c_start, c_end);

                        setTimeout(function() {
                                //Shadowbox.init();
                                Shadowbox.open({
                                        player:         'iframe',
                                        content:        openUrl,
                                        displayNav:     true,
                                        width:          '840',
                                        height:         '750',
                                        title:          'Please click out side the frame to close it',
                                        modal:          false
                                });
                        }, 2000);
                }
        }
}</script>";
} else {
     echo $_SERVER['REQUEST_URI'];
}
?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34969765
I think WordPress may be getting in your way of understanding the PHP-related issues here.  How much experience do you have in PHP (outside of WP)?
0
 

Author Closing Comment

by:COwebmaster
ID: 35064414
Thanks!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now