-2

Is there any way to stop refreshing the page on submit button if your if condition goes false and show all the input fields with values entered?

Pradeep
  • 9,667
  • 13
  • 27
  • 34
hammer
  • 82
  • 10
  • Yes, there is. Are you looking for a way to validate a form? If yes, have you tried something yet? It would help to solve your problem! – CodeF0x Jun 02 '18 at 18:32
  • You need to provide more details that what exactly you want to do. – Ram Thota Jun 02 '18 at 18:34
  • I am entering data in database but with if condition in which i am checking that two fields have same value if yes then enter and redirect to other page if not then stay on this page – hammer Jun 02 '18 at 18:35
  • Please provide a [mcve] – charlietfl Jun 02 '18 at 18:37
  • i tried event.preventDefault() but it did not work – hammer Jun 02 '18 at 18:37
  • It should but you need to show us what you tried in it's full context – charlietfl Jun 02 '18 at 18:38
  • It "sounds" like you want to make an ajax request and then read the response to display an error message or redirect if successful. However we would need to see the HTML form, JavaScript you've already written and ideally the page that handles the form submission. I'm assuming a PHP file somewhere? – James Jun 02 '18 at 18:41

3 Answers3

2

Since you mentioned PHP, then why not use it?

I assume your question has two parts like below.

Part one - you wrote:

show all the input fields with values entered

By above, you mean using $_SESSION to repopulate the fields with the submitted data?

Part two - you wrote:

Is there any way to stop refreshing the page on submit button if your if condition goes false

Note that submit and any on is an event within the client side processing scope. You can use jQuery or JS validations for that.

Here below are two files for your learning test.

The posting php:

<html>
  <body>
    <form action="action.php" method="POST">
      <input type="text" name="feedback" value="" />
      <input type="hidden" name="token" value="123456ABCDEF" />
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>

<?
session_start();

$token = '123456ABCDEF';

if(isset($_SESSION)){

    if($_SESSION['token'] == $token){

        echo "Your feedback:<br />";

        foreach($_SESSION as $field=>$value):
                echo $field.": ".$value."<br />";
        endforeach;     

    }else{
        echo " Bad token! Cross-Site Request Forgery (CSRF)";
    }

}else{
    echo "Nothing!";
}

The posted to php:

<?php
session_start();

$_SESSION = $_POST;
$_SESSION['message'] = "Thank you for the feedback!";

header('Location: ' . $_SERVER['HTTP_REFERER']);

?>

Hope I got you right and above helps.

sitedevcode
  • 107
  • 6
-1

First off, this is a duplicate of JavaScript code to stop form submission

Now, when we got that out of the way, here is abrief explanation. You cannot prevent the browser itself from refreshing. But what you can do is prevent the form from being submitted (and in turn causing a refresh). And this is the usual solution when dealing with form validation in JS.

You can check the abode linked answer for more details.

MrManafon
  • 146
  • 1
  • 12
-1

You can use Post / Redirect / Get Pattern. If you have errors redirect to same page with form and show errors. You can't just "stop" the form during POST.

Other way if you don't want "showing" any redirect you can use ajax request. Form wont "blink".

You can also use javascript for checking field onchange event of input elements.

dyd
  • 54
  • 3