Created
September 7, 2018 04:30
-
-
Save jashimcse/1d22b498bc6e0170a742625d2f929073 to your computer and use it in GitHub Desktop.
Data insert WordPress by using template.Form data insert
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 | |
/** | |
* Template Name: product-page. | |
* | |
* @package WordPress | |
* @subpackage Twenty_Fourteen | |
* @since Twenty Fourteen 1.0 | |
*/ | |
get_header(); | |
?> | |
// | |
<?php | |
$error_message=""; | |
$success=""; | |
$product_data=array(); | |
$product_nameErr = $sell_unit_price_Err = $product_unit_Err = $buy_unit_price_Err = $product_quantity_Err = ""; | |
$product_name = $sell_unit_price = $product_unit = $buy_unit_price = $product_quantity = ""; | |
if ($_SERVER["REQUEST_METHOD"] == "POST"){ | |
if (empty($_POST["product_name"])) { | |
$product_nameErr = "* Name is required"; | |
} else { | |
$product_name = esc_attr($_POST["product_name"]); | |
} | |
if (empty($_POST["buy_unit_price"])) { | |
$buy_unit_price_Err = "* product unit price required"; | |
} else { | |
$buy_unit_price = esc_attr($_POST["buy_unit_price"]); | |
} | |
if (empty($_POST["sell_unit_price"])) { | |
$sell_unit_price_Err = "* product unit sell required"; | |
} else { | |
$sell_unit_price = esc_attr($_POST["sell_unit_price"]); | |
} | |
if (empty($_POST["product_quantity"])) { | |
$product_quantity_Err = "* product quantity required"; | |
} else { | |
$product_quantity = esc_attr($_POST["product_quantity"]); | |
} | |
if (empty($_POST["product_unit"])) { | |
$product_unit_Err = "* product_unit required"; | |
} else { | |
$product_unit = esc_attr($_POST["product_unit"]); | |
} | |
if ($product_name && $sell_unit_price && $buy_unit_price && $product_quantity && $product_unit){ | |
$product_data = array( | |
'name' => $product_name, | |
'sell_unit_price' => $sell_unit_price, | |
'buy_unit_price' => $buy_unit_price, | |
'unit' => $product_unit, | |
'quantity' => $product_quantity, | |
); | |
global $wpdb; | |
$table_name = $wpdb->prefix."product"; | |
$con = $wpdb->INSERT($table_name, $product_data,array( | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%s') | |
); | |
if ($con){ | |
$success ="Product added Successful"; | |
}else{ | |
$error_message = "Please insert valid data"; | |
} | |
} | |
} | |
?> | |
<div id="primary" class="content-area"> | |
<main id="main" class="site-main"> | |
<div class="product-form"> | |
<div class="container"> | |
<form class="well form-horizontal" action="<?php the_permalink(); ?> " method="post" id="product_form"> | |
<fieldset> | |
<!-- Form Name --> | |
<legend>Product Name</legend> | |
<div class="form-group"> | |
<label class="col-md-4 control-label">Product Name</label> | |
<div class="col-md-4 inputGroupContainer"> | |
<div class="input-group"> | |
<span class="input-group-addon"><i class="fa fa-product-hunt"></i></span> | |
<input name="product_name" placeholder="Product Name" class="form-control custom-select-pic" type="text"> | |
</div> | |
</div> | |
</div> | |
<!-- Text input--> | |
<div class="form-group"> | |
<label class="col-md-4 control-label" >Sell Unit price</label> | |
<div class="col-md-4 inputGroupContainer"> | |
<div class="input-group"> | |
<span class="input-group-addon"><i class="fa fa-product-hunt"></i></span> | |
<input name="sell_unit_price" placeholder="Sell Unit price" class="form-control custom-select-pic" type="text"> | |
</div> | |
</div> | |
</div> | |
<!-- Text input--> | |
<div class="form-group"> | |
<label class="col-md-4 control-label">Buy Unit price</label> | |
<div class="col-md-4 inputGroupContainer"> | |
<div class="input-group"> | |
<span class="input-group-addon"><i class="fa fa-product-hunt"></i></span> | |
<input name="buy_unit_price" placeholder="Buy Unit price" class="form-control custom-select-pic" type="text"> | |
</div> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label class="col-md-4 control-label">Quantity</label> | |
<div class="col-md-4 inputGroupContainer"> | |
<div class="input-group"> | |
<span class="input-group-addon"><i class="fa fa-product-hunt"></i></span> | |
<input name="product_quantity" placeholder="Quantity" class="form-control custom-select-pic" type="text"> | |
</div> | |
</div> | |
</div> | |
<!-- Text input--> | |
<div class="form-group"> | |
<label class="col-md-4 control-label">Unit</label> | |
<div class="col-md-4 selectContainer"> | |
<div class="input-group"> | |
<span class="input-group-addon"><i class="fa fa-product-hunt"></i></span> | |
<select name="product_unit" class="form-control selectpicker custom-select-pic" > | |
<option value="Please select your unit " ></option> | |
<option>g</option> | |
<option>kg</option> | |
<option >Pc</option> | |
<option >Litter</option> | |
<option >Other</option> | |
</select> | |
</div> | |
</div> | |
</div> | |
<!-- Success message --> | |
<div class="alert alert-success" role="alert" id="success_message">Success <i class="fa fa-product-hunt"></i> Thanks for contacting us, we will get back to you shortly.</div> | |
<!-- Button --> | |
<div class="form-group"> | |
<label class="col-md-4 control-label"></label> | |
<div class="col-md-4"> | |
<button type="submit" class="btn btn-warning" >Save <span class="fa fa-product-hunt"></span></button> | |
</div> | |
</div> | |
<label class="green"><?php echo $success; ?></label> | |
<label class="red"><?php echo $error_message; ?></label> | |
</fieldset> | |
</form> | |
</div> | |
</div><!-- /.container --> | |
</div> | |
</main><!-- #main --> | |
<?php | |
get_footer(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
css code:
/product page start/
.custom-select-pic{
font-size: 12px;
border: solid 1px#ccc;
padding: 0 0 2px 11px;
}
.custom-select-pic:focus{
border-color: #ccc;
outline: none;
box-shadow: none;
}