开发者

How to pass the server side validation messages back to client?

开发者 https://www.devze.com 2023-01-10 05:36 出处:网络
I have the following two script files (i.e., formtest.html and calc.php). When I do the server side validation on calc.php, how do I pass the error message (i.e.

I have the following two script files (i.e., formtest.html and calc.php). When I do the server side validation on calc.php, how do I pass the error message (i.e. $err_msg back to the formtest.html?

Thank you

<html>
    <head>
        <title>Form Test</title>
    </head>
    <body>

    <form method="post" action="calc.php">
    <pre>
          Loan Amount <input type="text" name="principle" />
                      <input type="submit" />
    </pre>
    </form>
    </body>
    </html>

.

// c开发者_如何学Calc.php

        $err_msg = ''; 

        if ( !empty(isset($_POST['principle'])) )
        {
            // process the form and save to DB
        } else {
         $err_msg .= 'Loan Amount is empty!';
        }

        ?>


You will either need to use a Server Side Include to bring in the PHP output into the HTML file (if it needs to remain an HTML file), or (a better solution) would be to include it all in one file like so:

(calc.php)

<?php
    $err_msg = '';
    if (isset($_POST['principle']) && !empty($_POST['principle']) )
    {
        // process the form and save to DB
    } else {
     $err_msg .= 'Loan Amount is empty!';
    }
?>
<html>
<head>
    <title>Form Test</title>
    <script type="text/javascript">
    <!--
         var errMsg = '<?php echo $err_msg; ?>';
         // Do something with javascript here.
    -->
    </script>
</head>
<body>
<div class="error">
<?php 
    // Or echo it inline with your HTML. 
    echo $err_msg;
?> 
</div>
<form method="post" action="calc.php">
<pre>
      Loan Amount <input type="text" name="principle" />
                  <input type="submit" />
</pre>
</form>
</body>
</html>

Not sure if that's valid as I wrote it off the top of my head. But that's the gist. Hope that made sense. ;)

**Changed code to reflect your comment.*

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号