2017-07-14 84 views
0

我想通過使用帶有href的標籤將值傳遞給彈出窗口,當我單擊一個 將值表單數據庫獲取到隱藏層如何通過js傳遞值。如何將值表單數據庫傳遞到彈出窗口

的標記碼

<a 'href=index.php?id=3'></a> 

隱層

<div class='wrap'> 
<div class='content'> 
<h2>Well Hello!</h2> 
<p> 
<? if (isset($_GET['id'])){   
$id = $_GET['id']; 
echo $id ;} ?> 
</p> 
</div> 
</div> 

js代碼

$('a').on('click', function(){ 
$('.wrap, a').toggleClass('active'); 
    return false; 
}); 
+2

[結帳](https://developer.mozilla.org/en-US/docs/Web/HTML/元素/ a)''標籤的正確語法! – Jeff

+0

請將腳本的名稱添加到您的代碼中。目前還不清楚哪個代碼在一個文件中 – Jeff

+0

也應該是'<?php'。它可能在你的corrent配置中工作,但可能會打破其他。 – Jeff

回答

0

如果你想這樣做的JavaScript的方式,看到這個例子。

$(document).ready(function() { 
 

 
    $('a.toggle-wrap').on('click', function(e) { 
 
    e.preventDefault(); // prevent default behaviour of the link that would reload the page 
 
    $('.wrap, a.toggle-wrap').removeClass('active'); // remove class active on every link clicking 
 
    var target_id = $(this).attr("data-id"); //get the desired id from link 
 
    var wrap_element = $('.wrap[data-target=' + target_id + '] p'); 
 
    var link_element = $('a[data-id=' + target_id + ']'); 
 
    link_element.toggleClass('active'); 
 
    $.ajax({ 
 
     type: "GET", 
 
     url: "someOtherScriptThatOnlyOutputsResults.php", 
 
     data: "id="+target_id, 
 
     success: function(resultData) { 
 
     wrap_element.html(resultData).toggleClass('active'); // only toggle desired ids 
 
     }, error: function() { 
 
     wrap_element.html('Could not load data').toggleClass('active'); 
 
     } 
 
    }); 
 

 
    }); 
 

 
});
.wrap { 
 
    display: none; 
 
} 
 

 
.wrap.active { 
 
    display: block; 
 
} 
 

 
a { 
 
    color: green; 
 
} 
 

 
a.active { 
 
    color: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<a class="toggle-wrap" data-id="3">Link 3</a> 
 
<a class="toggle-wrap" data-id="4">Link 4</a> 
 

 
<div class="wrap" data-target="3"> 
 
    <div class="content"> 
 
    <h2>Well Hello content 3!</h2> 
 
    <p>Your db content related to id 3 from database, using php. 
 
    </p> 
 
    </div> 
 
</div> 
 

 
<div class="wrap" data-target="4"> 
 
    <div class="content"> 
 
    <h2>Well Hello content 4!</h2> 
 
    <p>Your db content related to id 4 from database, using php. 
 
    </p> 
 
    </div> 
 
</div>

添加標識將隱藏包裹(S)和鏈接(S),並與合作。通過使用js代碼片段中的查詢,您可以定位某些HTML標籤。使用CSS顯示來隱藏和顯示您的包裝標籤。

創建一個新的PHP文件someOtherScriptThatOnlyOutputsResults.php獲取和返回數據:

<?php 
if(isset($_GET['id'])) { 

    $pdo = new PDO('mysql:host=someHost;dbname=someDatabase', 'someUser', 'somePass'); 
    $statement = $pdo->prepare("SELECT columnWithContent FROM yourContentTable WHERE id = ?"); 
    $statement->execute(array($_GET['id'])); 
    $row = $statement->fetch(); 
    $content = $row['columnWithContent']; 
    echo $content; 

} 
?> 
+0

感謝兄弟,但我想通過ID得到價值形式分數 – Jhonwick

+0

你的意思是在飛行中嗎?在你點擊鏈接,然後加載一些數據到包裝數據? – Yolo

+0

雅是我需要的 – Jhonwick

相關問題