I have created an array of DropDown menus on a while-loop and I would like to post the value on change of the menu. The problem is that since I have created the menus with a loop and the id $_SESSION['List_company_id'] is being overwritten so I cannot get the id of the menu to post.
$i=0;
while($query_data = mysqli_fetch_row($result)) {
$_SESSION["List_company_id"]=$query_data[3];
$_SESSION["List"]=$query_data[1];
$_SESSION["list_name"]=$query_data2[2];
echo '<div class="form-group">';
echo "<p>List number $i</p>";
echo '<span class="icon-case hidden-xs"><i class="fa fa-home"></i></span>';
echo "<select class='dropdown' name=\"".$_SESSION['List_company_id']."\">";
echo "<option selected = 'selected' value=\"".$_SESSION["List_company_id"]."\">".$query_data[2]."</option>";
$query1="SELECT * FROM `calling_lists`";
$result1=mysqli_query($connection,$query1) or die ("Query to get data from list table failed: ".mysql_error());
while ($row1=mysqli_fetch_array($result1)) {
$list_name=$row1["calling_list_name"];
$list_description=$row1["calling_list_description"];
$list_id=$row1["calling_list_id"];
echo "<option value=\"$list_id\">$list_name</option>";
};
echo "</select>";
echo "</div>";
$i=$i+1;
};
echo '</form>';
echo "</div>";
?>
The Ajax I would like to use looks like:
<script type="text/javascript">
$(document).ready(function(){
$("#.$_SESSION['List_company_id'].").change(function(){
var company = $(this).val();
var dataString = "list="+list;
if($_SESSION['List_company_id']){
$.ajax({
type:'POST',
url:"get-data1.php",
data:dataString,
success:function(html){
$('#contact').html(html);
}
});
}
});
});
Finally my get-data1.php looks like:
<?php
/* Connect to MySQL and select the database. */
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
$database = mysqli_select_db($connection, DB_DATABASE);
$c=$_POST['$_SESSION['List_company_id'];
$query="UPDATE `calling_list_company` SET `calling_list_id`='$c' WHERE `calling_list_company_id`='".$_SESSION["company_id"]."' ;";
$result=mysqli_query($connection,$query) or die ("Query to get data from contact_company table failed: ".mysqli_connect_error());
?>