Created
March 21, 2015 06:37
-
-
Save devjosh12/c16c1528e3b0749269d5 to your computer and use it in GitHub Desktop.
add category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php include('config.php'); ?> | |
<style> | |
#search_category_id{ padding:3px; width:200px;} | |
#sub_category_id{ padding:3px; width:200px;} | |
</style> | |
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script> | |
<center><a href="view_categories.php">Show Categories</a> | |
<a href="addproduct.php">Add Products</a></center> | |
<form method="post" onsubmit="return(validate());" action="<?php $_PHP_SELF ?>" style="margin-top:20px;" name="myForm"> | |
<table width="400" border="0" cellspacing="1" cellpadding="2" align="center"> | |
<tr> | |
<td style="width:120px;float:left;">Category Title</td> | |
<td><input onkeyup="checkc('cat_title');" name="cat_title" type="text" id="cat_title"></td> | |
</tr> | |
<tr> | |
<td style="width:120px;float:left;">Category Content</td> | |
<td><textarea onkeyup="checkc('cat_content');" name="cat_content" type="textarea" id="cat_content"></textarea></td> | |
</tr> | |
<tr> | |
<tr> | |
<td style="width:120px;float:left;">Click Here For Parent Category</td> | |
<td><input value="0" type="checkbox" name="myparentcat" checked="checked" id="check"></td> | |
</tr> | |
<tr id="sub-cat" style="display:none;"> | |
<td style="width:120px;float:left;">Select Category</td> | |
<td><?php $categoryList = fetchCategoryTree();?> | |
<select name="pcategory" id="pcat"> | |
<option value="0">Please Select</option> | |
<?php foreach($categoryList as $cl) { ?> | |
<option value="<?php echo $cl["cat_id"] ?>"><?php echo $cl["title"]; ?></option> | |
<?php } ?> | |
</select> | |
</td> | |
</tr> | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
$('input[type="checkbox"]').click(function(){ | |
if($(this).prop("checked") == true){ | |
//alert("Checkbox is checked."); | |
$('#sub-cat').css('display','none'); | |
} | |
else if($(this).prop("checked") == false){ | |
//alert("Checkbox is unchecked."); | |
$('#sub-cat').css({'display':'block','width':'120px'}); | |
//var checkval = $('#check').val(); | |
//alert(checkval); | |
} | |
}); | |
}); | |
</script> | |
<tr> | |
<td width="100"> </td> | |
<td> | |
<input name="add" type="submit" class="submit" id="add" value="Add category"> | |
</td> | |
</tr> | |
</table> | |
</form> | |
<span class="error" style="display:none;color:red;"> Please Enter Valid Data</span> | |
<span class="success" style="display:none;color:green;"> Catgeory Created Successfully</span> | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
$(".submit").click(function(){ | |
var title = $('#cat_title').val(); | |
var cat_content = $('#cat_content').val(); | |
var checkval = $('#check').val(); | |
var cat = $('#pcat').val(); | |
alert(cat); | |
if(title=='' || cat_content == '' || cat == '' || checkval == '' ) | |
{ | |
$('.success').fadeOut(200).hide(); | |
$('.error').fadeOut(200).show(); | |
} | |
else | |
{ | |
$.ajax({ | |
type:'POST', | |
url:'insert_cat.php', | |
data:'title='+title+'&content='+cat_content+'&category='+cat+'&myparentcat='+checkval, | |
success:function(data) { | |
alert(data); | |
$('.success').fadeIn(200).show(); | |
$('.error').fadeOut(200).hide(); | |
} | |
}); | |
} | |
return false; | |
}); | |
}); | |
</script> | |
<?php | |
function fetchCategoryTree($parent = 0, $spacing = '', $user_tree_array = '') { | |
if (!is_array($user_tree_array)) | |
$user_tree_array = array(); | |
$sql = "SELECT cat_id, title, is_parent FROM categories WHERE is_parent = $parent ORDER BY cat_id ASC"; | |
$query = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $query); | |
if (mysql_num_rows($query) > 0) { | |
while ($row = mysql_fetch_object($query)) { | |
$user_tree_array[] = array("cat_id" => $row->cat_id, "title" => $spacing . $row->title); | |
$user_tree_array = fetchCategoryTree($row->cat_id, $spacing . ' ', $user_tree_array); | |
} | |
} | |
return $user_tree_array; | |
} | |
?> | |
<script type="text/javascript"> | |
<!-- | |
// Form validation code will come here. | |
function validate() | |
{ | |
if( document.myForm.cat_title.value == "" || document.myForm.cat_title.value.trim().length == 0) | |
{ | |
alert( "Please provide title!" ); | |
document.myForm.cat_title.focus() ; | |
return false; | |
} | |
if( document.myForm.cat_content.value == "" || document.myForm.cat_title.value.trim().length == 0) | |
{ | |
alert( "Please provide content!" ); | |
document.myForm.cat_content.focus() ; | |
return false; | |
} | |
return(true); | |
} | |
function checkc(id) | |
{ | |
if(document.getElementById(id).value == '') | |
{ | |
window.alert ("This field cant be left empty"); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment