2017-07-14 97 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

相关问题