Jump to content

AJAX and mysql help needed


sandsquid

Recommended Posts

I am unable to get additional data to populate in to a new pull down menu (2nd line/col). upon selecting the first col/line, all works well. When I select the "add", I get whatever the first col has in it populated to the new 2nd col/line. It's been driving me nuts, I can't seem to figure it out, I need to requery my db and get the resuts.

 


<?php

//removed



connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Where you served - MyDutyStation.com</title>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.service.options[form.service.options.selectedIndex].value;
self.location='served.php?service=' + val ;
}
function reload3(form)
{
var val=form.service.options[form.service.options.selectedIndex].value;
var val2=form.country.options[form.country.options.selectedIndex].value;
var val3=form.command_name.options[form.command_name.options.selectedIndex].value;

self.location='served.php?service=' + val + '&country=' + val2 + '&command_name=' + val3;
}


function addRowToTable()
{
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById("theValue").value -1)+ 2;
numi.value = num;
var divIdName = "my"+num+"Div";
var newdiv = document.createElement('div');
newdiv.setAttribute("id",divIdName);
newdiv.innerHTML = "Some text here";
ni.appendChild(newdiv);

  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);

  //Title
  var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode('Service '+iteration+':');
  cellLeft.appendChild(textNode);

  //Service
  var serviceCell = row.insertCell(1);
  serviceCell.appendChild(tbl.rows[1].cells[1].cloneNode(true));

  //Country
  var countryCell = row.insertCell(2);
  countryCell.appendChild(tbl.rows[1].cells[2].cloneNode(true));

  //Command
  var commandCell = row.insertCell(3);
  commandCell.appendChild(tbl.rows[1].cells[3].cloneNode(true));

}

function removeRowFromTable()
{
  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}

</script>
</head>


<body>
<table border=1 id=tblSample>
<tr>
    <td> </td><td>Service</td><td>Country</td><td>Command</td>
  </tr>
<tr>
    <td>Service 1:</td>
<?


///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT service FROM militarydb");
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
$cat=$HTTP_GET_VARS['service']; // This line is added to take care if your global variable is off
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT country FROM militarydb WHERE service = '$service'");
}else{$quer=mysql_query("SELECT DISTINCT country FROM militarydb WHERE service = '$service'"); }
////////// end of query for second country drop down list box ///////////////////////////


/////// for Third drop down list we will check if sub category is selected else we will display all the country/////
$cat3=$HTTP_GET_VARS['country']; // This line is added to take care if your global variable is off
if(isset($ca3) and strlen($cat3) > 0){
$quer3=mysql_query("SELECT DISTINCT command_name FROM militarydb WHERE service = '$service' AND country = '$country' ORDER BY comman
d_name");
}else{$quer3=mysql_query("SELECT DISTINCT command_name,major_units FROM militarydb WHERE service = '$service' AND country = '$countr
y' ORDER BY command_name"); }
////////// end of query for third subcategory drop down list box ///////////////////////////

if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT country FROM militarydb WHERE service = '$service'");
}else{$quer=mysql_query("SELECT DISTINCT country FROM militarydb WHERE service = '$service'"); }
////////// end of query for second country drop down list box ///////////////////////////


/////// for Third drop down list we will check if sub category is selected else we will display all the country/////
$cat3=$HTTP_GET_VARS['country']; // This line is added to take care if your global variable is off
if(isset($ca3) and strlen($cat3) > 0){
$quer3=mysql_query("SELECT DISTINCT command_name FROM militarydb WHERE service = '$service' AND country = '$country' ORDER BY comman
d_name");
}else{$quer3=mysql_query("SELECT DISTINCT command_name,major_units FROM militarydb WHERE service = '$service' AND country = '$countr
y' ORDER BY command_name"); }
////////// end of query for third subcategory drop down list box ///////////////////////////


echo "<form method=get name=f1 action='served.php'>"; 
//////////        Starting of first drop downlist /////////
echo "<td><select name='service' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['service']==@$cat){echo "<option selected value='$noticia2[service]'>$noticia2[service]</option>"."<BR>";}
else{echo  "<option value='$noticia2[service]'>$noticia2[service]</option>";}
}
echo "</select></td>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
if($service != "None"){
echo "<td><select name='country' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) {
if($noticia['country']==@$cat3){echo "<option selected value='$noticia[country]'>$noticia[country]</option>"."<BR>";}
else{echo  "<option value='$noticia[country]'>$noticia[country] </option>";}
}
echo "</select></td>";
}else{
echo "<p>Your all done! Thanks";
exit;
}
//////////////////  This will end the second drop down list ///////////


//////////        Starting of third drop downlist /////////
echo "<td><select name='command_name'><option value=''>Select one</option>";
while($noticia1 = mysql_fetch_array($quer3)) {
if($noticia1['command_name']==@$cat4){echo "<option selected value='$noticia1[command_name]'>$noticia1[command_name]</option>"."<BR>
";}
else{
echo  "<option value='$noticia1[command_name] $noticia1[major_units]'>$noticia1[command_name] $noticia1[major_units]</option>";
}
}
echo "</select></td>";
//////////////////  This will end the third drop down list ///////////
?>
</tr>
</table>
<input type="hidden" value="0" id="theValue" />
<div id="myDiv" style="border: 3px coral solid;"> </div>

 

sorta kinda working example can be found here: http://sandsquid.com/served.php

Link to comment
https://forums.phpfreaks.com/topic/76434-ajax-and-mysql-help-needed/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.