1

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

1
  • $_SESSION['did'] = $did; and you can use it on all pages. For more info get php session doc's... Commented Feb 28, 2017 at 9:53

3 Answers 3

1

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'];
Sign up to request clarification or add additional context in comments.

2 Comments

one more thing i want to ask when i am fetching the term from database there is lot of duplicate values but i want it only single time no matter how much time it is repeated in databse
@YogeshArya, I don't know why you want to do such thing, that depends on the structure of your database tables but I think you can change your SELECT query something like: SELECT DISTINCT field_name FROM table_name; This way you it should return unique values.
1

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>

1 Comment

one more thing i want to ask when i am fetching the term from database there is lot of duplicate values but i want it only single time no matter how much time it is repeated in databse
1

And for use that variable on deleteresult.php page u should have some code like this

$get_did = $_GET['did'];

1 Comment

one more thing i want to ask when i am fetching the term from database there is lot of duplicate values but i want it only single time no matter how much time it is repeated in databse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.