insert data with ajax and switch in php

Asked

Viewed 153 times

-4

I have a php script that I use GET to take a parameter from url and move on to a php switch and perform a certain function.

That one script works perfectly without errors but how to use it with ajax??

When I call the ajax with the url

url: cor.php, (file without switch) it records the data perfectly but when the ajax flame

url: cor.php?acao=inserir, (in this case, I would call the php file with switch) it shows error and does not proceed, it does not find the switch function

Can someone help me with that?

Switch structure:

<?php 
session_start();
if (empty($_SESSION['id_usuario'])){
    echo "Acesso negado!";
    exit;
}else{
    include "conexao.php";

    $id = (int)$_GET["id"];
    $acao = $_GET['acao'];

    switch ($acao) {


        case alterar:


        break;

        case excluir:


        break;

        case bloquear:



        case ativar:


        break;


        case cadastroCor:


        break;

    }}
    ?>
  • If you’re talking about switch of PHP, you’d better edit that question and show the script error.

  • Please, we need the code to help you - or at least part of it.

  • friends , if you know what ajax and switch is in php, the code is not necessary, so you must know how to make the ajax call a switch function from the php file and pass the data to it

  • Without the correct posting of your code, it is difficult to help you, the switch case you posted for example is treating the strings incorrectly, for example. Post php code and your view to be parsed.

1 answer

1

$.ajax({
url:'cor.php', data : {acao:'inserir'}, dataType : 'GET'
})
.done(function (retorno) {
//
});

In this case the action is how the variable is written in the Php code.

  • and tried this way but it didn’t work. with the file without switch it works perfect

  • 1

    Send the structure of your switch so we can see how ta.. How much the JS would look like: $. ajax({ url: 'url of destination', method: 'POST', date: 'acao=insert&' . $('form[name="name"]'). serialize(), beforeSend: Function(){ //If you want to give something to the user while performing the process }, Success: Function(data){ console.log(data); } });

  • This action is being passed as? In the method as it is?

  • @Marcusmaciel is there the structure of the switch , Hercules , I will test this way Voce said

  • @Herbertjunior posted the switch structure for a look

  • Right, and the error it gives ? It displays the null value?

  • @Herbertjunior does not load any switch in the script for when it arrives in php

  • Use Chrome in developer mode by pressing F12 and look if it doesn’t give 404 error or something ... The problem may be when calling the method ...

Show 3 more comments

Browser other questions tagged

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