Problem with Get in jquery

Asked

Viewed 189 times

0

Man script works perfectly, but there are hours that requires the page of the registration sent a selection type category, and it sends to the address bar as if it were $_get, but it’s all coming true $_post in the serialize of jQuery.

How the error appears, being that it is via post: https://www.meusite.com.br/cadastro?pro=produto

How it’s supposed to be: https://www.sistema.g4w.com.br/cadastro

Javascript code:

// JavaScript Document

$(document).ready(function () { 

//sessão de form produtos
   $(function () {

        $('#formss').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'matriz/mostra_select.php',
            data: $('#formss').serialize(),
            success: function (retorno) {

            $('#prods').html(retorno);

            }
          });

        });

   });


   });

HTML + PHP:

<form id="formss">
    <select class="select" name="bd" style="width: 95%">
            <option value=''>Selecione a Categoria para Cadastro dos Produtos</option>
      </select>
    <option value="categoria">categoria</option>';
            <input type="submit" id="novo" />
</form>

<!-- no select vai uma busca em php, ele busca no arquivo em questão na ulr no js, e retorna pra mim,
porem tem horas que ele mostra como se tivesse ido via get
codigo php abaixo, o arquivo que recebe-->

<div class="main_produtos">

<?php
    include('conecta.php');


$fila = $_POST['bd'];
$pega = $fila.'_lista';

$mostrando = "SELECT * FROM $fila order by cod desc limit 1";
$mm = mysqli_query ($con, $mostrando)  or die(mysql_error());


    $venda = mysqli_fetch_array($mm);

    $cods = $venda['cod'];

    if(empty($cods)){

        $cods = '1';

        ?>
    <?php   
    }
    else{

        $cods = $venda['cod'] + 1;
    }

?>
  • 1

    Without seeing the code becomes difficult to give an accurate answer.

  • 1

    And this option outside the select?

  • was an editing error right here.. rs, the script is correct

  • friend, the property that defines whether it is POST in $.ajax is not the method? Try to exchange the type for method

  • @Ademilsonsantanadasilva is not, I switched and sporadically continues the error

  • Try setting method="post" in your html form tag. Now understanding better, is the address that it is firing the url different from the address it should be? Because the reference will be the URL that is at the time you send, so it varies from the address you are accessing.

  • @Juarezturrini I do not want him to direct to another page, but give me an answer in the same, even if it is another page

  • "but there are hours"... strange this. In the programming area this statement does not match. Either it is or it is not. Your tb code has many inconsistencies. Could you at least edit the question and put exactly the correct code.

  • the code is correct, I say "has hours" because he makes the request quiet, however he gives this bug that I can not solve

  • Puts method="post" in <form tag>

  • Taken that in no part of the code there is the parameter pronor the value produto

  • As has been said, there are many inconsistencies in the code you have placed, from labels out of place, ' which are over, etc ... so I suggest you put the code here in the question exactly like the one you have otherwise you won’t get the help you’re looking for. It is difficult to help when the code we see is all hypothetical and does not play with what you have that is "correct"

  • It is true @Isac, it is not enough the problem itself, add to it the sloppiness when posting the code correctly.

  • 1

    Friend, it is not possible to reproduce the problem with the information you posted, so I will give two kicks. First of all, put in the ajax ' method: "POST" ', as it is in the official jQuery documentation. http://api.jquery.com/jquery.ajax/. So the kicks: first, in your html, there is no "action" in the form, which by default is get. So if your javascipt fails, for an untreated error for example, e.preventDefault() will never be triggered, and the upload will be in html form. When it fails, check your console for an error message. Second kick, your js script is not caching?

  • If you are caching, concatenate something in the file name. Example: <script src="app.js? v=2"></script>

  • @Paulosakamoto puts the answer that I will be positive, he did not give the answer json, but the get is gone from the bar

Show 11 more comments

1 answer

1

Friend,

First, change the ajax option from "type: 'post'" to "method: 'POST'", as it is in the official documentation of jQuery $.ajax:

$.ajax({
    method: 'POST',
    url: 'matriz/mostra_select.php',
    data: $('#formss').serialize(),
    success: function (retorno) {
        $('#prods').html(retorno);
    }
});

As for the error itself, it is not possible to reproduce it, but a probable reason is a javascript error, which may be interrupting the execution of it, and in this case, the e.preventDefault() will never be enabled and the form will be sent by html form (GET method by default). When you encounter the error, see the browser console (in Chrome pressing F12) and see if you have any javascript error.

Another possible reason is that on certain pages the script may be loaded from the cache. In this case, consider something in the file name. Example:

<script src="app.js?v=2"></script>
<form id="#formss" method="post"></form:

I hope it helps.

Browser other questions tagged

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