0

I'm having trouble getting my variable to post in my form. I use jquery to load a box if the user wants to enter a new sub category.

Here is the select in the form

<select id="sub_cat1" name="sub_cat1">
   <option value=""></option>
   <option value="new">New Sub Cat</option>
<?php
$query = "SELECT sub_category FROM blog_posts GROUP BY sub_category";
$data = mysqli_query($dbc, $query);
    if (mysqli_num_rows($data) > 0) {
      while ($row = mysqli_fetch_array($data)) {
    $sub_category = $row['sub_category'];
    echo '<option value="' . $sub_category . '">' . $sub_category . '</option>';
    }
     }
?>
        </select>
<div id="load_sub_cat_box"></div>

This is the jquery

$(document).ready(function(){ 
$("#sub_cat1").on("keyup change", function() {  
  var prod = $("#sub_cat1").val();
  if(prod == 'new'){
  $("#load_sub_cat_box").append('<div style="padding-top:15px;">Enter New Sub Category</div><input type="text" name="sub_cat2" />');
  }
    }); 
});

And here is the form validate code

  if($_POST['sub_cat1'] == "new"){
$sub_cat = mysqli_real_escape_string($dbc, trim($_POST['sub_cat2']));
  }
  else{
$sub_cat = mysqli_real_escape_string($dbc, trim($_POST['sub_cat1']));  
  }

If I select new, the box does load on the page but for some reason when form is submitted it will not load a variable for $sub_cat1. Is there something I don't know about jquery and forms?

1 Answer 1

2

You have two select elements with id="sub_cat1".

ids must be unique per page.

It is also invalid HTML, with a select inside a select.

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.