Conflict of forms

2

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

  

PHP With ajax

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

PHP Without ajax

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

If the conflict is not there I'll 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 non-ajax form

<?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')");
        }
    }
?>
    
asked by anonymous 08.03.2018 / 15:26

1 answer

2

If both forms are sending data via POST , when doing this if ($_SERVER["REQUEST_METHOD"] == "POST") checking both forms enter, set another condition to 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 should compare values or different names on both forms, either the submit button or fields that have one form but not 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") {
    //...
}

You can also do with switch case if you prefer

    
08.03.2018 / 15:45