SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how do I change an include file when clicking on a link?

    Hi,

    I was wondering if there is a way of changing a php pages include file when you click on a certain link on the page.

    For example, if I have 3 links called Web, Products, and Images. I was wondering that if when I clicked on the Web link then could it bring up an include file with my Web search box on and if I clicked on the Products link could that bring up the include file with my Products search box on

    Could this be done.

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    Baltimore, MD
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No. You could have those links pop up a separate window with another php script (i.e. a link on index.php creates a popup window containing the file search.php), but once a php page has been processed and displayed to the user, it becomes a static HTML page and cannot be changed with additional php code.

    (...notwithstanding sweet but way-too-experimental things like JPSpan)

  3. #3
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    From the sounds of it, this could be done either with frames or with a combination of PHP and JavaScript (which may or may not involve XmlHTTPRequest or JPSPAN).

    It could not be done with just PHP however.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anybody know where I can get a code/script that will do something like that! I would like it to do something like the following:

    When I click on a certain link by the search box, it will then change the search box. For example, if I click on the "Web" link it will then change the search box so I can search the Web. If I then click on the "Products" link it will then change the search box so I can search for Products.

    All the search boxes should change when clicking on the link, but the main page that the search area is on should not change.

    Could someone please help me on this!

    Thanks!

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Lytham
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could link back to the same page with a variable and then use if
    statements to set the include file.

    e.g. if you are on index.php

    set your link to something like

    index.php?page=web
    index.php?page=images
    index.php?page=products

    then

    <? if ($_GET['page'] == "web") {
    include("websearch.php");
    } else if ($_GET['page'] == "products") {
    include("productsearch.php");
    } etc...
    ?>

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that, but I don't think that will work with the kind of site that I am building as I need the search box feature to be on every page of my site and I will be having over 1,000,000 pages on my site soon. This will mean that I will have to change every url of every page in my site so that it can work with the if statements.

    Thanks for the suggestion anyway. I think I really need some sort of javascript mixed with php to make this work.

    Have you seen Yahoo!'s search box. That is something that I want. I don't want my search box to be like that, but I want it so when you click on one of their search box category options the search box changes so that you can search through that category that you had choosen without the main page changing when you choose the category option.

  7. #7
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not make a link to the same page + a get variable gosearch
    "<a href='{$_SERVER['PHP_SELF']}?gosearch=product'>go search product</a>

    and somewhere in your page (say in a cell)
    you include 'displaySearch.php' ;
    The 1st thing which this script would do before display is checking it has'nt no $_GET['gosearch']
    If yes, commute to this mode of search first.

    A brutal link this way is loosing the data allready input but not submited.
    So that should be refined whith a javascript submit with target this link + get.

    Thus you can go thru hidden variable.
    OK it's just ideas ....
    +
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  8. #8
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    Baltimore, MD
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How much do you know about CSS and the domain object model (DOM)? I think the best way to do what you want to do would be to use styled <div> tags and Javascript. Here's an example; save it to a file and take a look at it in your browser. Note that the CSS and Javascript I used here could be significantly cleaner, but I wanted to make it simple so you could see what was going on.

    HTML Code:
    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Test</title>
    <style type="text/css">
    	#search-web {
    		display: block;
    		border: 1px solid #000;
    		height: 150px;
    		width: 200px;
    	}
    	#search-images {
    		display: none;
    		border: 1px solid #000;
    		height: 150px;
    		width: 200px;
    	}
    	#search-products {
    		display: none;
    		border: 1px solid #000;
    		height: 150px;
    		width: 200px;
    	}
    </style>
    <script type="text/javascript">
    	function search(field) {
    		// Make everything invisible; then make the specified box visible.
    		// This is a VERY sloppy way to do this, but it's probably the best way to demonstrate it.
    		document.getElementById('search-web').style.display = "none";
    		document.getElementById('search-images').style.display = "none";
    		document.getElementById('search-products').style.display = "none";
    		
    		// Make the specified box visible
    		document.getElementById('search-'+field).style.display = "block";
    	}
    </script>
    </head>
    
    <body>
    
    <div id="search-web">Search the web!</div>
    
    <div id="search-images">Search images!</div>
    
    <div id="search-products">Search products!</div>
    
    <div id="links">Search: <a href="javascript:search('web');">Web</a> | <a href="javascript:search('images');">Images</a> | <a href="javascript:search('products');">Products</a></div>
    
    </body>
    
    </html>
    Specifying a document type and using standards-compliant code left as an exercise for the reader.

    Just insert your php code into each of those three <div>s, and you should be all set.

  9. #9
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that. I have tried it but it doesn't seem to be working. It says that there is a javascript error. It also says that this error is Line: 1, Char: 1, Error: Object Expected.

    Quote Originally Posted by graedus_dave
    How much do you know about CSS and the domain object model (DOM)? I think the best way to do what you want to do would be to use styled <div> tags and Javascript. Here's an example; save it to a file and take a look at it in your browser. Note that the CSS and Javascript I used here could be significantly cleaner, but I wanted to make it simple so you could see what was going on.

    HTML Code:
    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Test</title>
    <style type="text/css">
    	#search-web {
    		display: block;
    		border: 1px solid #000;
    		height: 150px;
    		width: 200px;
    	}
    	#search-images {
    		display: none;
    		border: 1px solid #000;
    		height: 150px;
    		width: 200px;
    	}
    	#search-products {
    		display: none;
    		border: 1px solid #000;
    		height: 150px;
    		width: 200px;
    	}
    </style>
    <script type="text/javascript">
    	function search(field) {
    		// Make everything invisible; then make the specified box visible.
    		// This is a VERY sloppy way to do this, but it's probably the best way to demonstrate it.
    		document.getElementById('search-web').style.display = "none";
    		document.getElementById('search-images').style.display = "none";
    		document.getElementById('search-products').style.display = "none";
    		
    		// Make the specified box visible
    		document.getElementById('search-'+field).style.display = "block";
    	}
    </script>
    </head>
    
    <body>
    
    <div id="search-web">Search the web!</div>
    
    <div id="search-images">Search images!</div>
    
    <div id="search-products">Search products!</div>
    
    <div id="links">Search: <a href="javascript:search('web');">Web</a> | <a href="javascript:search('images');">Images</a> | <a href="javascript:search('products');">Products</a></div>
    
    </body>
    
    </html>
    Specifying a document type and using standards-compliant code left as an exercise for the reader.

    Just insert your php code into each of those three <div>s, and you should be all set.

  10. #10
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    Baltimore, MD
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm - did you just copy the whole thing? How are you viewing it? It works fine for me in IE6 and Firefox (although it renders weird in FF because there's no doctype).

  11. #11
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah,

    I did copy and paste it straight into notepad and I tried viewing it in Netscape 7.1 and also IE but it just doesn't work. I had even tried copying and pasting it again, but still no joy.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •