Asked  7 Months ago    Answers:  5   Viewed   24 times

I have an issue while using buttons inside form. I want that button to call function. It does, but with unwanted result that it refresh the page.

My simple code goes like this

<form method="POST">
    <button name="data" onclick="getData()">Click</button>
</form>

On clicking the button, the function gets called with page refreshed, which resets all my previous request which affects the current page which was result of the previous request.

What should I do to prevent the page refresh?

 Answers

30

Let getData() return false. This will fix it.

<form method="POST">
    <button name="data" onclick="return getData()">Click</button>
</form>
Tuesday, June 1, 2021
 
LOKESH
answered 7 Months ago
24

If you want to use a non-default rounding strategy, you'd need to do something like:

var x = 42.999m;

var y = string.Format("{0:C}", Math.Floor(x * 100) / 100);

Math.Floor rounds down; however it doesn't take a number of decimal places, so you have to force the 2 decimal place behaviour.

Friday, August 6, 2021
 
chugadie
answered 4 Months ago
15

You didn't say anything about file types so I used HTML-5 formats that work in Safari.

<!DOCTYPE>
    <meta charset="UTF-8">
    <title> audio video</title>

    <audio id="audioInHTML" controls="controls">     
    <source src="audio.wav" type="audio/wav"/>
    </audio>
    </div>

    <video id="videoInHTML" width="320" height="240" controls="controls">
      <source src="movie.mp4" type="video/mp4" />
    </video>

    <div id="playButtonDiv" </div>

    <form>
    <input type="button" value="PlaySoundAndVideo" onClick="PlaySoundAndVideo('videoInHTML'),PlaySoundAndVideo('audioInHTML')">

    </div>

    <script>

    function PlaySoundAndVideo(soundObj,videoObj) {
    var soundAndVideo=document.getElementById(soundObj,videoObj);
    soundAndVideo.play();
    }


    </script>

    <style type="text/css">


    #playButtonDiv {
    position:absolute;
    top:800px;

    };   

    </style>
Sunday, August 15, 2021
 
aorfevre
answered 4 Months ago
92

When you submit the form you are effectively loading a new page. Returning false in the submit handler simply tells the browser not to bubble the event and not perform the default event, which in this case is submiting the form, which is why nothing is happening when you do this.

There are several solutions to your problem. If you are redirecting your user (after the form submission) back to the form page but with errors, you can include the "fragment identifier" in the url. Have the fragment identifier point to the ID of you form and the browser will automatically go to that part of the document. Example:

<form id="my-form">
    ...
</form>

And your url would be:

host/path?query=querystring#my-form

If you want to avoid having the page reload, try doing your validation in JS. The jQuery validation plugin is very easy to use and simply fantastic. See here to find out how to use it. It's great because it does most of the heavy lifting for you and has a set of common, default validation methods.

Alternatively you can submit your form via ajax and return any validation errors that the server picks up. If you don't have any errors then you can redirect to the next page. If there are errors then stick them in the form or display an alert. This solution will avoid you having to replicate any complex validation logic on the client (if you want to use the jQuery validate plugin).

Tuesday, August 24, 2021
 
Sagar
answered 4 Months ago
86

What you should do is implement your own controls (or use an existing set such as videojs)
You can read my answer to this question: Html5 video overlay architecture

Wednesday, November 10, 2021
 
Assassin
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share