inserting values in database sql server 2008 in mvc through javascript/
I want to insert the values in database in my MVC application but I am getting the values through javascript.Please tell me how shall I do ,is there any way through which we can handle javascript in mvc application.Please suggest some useful resources.
my code of javascript is regarding the dynamic fields creation through which the user creates dynamic fields .I am getting the values from the user please tell me what shall I do.
the javascript code is as follows:
<script language="javascript" type ="text/javascript" >
function addRow(tableID) {
var elem = document.getElementById("linkid");
if (typeof elem.onclick == "function") {
elem.onclick.apply(elem);
}
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if (rowCount < 25) {
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
element2.type = "text";
cell2.appendChild(element2);
var cell3 = row.insertCell(2);
var element3 = document.createElement("input");
element3.type = "text";
cell3.appendChild(element3);
var cell4 = row.insertCell(3);
var element4 = document.createElement("a");
element4.setAttribute("href", "#");
var anchortext = document.createTextNode('Edit');
element4.appendChild(anchortext);
// cell4.appendChild(element4);
}
else {
alert("you can add only upto 25 fields");
}
}
function deleteRow(tableID) {
try {
debugger;
var table = document.getElementById(tableID);
var rowCount = table.rows.length; 1
for (var i = 0; i < rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if (null != chkbox && true == chkbox.checked) {
table.deleteRow(i);
rowCount--;
i--;
}
}
} catch (e) {
alert(e);
}
}
function hideVisible(divid, divid1) {
try {
if (divid == "Advertiser")
{
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("advertiser_Text");
text.setAttribute("class", "newtext1");
text.value = "Advertiser";
}
}
}
if (divid == "Brand")
{
开发者_JAVA技巧 if (document.getElementById(divid).style.display == "block")
{
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none")
{
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("brand_Text");
text.setAttribute("class", "newtext1");
text.value = "Brand";
}
}
}
if (divid == "Target")
{
if (document.getElementById(divid).style.display == "block")
{
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none")
{
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("Target_Text");
text.setAttribute("class", "newtext1");
text.value = "Target";
}
}
}
if (divid == "Timing") {
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("Timing_Text");
text.setAttribute("class", "newtext1");
text.value = "Timing";
}
}
}
if (divid == "Program") {
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("Program_Text");
text.setAttribute("class", "newtext1");
text.value = "Program Scope";
}
}
}
if (divid == "Media") {
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("Media_Text");
text.setAttribute("class", "newtext1");
text.value = "Media Buy";
}
}
}
if (divid == "Distribution") {
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("Distribution_Text");
text.setAttribute("class", "newtext1");
text.value = "Distribution Goal";
}
}
}
if (divid == "Added") {
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("Added_Text");
text.setAttribute("class", "newtext1");
text.value = "Added Value";
}
}
}
if (divid == "Objective") {
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("Objective_Text");
text.setAttribute("class", "newtext1");
text.value = "Objective";
}
}
}
if (divid == "Strategy") {
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("Strategy_Text");
text.setAttribute("class", "newtext1");
text.value = "Strategy";
}
}
}
if (divid == "Market") {
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("Market_Text");
text.setAttribute("class", "newtext1");
text.value = "Market Research";
}
}
}
if (divid == "Refusal") {
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("Refusal_Text");
text.setAttribute("class", "newtext1");
text.value = "Right of First Refusal Date";
}
}
}
}
catch (e) {
alert(e);
}
}
</script>
Thanks in advance Ritz
Well i think you can't insert values to database with Javascript directly, you should use some AJAX to send data to server-side backend. Try jQuery Javascript framework, it has lots of convinient methods for it.
if (divid == "Refusal") {
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById("Refusal_Text");
text.setAttribute("class", "newtext1");
text.value = "Right of First Refusal Date";
}
}
}
...
can be cleaned up into:
function hideVisible(divid, divid1) {
var occupations = [{name:'Refusal',text:'Right of First Refusal Date', id:4},
{name:'Market',text:'Market Research'}];
while (var x=0;x<occupations.length;x++){
if (occupations[x]['name'] == divid){
var occupation = occupations[x];
break;
}
}
if (occupation){
if (document.getElementById(divid).style.display == "block") {
document.getElementById(divid).style.display == "none"
document.getElementById(divid).style.visibility = "hidden";
if (document.getElementById(divid1).style.display == "none") {
document.getElementById(divid1).style.display = "block"
document.getElementById(divid1).style.visibility = "visible";
var text = document.getElementById(divid+"_Text");
text.setAttribute("class", "newtext1");
text.value = occupation.name;
}
}
post('/newtable', function(xhr) {
alert('Saved!: '+xhr.responseText);
}, '?id='+occupation.id);
}
else {
//Column not found...
}
}
}
function post(url, callback, params) {
var xhr;
if(typeof XMLHttpRequest !== 'undefined') xhr = new XMLHttpRequest();
else {
var versions = ["Microsoft.XmlHttp",
"MSXML2.XmlHttp",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.5.0"];
for(var i = 0, len = versions.length; i < len; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
}
catch(e){}
} // end for
}
xhr.onreadystatechange = function() {
if((xhr.readyState < 4) || xhr.status !== 200) return;
callback(xhr);
};
xhr.open('POST', url, true);
xhr.send(params);
}
Then add an xmlhttp request to post some data to the server, which will return a callback.
精彩评论