gpt4 book ai didi

asp.net - 使用jquery将数据插入数据库

转载 作者:行者123 更新时间:2023-12-01 01:35:59 26 4
gpt4 key购买 nike

我正在尝试将从 asp .net 表单中获取的四个字段插入到数据库中的表中,但我无法做到这一点。请给我完整编译的代码,该代码可以工作并且成功地将 4 个值输入到数据库

这是我使用的代码

 <script type="text/javascript">
$(document).ready(function () {
$("#<%= Button1.ClientID %>").click(function () {
/* $(".style2").hide();
return false; */
var name = $('#<%=TextBox1.ClientID%>').val();
var email = $('#<%=TextBox2.ClientID%>').val();
var phone = $('#<%=TextBox3.ClientID%>').val();
var message = $('#<%=TextBox4.ClientID%>').val();
var json = "{'" + name + "','" + email + "','" + phone + "','" + message + "'}";
alert("comes here");
$ajax({
type:"POST",
url:"JqueryFunction.aspx/insert_details",

data:'{name: "' + $("#<%=TextBox1.ClientID%>")[0].value + '",email: "' + $("#<%=TextBox2.ClientID%>")[0].value + '",phone: "' + $("#<%=TextBox3.ClientID%>")[0].value + '",message: "' + $("#<%=TextBox4.ClientID%>")[0].value + '"}',

clientType:"application/json; charset=utf-8",
datatype:"json",
async: false,
success: function () { alert("success"); },
error: function(){alert("error");}
});
});
});
</script>

这是 insert_details 函数:

public static void insert_details(string name,string emailid,string phone,string message)
{
SqlConnection Con = new SqlConnection(@"Server=HP-PC;Integrated Security=True;" + "Database=DB1");
//Console.WriteLine("connected to db");
string query = "INSERT INTO [SubmissionFormTable]([Name],[Email],[Phone],[Message])" +
"VALUES (@Name,@Email,@Phone,@Message)";

SqlCommand cmd = new SqlCommand(query, Con);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Email", emailid);
cmd.Parameters.AddWithValue("@Phone", phone);
cmd.Parameters.AddWithValue("@Message", message);

try
{
Con.Open();

cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
Con.Close();
}
//string str_insert = "INSERT INTO remark VALUES(@remark)";

//SqlParameter[] parameter = {
// new SqlParameter("@remark", remark),
// };
// int abc = DBclass.TruyVan_XuLy_VoiThamSo(str_insert, parameter);
}

最佳答案

更新您的 js 代码,如下所示,并尝试使用 JSON.stringify 将值转换为 JSON 以传递您的参数:

$(document).ready(function () {
$("#<%= Button1.ClientID %>").click(function () {
var name = $('#<%=TextBox1.ClientID%>').val();
var email = $('#<%=TextBox2.ClientID%>').val();
var phone = $('#<%=TextBox3.ClientID%>').val();
var message = $('#<%=TextBox4.ClientID%>').val();
var data = JSON.stringify({ name: name, email: email, phone: phone, message: message });
alert("comes here");
$ajax({
type:"POST",
url:"JqueryFunction.aspx/insert_details",
data:data,
contentType: "application/json; charset=utf-8",
datatype:"json",
success: function () { alert("success"); },
error: function(){alert("error");}
});
});
});

上面的代码没有问题。

已编辑:
你的方法应该是一个WebMethode:

[System.Web.Services.WebMethod]
public static void insert_details(string name,string emailid,string phone,string message)
{
SqlConnection Con = new SqlConnection(@"Server=HP-PC;Integrated Security=True;" + "Database=DB1");
//Console.WriteLine("connected to db");
string query = "INSERT INTO [SubmissionFormTable]([Name],[Email],[Phone],[Message])" +
"VALUES (@Name,@Email,@Phone,@Message)";

SqlCommand cmd = new SqlCommand(query, Con);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Email", emailid);
cmd.Parameters.AddWithValue("@Phone", phone);
cmd.Parameters.AddWithValue("@Message", message);

try
{
Con.Open();

cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
Con.Close();
}
//string str_insert = "INSERT INTO remark VALUES(@remark)";

//SqlParameter[] parameter = {
// new SqlParameter("@remark", remark),
// };
// int abc = DBclass.TruyVan_XuLy_VoiThamSo(str_insert, parameter);
}

关于asp.net - 使用jquery将数据插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13513853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com