Link to home
Start Free TrialLog in
Avatar of shruti A
shruti A

asked on

Populated Dropdown data is not inserting into database table

Populated Dropdown data is not inserting into database table

<div id="div5">
                <input type="text" placeholder="Sub Category Name" required="" id="subcatname" name="subcatname" />
    <script>

        $(document).ready(function(){

        	 $.ajax({
              type: "Post",
              url: "getAttribute.php",
              success: function(data) {
                    var obj = $.parseJSON(data);      
                    var result = "<select id="+ this['catname'] + " ><option>select Parent Category</option>";
                    $.each(obj, function() {
                        result = result + "<option value=" + this['catname'] + ">"+ this['catname'] +"</option>" ;
                    });
                    result = result + "</select>"
                    $("#result1").html(result);
              }
        }); 
            $("#subcat").click(function(e){
                   e.preventDefault();
                //var color=$("#color").val();
                var subcatname=$("#subcatname").val();
                var subcat=$("#subcat").val();
                 var catname=$("#catname").val();
                
                $.ajax({
                    url:'color.php',
                    method:'POST',
                    data:{
                       
                       subcatname:subcatname,
                        subcat:subcat,
                        catname:catname
                    },
                   success:function(data){
                       alert(data);
                   }
                });
            });
        });
        
        
    </script>
   
          <!-- <input type="button" value="Color View" id="ajaxButton1"/>-->
<div id="result1"></div> 
 <input type="submit" value="ADD" name="ADD3" id="subcat" /> 
               
      
</div>

Open in new window



above is ajax for retrieving data into dropdown and inserting into table is below

if(isset($_POST['subcat'])){
$subcatname=$_POST['subcatname'];
$catname=$_POST['catname'];
echo $catname;
if($subcatname!="" ||  $catname!=""){
$sql="INSERT INTO `sub_category` (`id`,`sub_category`,`cat_name`) VALUES (NULL,'$subcatname','$catname')";
if ($db->query($sql) === TRUE) {
    echo "data inserted";
}
else 
{
    echo "failed";
}
}
else
{
    echo "Fill Values Properly";
}
}

Open in new window


So please help I'm not getting What is the problem here Please lhelp me to solve this error
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

OK. Your code looks a little quirky. Before trying to deal with the DB stuff, I would get your dropdown and button sorted.

When trying to build your select, what are you expecting this['catname'] to be - nothing in your code defines that! You may want to show us what getAttribute.php returns.

Also, in your click event, you have this;

var subcat=$("#subcat").val();
var catname=$("#catname").val();

#subcat is the ID of your button, to the value will always be 'ADD'. There is nothing in your code with an ID of catname, so not sure what that refers to.
What about (please note the include) :
<div id="div5">
    <input type="text" placeholder="Sub Category Name" required="" id="subcatname" name="subcatname" />


    <!-- <input type="button" value="Color View" id="ajaxButton1"/>-->
    <div id="result1">
        <select>
            <option>select Parent Category</option>
        <?php
            include("getAttribute.php");
        ?>
        </select>
    </div>
    <input type="submit" value="ADD" name="ADD3" id="subcat" />


</div>

Open in new window


With the following Javascript :
    <script>
        $(document).ready(function(){
            $("#subcat").click(function(e){

                e.preventDefault();

                //var color=$("#color").val();

                var subcatname = $("#subcatname").val();
                var subcat = $("#subcat").val();
                var catname = $("select", "#result1").val();

                $.post('color.php', { subcatname: subcatname, subcat: subcat, catname: catname }).then(function(data){
                    alert(data);
                });
            });
        });
    </script>

Open in new window

Avatar of shruti A
shruti A

ASKER

@Chris Stanyon

Actully $catname means checkbox id what i have retrieved
Hye Shruti,

I think you misunderstood me. You have this line:

var result = "<select id="+ this['catname'] + " ><option>select Parent Category</option>";

Open in new window

You are trying to build a <select> by using a variable called this['catname'] - that doesn't exist. You need to change that to the following:

var result = "<select id='catname'><option>select Parent Category</option>";

Open in new window

You also need to wrap your <select> values in single quotes:

result = result + "<option value='" + this['catname'] + "'>"+ this['catname'] +"</option>";

Open in new window

Having said all that, is there a reason why you're building the <select> with an AJAX call on page load. Why not just do it directly on the server?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.