2013-01-17 51 views
0

所以即時通訊(試圖)在基礎建立一個網站,即時通訊與下一部分掙扎。如何通過url傳遞變量,根據變量更改dd和li的類?

首先讓我存在(的一部分)的代碼第一

<dl class="contained tabs"> 
    <dd class="active"><a href="#contactForm">Neem contact op</a></dd> 
    <dd><a href="#hoofdleiders">Hoofdleiders</a></dd> 
    <dd><a href="#leiders">Leiders</a></dd> 
    <dd><a href="#staf">Staf</a></dd> 
    </dl> 

<ul class="tabs-content contained"> 

    <li id="contactFormTab" class="active"> 
     <div class="row collapse"> 
     <div class="two columns"><label class="inline">Naam</label></div> 
     <div class="ten columns"><input type="text" name="naam" id="naam" placeholder="Naam" value="' . (isset($_POST['naam']) ? htmlspecialchars($_POST['naam']) : '') . '" /> 
     </div> 
     <div class="row collapse"> 
     <div class="two columns"><label class="inline">Email</label></div> 
     <div class="ten columns"><input type="text" name="mail" id="mail" placeholder="[email protected]" value="' . (isset($_POST['mail']) ? htmlspecialchars($_POST['mail']) : '') . '" /> 
     </div> 
     <div class="row collapse"> 
     <div class="two columns"><label class="inline">Onderwerp</label></div> 
     <div class="ten columns"><input type="text" name="onderwerp" id="onderwerp" placeholder="onderwerp" value="' . (isset($_POST['mail']) ? htmlspecialchars($_POST['onderwerp']) : '') . '" /> 
     </div> 

     <label>Wat wilt u zeggen?</label> 

     <textarea id="bericht" name="bericht" rows="3">' . (isset($_POST['bericht']) ? htmlspecialchars($_POST['bericht']) : '') . '</textarea><br /> 

     <button type="submit" name="submit" class="radius button">Verstuur</button> 

    </li> 

<li id="hoofdleidersTab"> 
     <ul class="block-grid five-up"> 
     <li><a href="mailto:[email protected]"><img src="http://placehold.it/200x200&text=[persoon]" /><br />Naam 1</a></li> 
     <li><a href="mailto:[email protected]"><img src="http://placehold.it/200x200&text=[persoon]" /><br />Naam 2</a></li> 

     </ul> 
    </li> 
    <li id="leidersTab"> 
     <ul class="block-grid five-up"> 
     <li><a href="mailto:[email protected]"><img src="http://placehold.it/200x200&text=[persoon]" /><br />Naam 1</a></li> 
     <li><a href="mailto:[email protected]"><img src="http://placehold.it/200x200&text=[persoon]" /><br />Naam 2</a></li> 

     </ul> 
    </li> 
    <li id="stafTab"> 
     <ul class="block-grid five-up"> 
     <li><a href="mailto:[email protected]"><img src="http://placehold.it/200x200&text=[persoon]" /><br />Naam 1</a></li> 
     <li><a href="mailto:[email protected]"><img src="http://placehold.it/200x200&text=[persoon]" /><br />Naam 2</a></li> 

     </ul> 
    </li> 

所以DD類「活動」和利類「活動」,使標籤高亮顯示。

