Stephen Rogers
Stephen Rogers

Reputation: 1

html form not posting from javascript and php

I have a html form that posts the data to a php file for processing, on this form there is a dynamically produced combo box that is produced from a php file using javascript. The combo box displays and functions fine when the page is loaded but when the form is submitted the value from this box isn't posted.

the JavaScript function is

function showUser(str) {
    if (str == "") {
        document.getElementById("selSubCat").innerHTML = "";
        return;
     } else { 
         if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
         } else {
             // code for IE6, IE5
             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.onreadystatechange = function() {
             if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                 document.getElementById("selSubCat").innerHTML = xmlhttp.responseText;
              }
         }
         xmlhttp.open("GET","getSubCats.php?Cat="+str,true);
         xmlhttp.send();
    }
}

The html is

    <td >Category:</td>
      <td > 
        <select name="Cats" onchange="showUser(this.value)" ><?    

        $qryCats="SELECT * FROM tblCategories";
        $resCats=mysql_query($qryCats,$dbMain);
        while($rowCats = mysql_fetch_array($resCats)){
        echo "<option value='".$rowCats['Name']."'>".$rowCats['Name']."</option>";
        }
    ?>
    </select>

    </td>
    </tr>
    <tr>
    <td >Sub Category:</td>

    <td  id="selSubCat"> 
    </td>
    </tr>

And the php file:

    <? 
    include("dbconfig.php");
   $cat=$_GET['Cat'];

    $qryCats="SELECT * FROM tblSubCats WHERE Parent='" .$cat. "'";
        $resCats=mysql_query($qryCats,$dbMain);
        if ($numrow=mysql_num_rows($resCats)>0){
            echo "<select name='subCats'>";
        while($rowCats = mysql_fetch_array($resCats)){
        echo "<option value='" .$rowCats['Name']. "'>" .$rowCats['Name']. "</option>";
        }

        echo "</select>";
        }
        else{
            echo " There are no sub categories ";
        }
    ?>

Any suggestions will be appreciated, I can't figure out why everything else apart from the subcategory is posted

Upvotes: 0

Views: 58

Answers (2)

Stephen Rogers
Stephen Rogers

Reputation: 1

Solved, I had a table on the html page which the form was inside. I swapped the tags around so that the table is inside the form and the dynamic fields post just fine. Not sure why having a form within a table stopped these from posting but at least it works now.

Upvotes: 0

Abhinav
Abhinav

Reputation: 8168

Check out the name attribute. In HTML its Cats but in your code you are using $_GET['Cat']; It should be

$cat=$_GET['Cats'];

Upvotes: 1

Related Questions