-2

I want to get the value that I entered in the prompt, and save it in a variable to use it to update a DB later .. I try this but is does not work !!

@{
    var fileName = "";
    var db = Database.Open( "GP" );
    var sqlupdate = "Update rc_Files set fileName=@0 Where fileID= 5";
    db.Execute(sqlupdate, fileName);
 }


<html lang="en">
  <body>
    <script>
        function myFunction() {
             newName = prompt("Please enter new file name :");
             if (newName != null) 
             {
                 @fileName = newName;
             }
        }
    </script>
  </body>
</html>
maksimov
  • 5,792
  • 1
  • 30
  • 38

3 Answers3

1

JavaScript is client side language. You can't updated db with it. You can send request to your server side script, which will update something in datatable.

You can find example of doing this here or just use google.

Community
  • 1
  • 1
Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
0

Try this code:

$(document).ready(function() {
    var fileName = '';
    var newName = prompt('Please enter a new file name');
    if(newName != null) {
       fileName = newName;
        console.log(fileName);
    }
});  

Its getting the value you entered through javascript.

Demo here

Bas
  • 2,106
  • 5
  • 21
  • 59
0

From your question is not clear what is your goal.

If you want to store a value in your page waiting to use it when the page is posted, you could use a hidden input field.

In my example the value inputed when the page is loaded is stored until the user clicks the submit button:

@{
    if(IsPost){
        var fileName = Request["fileName"];
        var db = Database.Open("GP");
        var sqlupdate = "Update rc_Files set fileName=@0 Where fileID= 5";
        db.Execute(sqlupdate, fileName);
    }
}

<html lang="en">
    <head>
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    </head>
    <body>
        <form method="post">
            <input type="hidden" name="fileName" id="fileName" value="" />
            <input type="submit" />
        </form>
        <script>
            $(document).ready(function () {
                var newName = prompt('Please enter a new file name');
                $('#fileName').val(newName);
            });
        </script>
    </body>
</html>

Else, if you want to update your database without submitting your page, you should use Ajax. This article could help you: Posting Data With jQuery AJAX In ASP.NET Razor Web Pages.

GmG
  • 1,372
  • 1
  • 9
  • 10