我的問題是,如何才能讓這個類轉換到另一個項目(可以說:第二個),另一個李ID(讓也說在網址中使用過的變量,第二個(hoofdleiders)?

這是一個接觸的形式的一部分,我希望能夠到陸地上的人的權利,他們需要的人。

如果有什麼不清楚的地方,請與我裸露,這是我在計算器上的第一個問題。

+0

URL參數/變量可能會以PHP形式出現:$ _GET ['']; – ethrbunny

+0

你失去了我的存在....... – defiancenl

+0

dd和li現在是硬編碼的活動,我需要它們根據url中的字符串變成活動的,比如說contact.php?誰= Hoofdleiders – defiancenl

回答

0

我不推薦以這種方式實現。你應該使用像MVC這樣的框架或模式來更好地管理你的代碼。但是,就你而言,這可能是一個簡單明瞭的解決方案。

存放在具有相同的名稱作爲選項卡分離PHP文件每個標籤,例如,該staf.php文件將是這樣的:

<ul class="block-grid five-up"> 
    <li><a href="mailto:[email protected]"><img src="http://placehold.it/200x200&text=[persoon]" /><br />Naam 1</a></li> 
    <li><a href="mailto:[email protected]"><img src="http://placehold.it/200x200&text=[persoon]" /><br />Naam 2</a></li> 
</ul> 

主要contact.php會是這樣

<?php 
# list all available tabs 
$tabs = array(
    'contactform' => 'Contact form', 
    'hoofdleiders' => 'Hoofdleiders', 
    'leiders' => 'Leiders', 
    'staf' => 'Staf', 
); 

# get the selected Tab from the url, if no tab is specified use the first tab 
$selectedTab = filter_input(INPUT_GET, 'tab'); 
if (!in_array($selectedTab, $tabs)) 
    $selectedTab = $tabs[0]; 
?> 
<dl class="contained tabs"> 
    <?php foreach($tabs as $name => $label):?> 
    <dd <?php echo ($tabName == $selectedTab ? "class='active'" : ""; ?>><a href="#<?php echo $name ?>"><?php echo $label ?></a></dd> 
    <?php endforeach; ?> 
</dl> 

<ul class="tabs-content contained"> 
<?php foreach($tabs as $tabName): ?> 
    <li id="<?php echo $tabName; ?>" <?php echo ($tabName == $selectedTab ? "class='active'" : ""; ?>> 
     <?php require_once ($tabName . ".php"); ?> 
    </li> 
<?php endforeach; ?> 
</ul> 

在這種情況下,contact.php和每個li的所有php文件必須在相同的der並且可以在這樣的URL中調用contact.php文件contact.php?tab=staf

+0

能否詳細說明爲什麼你的方法更好,或者更好,爲什麼他的方法不好? – defiancenl

+0

即時通訊從這裏可以學習的答案不只是解決我的問題;) – defiancenl

+0

是的,用我的方法,你可以很容易地編輯標籤列表和每個標籤的內容通過'$ tabs'變量和PHP文件以及維護你的'contact.php'文件。您也可以決定什麼是默認選項卡,當用戶只使用'contact.php'而沒有任何參數時我們該做什麼。還有一點是,當獲得$ _GET中的參數值時,使用'filter_input'可以幫助您避免一些安全攻擊(只是**一些**):D 但是,我認爲您應該閱讀** MVC **來獲得更好的實現PHP應用程序的方式。 –

0

如果你不想循環項目,你可以使用IF子句添加活動狀態:

<dl class="contained tabs"> 
<dd<?php if(!isset($_GET['tab']) || $_GET['tab']=='contactform') echo ' class="active"'; ?>> 
     <a href="#contactForm?tab=contactform">Neem contact op</a> 
</dd> 
<dd<?php if($_GET['tab']=='hoofdleiders') echo ' class="active"'; ?>> 
     <a href="#hoofdleiders?tab=hoofdleiders">Hoofdleiders</a> 
</dd> 
<dd<?php if($_GET['tab']=='leiders') echo ' class="active"'; ?>> 
     <a href="#leiders?tab=leiders">Leiders</a> 
</dd> 
<dd<?php if($_GET['tab']=='staf') echo ' class="active"'; ?>> 
     <a href="#staf?tab=staf">Staf</a> 
</dd> 
</dl> 

我將!isset($_GET['tab']) ||添加到第一個選項卡,以便此選項卡是默認值(如果未設置$_GET['tab'])。

然後每個LI添加相同的PHP:

<li id="contactFormTab"<?php if(!isset($_GET['tab']) || $_GET['tab']=='contactform') echo ' class="active"'; ?>> 

... 

<li id="hoofdleidersTab"<?php if($_GET['tab']=='hoofdleiders') echo ' class="active"'; ?>> 

... 

<li id="leidersTab"<?php if($_GET['tab']=='leiders') echo ' class="active"'; ?>> 

... 

<li id="stafTab"<?php if($_GET['tab']=='staf') echo ' class="active"'; ?>> 
+0

thnx,即將去檢查你在這裏做了什麼:) – defiancenl

+0

嗯審查後,它是如何改變每個項目的li id來獲得「積極」的類?我得到你在這裏做的部分爲dd – defiancenl

+0

查看我的編輯,你添加了與DD元素相同的PHP代碼給LI的 – jtheman

0

如果你wan't做渲染,你可以使用PHP類的頁:

<? if ($_GET['param_name'] == 'somecondition'):?> 
<html do something> 
<?endif;?> 

或者你也可以使用jQuery後使頁面渲染與用戶動作交互:

function getURLParameter(name) { 
return decodeURI(
    (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] 
); 
} 

而且你可以使用這個函數來獲得url中的參數並在javascript/jquery上做一些邏輯來做一些很酷的事情。如果你需要更多的東西讓我知道。