2017-02-28 23 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

還有一件事我想問,當我從數據庫中提取這個術語時,有很多重複的值,但我只希望它只有一次,無論它在數據庫中重複多少次 –

相關問題