简体   繁体   English

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

[英]how to pass a value in javascript and get it on another page?

JavaScript JavaScript的

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

PHP 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>

this is my code on the click of delete i want that javascript take id of selected option and take it to next page.... and how to get it on next page .. i want to use it as a php variable on next page ???? 这是我的代码点击删除我希望javascript采取所选选项的ID并将其带到下一页....以及如何在下一页上获取它...我想在下一页上将它用作php变量????

Use this: 用这个:

 <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> 

First you need to have a value on the option like: 首先,你需要在option上有一个值,如:

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

I've made you a jQuery demo, see snippet below: 我已经为你做了一个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> 

A Javascript alternative would be to use addEventListener instead of the deprecated onclick : 一个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> 

Other options would be to use either Javascript's localStorage() or PHP's $_SESSION 其他选项是使用Javascript的localStorage()或PHP的$_SESSION

And on the page you will be redirected you can get the value used as a $_GET parameter using: 在页面上,您将被重定向,您可以使用以下方法获取用作$ _GET参数的值:

 echo $_GET['did'];

并且为了在deleteresult.php页面上使用该变量,你应该有一些像这样的代码

$get_did = $_GET['did'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM