使用Display Pic注册表单 [英] Sign up form with Display Pic

查看:85
本文介绍了使用Display Pic注册表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为管理员设计了一个添加产品表单,他可以在其中添加产品信息并上传项目图片。表格类似于注册,这就是为什么问题的主题是这样的。我已经包括了服务器端和客户端验证。

我得到的问题是上传图像我在主窗体中添加了另一种形式,它只能以这种方式工作,但我在其他字段中得到必填字段错误。图像路径也不会保存在数据库中。

这是我的代码。任何帮助,将不胜感激。谢谢



< table cellpadding ='''''cellspacing =''0''border =''0''id =' 'ap_table''> 
< tr>
< td>< table cellpadding =''0''cellspacing =''0''border ='''''width ='''100%''>< tr>< td> ; 输入产品信息< / th>
< / tr>
< tr>
< td>< br>
< form method =postaction =name =prodid =prodtarget =_ top>
@ Html.ValidationSummary()
< input type =hiddenname =actionvalue =login>
< input type =hiddenname =hidevalue =>
< center>
< table>
< tr>< td>产品名称:< / td>< td>< input type =textname =pnameid =pnamevalue =@ Request [pname ]/>< / td>
< td>
< div style =height:auto>
@ Html.ValidationMessage(pname)
< / div>
< / td>
< / tr>
< tr>< td>说明:< / td>< td>< textarea rows =10cols =25name =descid =desc>< / textarea的> < / TD>
< td>
< div style =height:auto>
@ Html.ValidationMessage(desc)
< / div>
< / td>
< / tr>
< tr>< td>价格:< / td>< td>< input type =textname =priceid =pricevalue =@ Request [price ]>< / TD>
< td>
< div>
@ Html.ValidationMessage(price)
< / div>
< / td>
< / tr>
< tr>< td>品牌:< / td>< td>< input type =textname =brandid =brandvalue =@ Request [brand ]>< / TD>
< td>
< div style =height:auto>
@ Html.ValidationMessage(品牌)
< / div>
< / td>
< / tr>
< tr>< td>类别:< / td>
< td>
< select id =catname =cat>
< option>< / option>
@foreach(猫的var项目){
< option> @ item.Title< / option>
}
< / select>
< / td>
< / tr>
< tr>< td> < / td>< td>< input type =submitvalue =Save>< / td>< / tr>
< tr>< td colspan = 2> < / td>< / tr>
< / table>
< / form>
< / td>
< td>
< form action =method =postenctype =multipart / form-data>
< fieldset>
< legend>添加图片< / legend>
< label for =Image>图片< / label>
< input type =filename =Image/>
< br />
< input type =submitvalue =上传/>
< / fieldset>
< / form>
<! - < h1>上传的图片< / h1> - >
@if(imagePath!=){
< div class =result>
< img alt =图片不可用src =@ imagePath>

< / div>
}
< / td>
< / tr>
< / table>
< / td>
< / tr>
< / table>
< / center>

< script type =text / javascript>

$(#prod)。验证({
规则:{
pname:{
required:true
},
desc:{
required:true
},
price:{
required:true
},
品牌:{
required:true
}
},
消息:{
pname:{
required:请提供产品名称
},
desc: {
required:请提供产品说明
},
价格:{
required:请输入价格
},
品牌:{需要
:请提供产品品牌
}
}
});

< / script>







这是服务器端代码:





布局=〜/ Layouts / _Layout.cshtml;

PageData [title] =添加产品;



//获取类别

var db = Database.Open(myDB);

var cq =从类别中选择标题;

var cats = db.Query(cq);





WebImage photo = null;

var newFileName =;



var name =;

var desc =;

var price =;

var brand =;

var cat =;

var imagePath =;



//验证



Validation.RequireField(pname,请输入产品名称);

Validation.RequireField(desc,请提供产品说明);

Validation.RequireField(price,请输入产品价格);

验证.RequireField(品牌,请提供品牌名称);



if(IsPost){

//图片上传代码

photo = WebImage.GetImageFromRequest();



< p> @ photo< / p> 



if(photo != null){

newFileName = Path.GetFileName(photo.FileName);

photo.Resize(100,100);



imagePath = @images \+ newFileName;



//photo.Save(@\"~\+ imagePath);

photo.Save(imagePath);

}

if(Validation.IsValid())

{

name = Request [pname];

desc =请求[desc];

price =要求[价格];

brand =要求[品牌];

cat =请求[cat];



var q =插入产品(名称,描述,价格,品牌,类别,ImagePath)值(@ 0,@ 1,@ 2,@ 3, @ 4,@ 5);

//db.Query(q,name,desc,price,brand,cat,imagePath);



if(db.Execute(q,name,desc,price,brand,cat,imagePath)> 0)

{

 < p> 产品已成功添加< / p> 



}

其他

{

< p> 添加产品时出错< / p> 



}

}

}

}

解决方案

(#prod)。validate({
规则:{
pname:{
required:true
} ,
desc:{
required:true
},
price:{
required:true
},
品牌:{$ b需要$ b:true
}
},
消息:{
pname:{
required:请提供产品名称
},
desc:{
required:请提供产品说明
},
价格:{
required:请输入价格
},
品牌:{
必填:请提供产品品牌
}
}
});

< / script>







这是服务器端代码:





布局=〜/ Layouts / _Layout.cshtml;

PageData [title] =添加产品;



//获取类别

var db = Database.Open(myDB);

var cq =从类别中选择标题;

var cats = db.Query(cq);





WebImage photo = null;

var newFileName =;



var name =;

var desc =;

var price =;

var brand =;

var cat =;

var imagePath =;



//验证



Validation.RequireField(pname,请输入产品名称);

Validation.RequireField(desc,请提供产品说明);

Validation.RequireField(price,请输入产品价格);

验证.RequireField(品牌,请提供品牌名称);



if(IsPost){

//图片上传代码

photo = WebImage.GetImageFromRequest();



< p> @ photo< / p> 



if(photo != null){

newFileName = Path.GetFileName(photo.FileName);

photo.Resize(100,100);



imagePath = @images \+ newFileName;



//photo.Save(@\"~\+ imagePath);

photo.Save(imagePath);

}

if(Validation.IsValid())

{

name = Request [pname];

desc =请求[desc];

price =要求[价格];

brand =要求[品牌];

cat =请求[cat];



var q =插入产品(名称,描述,价格,品牌,类别,ImagePath)值(@ 0,@ 1,@ 2,@ 3, @ 4,@ 5);

//db.Query(q,name,desc,price,brand,cat,imagePath);



if(db.Execute(q,name,desc,price,brand,cat,imagePath)> 0)

{

 < p> 产品已成功添加< / p> 



}

其他

{

< p> 添加产品时出错< / p> 



}

}

}

}


I have design a "Add Product" form for admin where he can add his product information and upload item picture. The form is similar to sign up thats why the subject of question is like this. I have included both server and client side validations.
The problem I''m getting is that to upload image I have added another form in the main form and it works only this way but I get "required field" errors in other field. Also the image path is not saved in the database.
This is my code. Any help would be appreciated. Thanks

<table cellpadding=''2'' cellspacing=''0'' border=''0'' id=''ap_table''>
    <tr>
        <td ><table cellpadding=''0'' cellspacing=''0'' border=''0'' width=''100%''><tr><td>Enter product information</th>
    </tr>
    <tr>
        <td><br>
            <form method="post" action="" name="prod" id="prod" target="_top">
                @Html.ValidationSummary()
                <input type="hidden" name="action" value="login">
                <input type="hidden" name="hide" value="">
                <center>
                    <table>
                        <tr><td>Product Name:</td><td><input type="text" name="pname" id="pname" value="@Request["pname"]" /></td>
                            <td>
                                <div style="height: auto">
                                    @Html.ValidationMessage("pname")
                                </div>
                            </td>
                        </tr>
                        <tr><td>Description:</td><td><textarea rows="10" cols="25" name="desc" id="desc"></textarea> </td>
                            <td>
                                <div style="height: auto">
                                    @Html.ValidationMessage("desc")
                                </div>
                            </td>
                        </tr>                        
                        <tr><td>Price:</td><td><input type="text" name="price" id="price" value="@Request["price"]"></td>
                            <td>
                                <div>
                                    @Html.ValidationMessage("price")
                                </div>
                            </td>
                        </tr>
                        <tr><td>Brand:</td><td><input type="text" name="brand" id="brand" value="@Request["brand"]"></td>
                            <td>
                                <div style="height: auto">
                                    @Html.ValidationMessage("brand")
                                </div>
                            </td>
                        </tr>
                        <tr><td>Category:</td>
                            <td>
                                <select id="cat" name="cat">
                                    <option></option>
                                    @foreach(var item in cats){
                                        <option>@item.Title</option>
                                    }
                                </select>                            
                            </td>                            
                        </tr>                        
                        <tr><td> </td><td><input type="submit" value="Save"></td></tr>
                        <tr><td colspan=2> </td></tr>
                    </table>
                    </form>
                </td>
        <td>
            <form action="" method="post" enctype="multipart/form-data">
                <fieldset>
                    <legend> Add Image </legend>
                    <label for="Image">Image</label>
                    <input type="file" name="Image" />
                    <br/>
                    <input type="submit" value="Upload" />
                </fieldset>
            </form>
            <!--<h1>Uploaded Image</h1>-->
                @if(imagePath != ""){
                <div class="result">
                    <img alt="Image not available" src="@imagePath">

                </div>
                }
        </td>
            </tr>
        </table>
        </td>
    </tr>
</table>
</center>

<script type="text/javascript">
   
    $("#prod").validate({
    rules: {
        pname: {
            required: true
        },
        desc: {
            required: true
        },
        price: {
            required: true
        },
        brand: {
            required: true
        }
    },
    messages: {
        pname: {
            required: "Please provide a product name"
        },
        desc: {
            required: "Please provide product description"
        },
        price: {
            required: "Please enter price"            
        },
        brand: {
            required: "Please provide brand for product"            
        }
    }
});

</script>




and this is server side code:


Layout = "~/Layouts/_Layout.cshtml";
PageData["title"] = "Add Product";

//getting categories
var db = Database.Open("myDB");
var cq = "select Title from Categories";
var cats = db.Query(cq);


WebImage photo = null;
var newFileName = "";

var name = "";
var desc = "";
var price = "";
var brand = "";
var cat = "";
var imagePath = "";

// Validation

Validation.RequireField("pname", "please enter product name");
Validation.RequireField("desc", "please provide product description");
Validation.RequireField("price", "please enter product price");
Validation.RequireField("brand", "please provide brand name");

if(IsPost){
// image upload code
photo = WebImage.GetImageFromRequest();

<p>@photo</p>


if(photo != null){
newFileName = Path.GetFileName(photo.FileName);
photo.Resize(100, 100);

imagePath = @"images\" + newFileName;

//photo.Save(@"~\" + imagePath);
photo.Save(imagePath);
}
if(Validation.IsValid())
{
name = Request["pname"];
desc = Request["desc"];
price = Request["price"];
brand = Request["brand"];
cat = Request["cat"];

var q = "insert into Products(Name, Description, Price, Brand, Category, ImagePath) values(@0, @1, @2, @3, @4, @5)";
//db.Query(q, name, desc, price, brand, cat, imagePath);

if(db.Execute(q, name, desc, price, brand, cat, imagePath) > 0)
{

<p>Product added successfully</p>


}
else
{

<p>There was an error while adding the product</p>


}
}
}
}

解决方案

("#prod").validate({ rules: { pname: { required: true }, desc: { required: true }, price: { required: true }, brand: { required: true } }, messages: { pname: { required: "Please provide a product name" }, desc: { required: "Please provide product description" }, price: { required: "Please enter price" }, brand: { required: "Please provide brand for product" } } }); </script>




and this is server side code:


Layout = "~/Layouts/_Layout.cshtml";
PageData["title"] = "Add Product";

//getting categories
var db = Database.Open("myDB");
var cq = "select Title from Categories";
var cats = db.Query(cq);


WebImage photo = null;
var newFileName = "";

var name = "";
var desc = "";
var price = "";
var brand = "";
var cat = "";
var imagePath = "";

// Validation

Validation.RequireField("pname", "please enter product name");
Validation.RequireField("desc", "please provide product description");
Validation.RequireField("price", "please enter product price");
Validation.RequireField("brand", "please provide brand name");

if(IsPost){
// image upload code
photo = WebImage.GetImageFromRequest();

<p>@photo</p>


if(photo != null){
newFileName = Path.GetFileName(photo.FileName);
photo.Resize(100, 100);

imagePath = @"images\" + newFileName;

//photo.Save(@"~\" + imagePath);
photo.Save(imagePath);
}
if(Validation.IsValid())
{
name = Request["pname"];
desc = Request["desc"];
price = Request["price"];
brand = Request["brand"];
cat = Request["cat"];

var q = "insert into Products(Name, Description, Price, Brand, Category, ImagePath) values(@0, @1, @2, @3, @4, @5)";
//db.Query(q, name, desc, price, brand, cat, imagePath);

if(db.Execute(q, name, desc, price, brand, cat, imagePath) > 0)
{

<p>Product added successfully</p>


}
else
{

<p>There was an error while adding the product</p>


}
}
}
}


这篇关于使用Display Pic注册表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