0
votes

Query is If I select 'Select a New Parent category' from dropdown, a new textbox gets appear. I wants to insert that value into database. That textbox is hidden bydefault.

HTML FILE

<head>
    <script type="text/javascript">
                    $(document).ready(function() {  
    $('#parentcat').change(function(){  
    if($('#parentcat').val() === 'Create a New Parent Category')  
       {  
       $('#other').show();   
       $('#catname').hide();

       }  
    else 
       {  
       $('#other').hide();   
       $('#catname').show(); 

       }  
    });  
    });     

    </script>   </head>

    <table>
    <body>
            <form method="POST" action="" name="form1"  enctype="multipart/form-data" >
                <tr>
                    <td><h4>Add category</h4></td>
                </tr>
                <tr>
                    <td>Select parent Category:</td><td><select name="parentcat" id="parentcat">

                    <?php 
            include "../storescripts/connect_to_mysql.php";
                        $result=mysql_query("SELECT parent from category"); while($row=mysql_fetch_array($result)){
                            echo "<option>".$row['parent'];"</option>";
                        } ?>
                            <option value="Create a New Parent Category">Create a New Parent Category</option>   
                        </select><br/></td>
                        <tr><td><input type="text" id="other" name="other" placeholder="New Parent Category Name:" style="display: none;"/></td></tr>


                </tr>

                <tr>
                    <td></td><td><input type="text" name="catname" placeholder="SubCategory name" id="catname"><br/></td>
                </tr>

                <tr>
                    <td>Status:
                    </td>
                    <td><select name="status" >
                        <option selected="Active">Active</option>
                        <option>Inactive</option>
                    </select></td>
                </tr>


                    <tr><td>Category Image :</td><td><input type="file" name="imageUpload" id="imageUpload"></td>
                    </tr>
                    <tr>
                    <td></td>
                    <td><input type="submit" name="submit" value="Insert"></td>
                    </tr>


                </form>
                </table>

Here is my complete HTML file,bydefault 'other' dropdown is hidden. Now Here I'm providing my php code that inserts values into database, issue is how can I insert value of hidden filed into database. My database fields includes : cat_id(Auto-increment),cat_name,parent,cat_status,cat_image. Here Is my php code for inserting values into database.

<?php 
                                 if(isset($_POST['submit']))
                                 {
                                 $parentcat=$_POST['parentcat'];
                                $catname=$_POST['catname'];
                            $status=$_POST['status'];
                                 $target_dir = "../uploads/";
                                  $target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
                                  $uploadOk = 1;
                                  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

                                    if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
                                        echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
                                    } else {
                                        echo "Sorry, there was an error uploading your file.";
                                    }

                                    $image=basename( $_FILES["imageUpload"]["name"],".jpg"); // used to store the filename in a variable


                                $sql = mysql_query("SELECT cat_id FROM category WHERE cat_name='$catname' LIMIT 1");
                                  $CatMatch = mysql_num_rows($sql); // count the output amount
                                    if ($CatMatch > 0) {
                                    echo 'Sorry you tried to place a duplicate "Category Name" into the system, <a href="addcategoryy.php">click here</a>';
                                    exit();

                                  }

                                  else{
                                   $sql=mysql_query("INSERT INTO `category` (`cat_name`, `parent`, `cat_status`,`cat_image`) VALUES ('$catname', '$parentcat', '$status','$image') ");
                                   echo $sql;
                                }

                                }

?>

For an eg. If I Select A new parent category then Subcategory named textbox appears and I want to insert that value into database. How Can i do that.

1
Remove the display:none and take the <input type="hidden"> and set your value. Now you can use this hidden value by its id on $_POST. - RJParikh
Then how would I Display it on dropdown option change? - user4563796

1 Answers

0
votes

Change your jquery code like this

<script type="text/javascript">
$(document).ready(function () {
    $('#parentcat').change(function () {

        if ($('#parentcat').val() === 'Create a New Parent Category'){
            $('#other').prop('type', 'text');
            $('#catname').prop('type', 'hidden');
        }
        else{
            $('#other').prop('type', 'hidden');
            $('#catname').prop('type', 'text');

        }
    });
});
</script> 

Also change your HTML code for #other input box like this

<input type="hidden" id="other" name="other" placeholder="New Parent Category Name:"/>

Let me know if it works for you.


Edit

In your PHP file, Instead of $catname=$_POST['catname']; add this

$catname = ( $parentcat == "Create a New Parent Category") ? $_POST['other'] : $_POST['catname'];