How to check data after form Ubmit and return result in selected Div?

Asked

Viewed 519 times

2

I’ve got two little problems I’m bumping my head on, let’s go to the first one:

1- I have the code below just after my Submit to check that the data has all been filled in:

    <?php
$data_coleta  = $_POST["data_coleta"];
$hora_coleta  = $_POST["hora_coleta"];
$unidade      = $_POST["unidade"];
$observacao   = $_POST["observacao"];
$solicitante  = $_POST["solicitante"];
$coletadora   = $_POST["coletadora"];
$erro         = 0;

// Verifica se os campos não estão em branco
if (empty($data_coleta))
  {echo "Favor inserir a Data da Coleta.<br>"; $erro=1;}
if (empty($hora_coleta))
  {echo "Favor inserir a Hora da Coleta.<br>"; $erro=1;}
if (empty($unidade))
  {echo "Favor inserir a Unidade.<br>"; $erro=1;}
if (empty($observacao))
  {echo "Favor inserir a Observação.<br>"; $erro=1;}
if (empty($solicitante))
  {echo "Favor inserir o Solicitante.<br>"; $erro=1;}
if (empty($coletadora))
  {echo "Favor inserir a Coletadora.<br>"; $erro=1;}
//Verifica se não houve erro
if($erro==0)
  {echo "<center>Todos os dados foram inseridos corretamente!</center>";
  include 'insere.php';
}
?>

The problem is that right away PHP is returning me an error saying that the variables have no value defined, this is because it has not yet been given Submit in the form to define the values (which is what is inserted by the user), I imagine I then need to do an if to only do this value check after being given Ubmit, how to do?

2- I think there’s something in the Bootstrap JS that I’m using that asks that always when refreshing the page should return to the top, so I can’t direct the form action to the div that I need, JS the button to go to the top below:

<script type="text/javascript" src="js/move-top.js"></script>
    <script type="text/javascript" src="js/easing.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function($) {
        $(".scroll").click(function(event){     
            event.preventDefault();
            $('html,body').animate({scrollTop:$(this.hash).offset().top},1200);
        });
    });
</script>

     <a href="#" id="toTop" style="display: block;"><span id="toTopHover" style="opacity: 1;"></span></a>

Tips?

  • Makes a if isset in his $_POST before making the verification

  • Get this man an Oscar! It was very simple, but as I’m learning everything by myself I don’t know some basic functions and what they do, thank you very much! Only need to resolve the issue of returning to the Register DIV.

  • Do one last test, in the form action put . php##support and in <a name="#support"></a>

  • The page is not found, I will send open the result of the registration (it is a service protocol) in a popup, is the way, is even more beautiful if I can configure the Magnific Popup.

  • see in the answer some considerations regarding anchors.

1 answer

1

The first question has already been solved by the friend leonardopessoa if (isset($_POST['submit'])) {

The second question is also very easy.

in your form action put the name of the destination page followed by a #support

example:

form action="index.php#support"

and on the landing page put <a name="support"></a> at the exact location you wish to be directed to.

More details

OBS: <a name=""> specifies the name of an anchor. Not supported in HTML5

SOURCE 1 ----- FONT 2 -name attribute on img, form and a (use id Instead)

Use the id attribute on an element to act as an anchor. Examples: <h1 id="support"></h1> <div id="support"></div> etc...

  • It had basically the same result as my previous code, it shows that it is in index.php? #support in the address bar but does not scroll down to the id="support" div. Otherwise, I must keep my script or it does not serve?

  • It is not that it will descend until the div of id='support' and rather that it will descend in the exact place that you put < a name="support" >< /a >, it is a link that for not having any text will not appear on the page but will serve as indicator. Of course it should be placed next to div='Uport', which I assume is the desired location. Your javascript did not serve or serve. Throw away in the trash, rs, :)

  • Same result, but analyzing calmly now, I think he’s giving Reload and staying in the same div but quickly after Submit he plays to the top of the page, is it something in the JS of this Bootstrap that I’m using? It has a little button that serves to play to the top of the page, but will have something in JS saying to play to the top whenever der Reload?

  • I edited the original post with JS.

  • but where is the part of the form?

  • <method="post" action="index.php#support"> and the <input type="Submit" name="Submit" onclick="myfunction()" value="Register" >

  • and you put on the destination page < a name="support" >< /a > (no spaces - copy there from the answer) on the side of the div you need

  • Pastebin of the entire div: https://pastebin.com/WbT3AH0t

  • test here and see if it works http://kithomepage.com/sos/fg.php just click on sign up

  • It works perfectly, that Zica, I imagine it is the same Bootstrap JS.

  • Follow the same thing, as if nothing had been changed, I’m thinking of having open the result of the form in a Magnific Popup, but I can see that will be a pain in the ass for the reason the data return from SQL.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.