简体   繁体   中英

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

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>

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

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 id="option1" value='1'>option 1</option>

I've made you a jQuery demo, see snippet below:

 $('.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 :

 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

And on the page you will be redirected you can get the value used as a $_GET parameter using:

 echo $_GET['did'];

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

$get_did = $_GET['did'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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