Conflict of forms

Asked

Viewed 67 times

2

I have 2 forms, one by ajax and the other php normal. both forms are working, but when I run the php form without ajax it returns me the errors of the php form with ajax.

PHP Com ajax

if ($_SERVER["REQUEST_METHOD"] == "POST") {

PHP Sem ajax

if (isset($_POST['Btnlk'])){

If the conflict is not there I put all the code.

Form code with ajax

<form class="form1"  method="POST" enctype="multipart/form-data">

    <div id="ressult_log_succes"></div>
    <div id="ressult_log_error"></div>

    <p>E-mail</p>
    <input type="text" name="Email_user" placeholder="Informe seu E-mail">

    <p>Senha</p>
    <input type="password" name="Senha_user" placeholder="Informe a sua senha">

    <input type="submit" id="Bot_login" name="Logar_sn" value="Logar">
</form>

<script>
    $(function(){
        $(".form1").submit(function(event){
            event.preventDefault();
            var formDados = $(".form1").serialize();

            $.ajax({
                url:"/complement-of-archive/ConsultaLoginUser.php",
                type:"POST",
                data:formDados,
                cache:false,
                processData:false,
                success:function(data){
                    $("#ressult_log_succes").html(data);
                },
                error:function(data){
                    $("#ressult_log_error").html(data);
                },
                dataType:"html"
            });
            return false;
        });
    });
</script>

My form without ajax

<form class="form2" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="ID_Pagina_LK_DL" value="2">
    <input type="text" name="Usuario_LK_DL" value="" placeholder="Informe seu nome">
    <input type="submit" name="Btnlk" value="1">
</form>

<?php
    require_once('SysLKDLe.php');
?>

php from my form without ajax

<?php
    if (isset($_POST['Btnlk']) && $_POST['Btnlk'] == "1") {
        $LK_DL = 1;
        $ID_Pagina_LK_DL = $_POST['ID_Pagina_LK_DL'];
        $ID_Usuario_LK_DL = $_POST['Usuario_LK_DL'];

        if ($LK_DL != 1) {
        }
        else{
            $SQL_LK_DL = mysqli_query($conex,"INSERT INTO Banco_lk (LK_DL, Nome_LK_DL, IDUser_LK_DL) VALUES ('$LK_DL', '$ID_Pagina_LK_DL', '$ID_Usuario_LK_DL')");
        }
    }
?>
  • 1

    Man, both Forms are entering if do with ajax. After all, it validates only if the request was via POST. Or send a parameter in the request to identify which form was sent or change the HTTP verb, if possible.

1 answer

2


If both forms are sending data via POST, when making such a verification if ($_SERVER["REQUEST_METHOD"] == "POST") the two forms enter, put another condition to the if(), for example

Form 1:

<form action="meuphp.php" method="post">
    <!-- ... -->
    <input type="submit" name="botao_enviar" value="Cadastrar">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["botao_enviar"]) && $_POST["botao_enviar"] == "Cadastrar") {

Form 2:

<form action="meuphp.php" method="post">
    <!-- ... -->
    <input type="submit" name="botao_enviar" value="Alterar">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["botao_enviar"]) && $_POST["botao_enviar"] == "Alterar") {

You must compare different values or names in both forms, it can be the upload button or fields you have in one form but not in another

Another option is to pass a variable op via GET for example:

<form action="meuphp.php?op=cadastrar" method="post">
    <!-- ... -->
    <input type="submit" name="botao_enviar" value="Cadastrar">
</form>

<form action="meuphp.php?op=alterar" method="post">
    <!-- ... -->
    <input type="submit" name="botao_enviar" value="Cadastrar">
</form>

<?php
if ($_GET["op"] == "cadastrar") {
    //...
} else if ($_GET["op"] == "alterar") {
    //...
}

Can also do with switch case prefer

  • So, I updated the post. I tried to specify the php button with ajax but did not return anything. pq defined in my ajax that I would only request php if "Form1" was clicked.

  • put your other <form> and PHP

  • Okay, I just put it there.

  • 1

    and the PHP of <form> with AJAX? Already tried to pass a variable op through the url and make a if else or switch case?

Browser other questions tagged

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