1

I'm pulling data from an Azure SQL database using PHP and have successfully created a drop-down (Select) box with Options that are pulled from the database. The SQL query returns 2 columns, Title & Cycle_ID.

I have set the Title as the text string and the Cycle_ID as the value. I now want to store the value (Cycle_ID) of the current selection in a variable (MyVariable), which I will use in the SQL query for the next drop-down box I'm creating, i.e. WHERE Cycle_ID = MyVariable. This way the user progressively narrows their selection as they work their way down through my drop-down boxes. My current code is below, but I don't know how to create and store the current selection to MyVariable.

<?php
//create the database connection and define the query
$serverName = "myserver";
$connectionOptions = array(
    "Database" => "mydatabase",
    "Uid" => "mysqlaccount",
    "PWD" => "mypassword"
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);

//defines cycle query
$cyclesql= "SELECT [Cycle_ID]
      ,[Title]
  FROM [ods].[Cycle]
  WHERE End_Date > DATEADD(month,-6,GETDATE()) AND Updated IS NOT NULL
  ORDER BY Cycle_ID Asc";
$cycleResults= sqlsrv_query($conn, $cyclesql);

if ($cycleResults == FALSE)
    echo (sqlsrv_errors());
?>

<html>
<body>

<form action="cyclefile.php" method="post"> 
<div id="select">
<p>Select the week:</p><select name='Week'>
<option value="">Select...</option>
<?php
//starts loop
while ($row = sqlsrv_fetch_array($cycleResults, SQLSRV_FETCH_BOTH)) {
//defines cycle variable
$cycle = $row['Title'];
//defines cycle_id variable
$cycle_id = $row['Cycle_ID'];
//displays cycle variable as option in select (dropdown) list and cycle_id as value 
echo '<option value="'.$cycle_id.'">'.$cycle.'</option>';
}
?>
</select>
</div>
</form>
</body>
</html>
2
  • you're going to have to use JavaScript if you don't want to reload the webpage. If you don't mind, then build a new URL with that variable as GET and build it showing the new drop-down box Commented Nov 8, 2017 at 9:40
  • when you post back the form, there will be a variable $_POST["Week"] available which contains the ID of the selected option in that dropdown. Or if you want to use the value client-side in Javascript before the page is refreshed then please clarify that, and you can google an example of it anyhow. Commented Nov 8, 2017 at 10:06

1 Answer 1

1

I suggest you to make an ajax call on selection and pass the selected value to process your result in another file where you can run your query.

<select name="cycle_data" id="cycle_id">
   <option value="cycle1" > Cycle 1</option>
   <option value="cycle2" >Cycle 2</option>
</select>

Then lets do a script:

 <script type="text/javascript">
   $(document).ready(function(){ 
   $("#cycle_id").change(function(){ 
   var cycle_data = $(this).val(); 
   var dataString = "cycle_data="+cycle_data; 

    $.ajax({ 
    type: "POST", 
    url: "get-data.php", 
    data: dataString, 
    success: function(result){ 
      // Process your result here
    }
    });
    });
   });
</script>
Sign up to request clarification or add additional context in comments.

Comments

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.