Old 19-11-2007, 04:43   #1 (permalink)
key_uk
Senior Member
 
key_uk's Avatar
 
Join Date: Mar 2006
Location: Shropshire
Posts: 125
PHP GET and switch()

Hello,

My website is here

Basically I have my layout linked together using a index.php using code like this:

<body>
<div id="maincontainer">
<div id="topsection"><?php include("http://www.indie-design.co.uk/topsection.html"); ?></div>
<div id="contentwrapper"><?php include("page2.php"); ?></div>
<div id="contentwrapper"><?php include("searchresults.php"); ?></div>
<div id="leftcolumn"><?php include("http://www.indie-design.co.uk/leftcolumn.html"); ?></div>
<div id="rightcolumn"><?php include("http://www.indie-design.co.uk/rightcolumn.html"); ?></div>
<div id="footer"><?php include("http://www.indie-design.co.uk/footer.html"); ?></div>
</div>
</body>

My main content page is page2.php, I have a search form in my leftcolumn.html and the results appear in the searchresults.php which is placed where my content page is (page2.php). At the moment it does this but still keeps the page2.php in the same place and puts the searchresults.php just under it. I wanted the page2.php to load in the content place, then when a search is made, the searchresults.php, replaces the page2.php.

So someone told me to add a GET and switch() into my index.php so this is what I added:

</head>

<body>
<div id="maincontainer">
<div id="topsection"><?php include("http://www.indie-design.co.uk/topsection.html"); ?></div>

<div id="contentwrapper"><?php
$page = (isset($_GET['page'])?$_GET['page']:'';
switch ($page)
{
case 'search':
include("searchresults.php");
break;
case 'main':
default:
include("page2.php");
break; //this isn't necessary
}
?></div>

<div id="leftcolumn"><?php include("http://www.indie-design.co.uk/leftcolumn.html"); ?></div>
<div id="rightcolumn"><?php include("http://www.indie-design.co.uk/rightcolumn.html"); ?></div>
<div id="footer"><?php include("http://www.indie-design.co.uk/footer.html"); ?></div>
</div>
</body>
</html>

But now when I go to the page, its blank,

any ideas?

Thanks
  Reply With Quote
Old 19-11-2007, 04:50   #2 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,962
Put a hidden field in the form

<input type="hidden" name="showresults" value="1">

In content section

if($showresults){include("searchresults.php"); }

else{include("page2.php");}
__________________
  Reply With Quote
Old 19-11-2007, 05:57   #3 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,610
The method I use, which does the exact same thing as what datahound said, but doesn't require the hidden field, is to give the submit button a name and value. For example:

Code:
<input type="submit" name="submit_form" value="submit" />

Then in your content section, add this code:

Code:
if($_POST['submit_form']) { include("searchresults.php"); } else { include("page2.php"); }

(note: the above code assumes that your form is set to pass the results through POST. If it is set to GET, then change POST to GET in the code)

This works because by adding a name and value to the submit button, the value is passed along with the contents of the form. Since that value can only be passed if the form has been submitted, it will output the results if the form has been submitted, or page2.php if it hasn't. The actual value of the button doesn't matter whatsoever. All that matters is that it has a value, as the code above only checks for a value, not what that value is.
  Reply With Quote
Old 19-11-2007, 10:55   #4 (permalink)
key_uk
Senior Member
 
key_uk's Avatar
 
Join Date: Mar 2006
Location: Shropshire
Posts: 125
Thanks both for your help, I managed to get it working using the ways you shown.

Thanks again.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8