0

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

2
  • Danger: You are using an obsolete database API and should use a modern replacement. You are vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from. Commented Jun 21, 2015 at 15:20
  • Thanks for the advise Quentin, I do plan on changing everything to mysqli or PDO Commented Jun 23, 2015 at 19:06

2 Answers 2

1

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

$cat=$_GET['Cats'];
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, the php file is getting the Cat variable from the javascript not the html select
I dont find any errors with the code. Did you watch console for any errors?
there is no errors in the error log, it posts the Cat select ok but doesn't post the subCats one from the added php code
0

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.

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.