3

peace be with you all, I am retrieving some values from database and displaying in a drop down list using ajax and php.After selecting a field from drop down list(i.e. one of $row['course_codes']) I want to send the primary key contained in $courseId to another file for further processing after clicling the submit button. I am getting the values in drop down as expected but i can not pass the id to another file. Here is my code used to retrieved data in drop down list.

<?php
    $qd = $_GET['q'];
$q = mysql_real_escape_string($qd);
$branch = $_SESSION['session_branch'];
$_SESSION['sem'] = $q;
$query = "select *FROM course_details WHERE branch='" . $branch . "' AND sem = '" . $q . "'";               
    $run = mysql_query($query) or die($query."<br/><br/>".mysql_error()); 
$num = mysql_numrows($run); 
?>

<select class="action" name='id'>
<option name="usn" value="" class="displayUsn">-- Select Course --</option>
<?php
            while($row = mysql_fetch_assoc($run)){
                $courseId = $row['course_id'];
                echo "<option class='displayRec' value = 'addToTemp.php?id=$courseId'" . ">" . $row['course_codes'] . "</option>"; 
            }   
            echo "</select>";
            if($num > 0){
                echo " ";
                echo "<input type='button' onClick=\"window.location.href='addToTemp.php'\" value='Add' id='id'>";          
            }else{
                echo "No courses available";
            }
            mysql_close($bd);
?>

in addToTemp.php i am trying to get the id as follows:

if(isset($_GET['id'])){
$getId = $_GET['id'];
}

but id does not contained any value. please help.

user3001644
  • 33
  • 1
  • 4

2 Answers2

0

You can use a form instead of linking to another page. When you link to another webpage, you will need to get all the data using javascript and pass it as to a new page. For this, have a look at this: (Pass entire form as data in jQuery Ajax function). If it does not matter to you that a new page is loaded, you can use a form, as described here.

This is the index.php, whereby I left out the first php tag:

<form method="get" id="myform" enctype="multipart/form-data" action="addToTemp.php">
<input type="hidden" name="courseID" value="<?php echo $courseID; ?>">
<select class="action" name="id">
<option value="select" class="displayUsn">-- Select Course --</option>
<?php
        while($row = mysql_fetch_assoc($run)){
            $courseId = $row['course_id'];
            echo "<option class='displayRec' value = '".$courseId."'>" . $row['course_codes'] . "</option>"; 
        }   
        echo "</select>";
        if($num > 0){
            echo "<input type=\"button\" onClick=\"document.getElementById('myform').submit();\" value=\"Add\" id=\"id\">";
        }else{
            echo "No courses available";
        }
        mysql_close($bd);
?>
</form>     

and use this to read the $_GET data. Alternatively, you can also use the method "post" and $_POST to read out the data. Mind the hidden form tag for submitting the "courseID" value of the current form.

<?php
 $value=$_GET['id'];
 echo "Select value: ".$value;
 print_r($_GET);
?>
Community
  • 1
  • 1
  • @oliverbachmann... thanks for reply, i tried but the button "Add" is not working. when i click it gives no response – user3001644 Nov 17 '13 at 13:55
  • Instead of using javascript inside the onClick you can also use , which does not need javascript to be activated. Make sure you escape the double quotes correctly here. – oliverbachmann Nov 17 '13 at 14:05
  • Have a look at the HTML source of the generated website and make sure that the hidden input field with the name "courseID" has the correct value, as well as the options of the select box (maybe the quotes are wrong?). After pressing "Add", the URL should be addToTemp.php?courseID=&id=. What URL do you get? – oliverbachmann Nov 17 '13 at 15:11
  • after submiting the form i stay in the page and nothing is added to the url – user3001644 Nov 17 '13 at 16:12
  • i mean the page is not redirecting as well as adds nothing to url – user3001644 Nov 17 '13 at 16:21
  • Try this HTML:
    - does this take you to addToTemp.php? If so, partially add the other HTML one after another, until it breaks. Then you know what exactly is not working.
    – oliverbachmann Nov 17 '13 at 16:43
0

You have to change the printout of your PHP code to just print the id in the value of otion not the url:

while($row = mysql_fetch_assoc($run)){
                $courseId = $row['course_id'];
                echo "<option class='displayRec' value = '".$courseId."'" . ">" . $row['course_codes'] . "</option>"; 
            } 

the above may generate the following HTML:

<option name="usn" value="" class="displayUsn">-- Select Course --</option>
    <option class='displayRec' value = "1" >1Code</option>
    <option class='displayRec' value = "2" >2Code</option>
    <option class='displayRec' value = "3" >3Code</option>
    <option class='displayRec' value = "4" >4Code</option>
    <option class='displayRec' value = "5" >5Code</option>

The use window.location from a function. All result is:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <select class="action" name='id' id="sel">
<option name="usn" value="" class="displayUsn">-- Select Course --</option>
    <option class='displayRec' value = "1" >1Code</option>
    <option class='displayRec' value = "2" >2Code</option>
    <option class='displayRec' value = "3" >3Code</option>
    <option class='displayRec' value = "4" >4Code</option>
    <option class='displayRec' value = "5" >5Code</option>
  </select>

  <input type='button' onClick="GotoPage()" value='Add' id='id'> 

  <script>

    function GotoPage(){
      s = document.getElementById('sel');
      if (s.value != ''){
        window.location.href='addToTemp.php?id='+s.value;        
      }
    }
  </script>
</html>

Checkout this DEMO

SaidbakR
  • 13,303
  • 20
  • 101
  • 195