I have a code using AJAX, and I want to validate if the username and exist. If the the username and password exist it will ask a question if the user want to proceed. If he clicks YES it will be inserted.
And if the username and password is not existing, it will be in inserted too!
<script type = "text/javascript">
function Save() {
$.ajax({
type: "POST",
url: "test.aspx/GetData",
//data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '", name2: "' + $("#<%=txtPassword.ClientID%>")[0].value + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}//var a = confirm("Existing! Proceed?")
function OnSuccess(response) {
// if exist
// confirm(You want to proceed?
// if the user clicks YES, it will be inserted
// if not exist
// insert
}
</script>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<input id="btnSave" type="button" value="Save"
onclick = "Save()" />
C# Code
[System.Web.Services.WebMethod]
public static bool GetData(string name, string name2)
{
test insert = new test();
MySqlConnection conn = Functions.GetConnection();
string sql = "SELECT CustomerID, RefDocNo FROM tblsomain WHERE CustomerID=@CustomerID AND RefDocNo=@RefDocNo";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@CustomerID", name);
cmd.Parameters.AddWithValue("@RefDocNo", name2);
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
return true;
}
else
{
//return "not exist!";
return false;
}
}
Thank you