2017-02-28 56 views
1

的JavaScript如何在JavaScript中传递一个值并在另一页上获取它?

function resultfunction() { 
    var result = document.getElementById("result"); 
    window.location.href = "deleteresult.php?did="+result; 
} 

PHP

<select id="result"> 
<?php 
    include 'model.php'; 
    $rs=new database(); 
    $res=$rs->result(); 
    while($row=mysql_fetch_array($res)){ 
     $did=$row["id"]; 
     $dterm=$row["term"]; 
?>   
<option id="<?php echo $dterm;?>"><?php echo $dterm;?></option> 
<?php } ?> 
</select> 
<a href="#" onclick="resultfunction()" class="btn dropdown-toggle">Delete </a> 

这是我上删除的点击我想,采取的JavaScript所选的选项的ID,并把它带到下一个页面的代码....以及如何在下一页得到它..我想在下一页使用它作为一个PHP变量?

+0

$ _SESSION ['did'] = $ did;你可以在所有页面上使用它。欲了解更多信息获取php会话文档的... –

回答

1

首先,你需要对option像一个值:

<option id="option1" value='1'>option 1</option> 

我做你jQuery演示,看到片段如下:

$('.btn').on('click', function(){ 
 
    var result = $("#result").val(); 
 
    console.log(result); 
 
    //I've commented this line just for demonstrating purpose 
 
    //window.location.href = "deleteresult.php?did=" + result; 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select id="result"> 
 
    <option id="option1" value='1'>option 1</option> 
 
    <option id="option2" value='2'>option 2</option> 
 
    <option id="option3" value='3'>option 3</option> 
 
</select> 
 
<a href="#" class="btn dropdown-toggle">Delete </a>

一个JavaScript替代方案是使用addEventListener,而不是过时的onclick

document.getElementById("btn").addEventListener("click", function(){ 
 
    var select = document.getElementById("result"); 
 
    var selectedOption = select.options[select.selectedIndex].value; 
 
    console.log(selectedOption); 
 
    //I've commented this line just for demonstrating purpose 
 
    //window.location.href="deleteresult.php?did=" + selectedOption; 
 
});
<select id="result"> 
 
    <option id="option1" value='1'>option 1</option> 
 
    <option id="option2" value='2'>option 2</option> 
 
    <option id="option3" value='3'>option 3</option> 
 
</select> 
 
<a href="#" class="btn dropdown-toggle" id='btn'>Delete </a>

其他选项是为使用JavaScript的localStorage()或PHP的$_SESSION

你会被重定向你可以使用get作为$ _GET参数的值在页面上:

echo $_GET['did']; 
+0

还有一件事我想问我什么时候从数据库中提取术语有很多重复的值,但我只需要一次,无论它在数据库中重复多少次 –

+0

@YogeshArya,我不知道你为什么想要做这样的事情,这取决于你的数据库表的结构,但我认为你可以改变你的'SELECT'查询如下所示:'SELECT DISTINCT field_name FROM table_name;'这样你应该返回唯一值。 – Ionut

+1

@lonut致谢bro –

1

使用此:

<select id="result"> 
 
    <option id="option1">option 1</option> 
 
    <option id="option2">option 2</option> 
 
    <option id="option3">option 3</option> 
 
</select> 
 
<a href="#" onclick=" resultfunction()" class="btn dropdown-toggle">Delete </a> 
 
<script> 
 
    function resultfunction() { 
 
    var select = document.getElementById("result"); //<---get the select 
 
    var result= select.options[select.selectedIndex].value; // <--selectedIndex will let you get the value. 
 
    //window.location.href="deleteresult.php?did=" +result; 
 
    console.log(result); 
 
    } 
 
</script>

+0

我想问一个更多的事情,当我从数据库中提取的术语有很多重复的值,但我只希望它只有一次,无论它在数据库中重复多少时间 –

1

及使用该deleteresult.php页变量u应该有这样

一些代码
$get_did = $_GET['did']; 
+0

还有一件事我想问,当我从数据库中提取这个术语时,有很多重复的值,但我只希望它只有一次,无论它在数据库中重复多少次 –