Jquery-Send Post to save to database

Asked

Viewed 65 times

0

I’m developing a site that takes the data of a particular location and when clicking the button it has to save elsewhere and for this I’m using Jquery but I’m not able to save in my bank someone can help me.

code that contains the contents

$(document).ready(function(){
        $.getJSON("myonsala", function(result){
            $("form div.online").empty();
                $.each(result, function(i, field){


                    $("form div.online").append("<div class=\"user impar\">" + "<figure>" + "<img src=\"img/users/Profiles/"+field.foto+"\">" + 
                    "</figure>"+"<span class=\"nickname\">"+field.Ninck +"<span class=\"cartaoAmarelo cartaoAmarelo0\">"+"</span>"
                    +"</span>"+"<p class=\"desafiar\" onclick=\"desafio();\">"+" DESAFIAR!"+"<input  name=\"desafiado\" type=\"hidden\" value="+field.Id+">"+
                    " </p>"+ "</div>");

            });
            $("span#onsala").empty();  
            $("span#onsala").append(result.length);

        });
});

It has to take the value that will come from the field desafiado and send to php page for this.

php page:

      if(isset($_POST['desafiado'])){
            if(isset($_SESSION['sala'])){
                if(isset($_SESSION['tkm'])){
                    $desafiado = $_POST['desafiado'];
                    //$desafiado=16;
                    $sala=$_SESSION['sala'];
                    $tkm=$_SESSION['tkm'];
                    $results = DB::select("SELECT Id,Ninck FROM usuarios WHERE Has='$tkm'");
                    foreach($results as $row){
                        $Id=$row->Id;
                        $Nick=$row->Ninck;
                    }
                    $results = DB::select("SELECT Ninck FROM usuarios WHERE Id='$desafiado'");
                    foreach($results as $row){  
                        $Nickop=$row->Ninck;
                    }
                    $results = DB::select("SELECT s.Valor as valors FROM onsalas o,salas s WHERE o.Idsala=s.Id");
                    foreach($results as $row){  
                        $valor=$row->valors;
                    }
                    //dd($valor);
                    $cron = '02:40';
                    $date =date('Y-m-d H:i:s');
                    $datec = date('Y-m-d');
                    $horac= date('H:i:s');
                    $inserir= DB::table('Partidas')->insert(
                        ['IdDesafiante'=>$Id,'NickDesafiante'=>$Nick,'IdDesafiado'=>$desafiado,'NickDesafiado'=>$Nickop,'Aceitou'=>0,'CronDesafiante'=>$cron,'CronDesafiado'=>$cron,'Valor'=>$valor,'IdSala'=>$sala,'CanceladoPreJogo'=>0,'IdCanceladorPreJogo'=>0,'Motivo'=>0,'AbamdonarPartida'=>0,'IdAbandonarPartida'=>0,'Divegencia'=>0,'DataCriacao'=>$datec,'HoraCriacao'=>$horac,'HoraAceito'=>0,'HoraFimPrejogo'=>0,'HoraFinal'=>0,'updated_at'=>$date,'created_at'=>$date]    
                    );
                    dd($inserir);
                    $banco='f';
                    return response()->json($banco); 
                }else{
                    echo "erro1";
                }
            }else{
                echo "erro3";
            }           
        }else{
            echo "erro3";
        }

Someone can help me. I’m using jquery pq n I can give Submit on the page because I don’t want it to reload

update dia 16/07/2018 using the ajax

function desafio(){
$('form').submit(function(e){
    e.preventDefault();
    var data=$(this).serializeArray();
    $.ajax({
     url: "desafio",
     type: "post",
     dataType: "json",
     data: data,   
   })
   .done(function() {
     alert( "success" );
   })
   .fail(function() {
     alert( "error" );
   })
   .always(function() {

   });
})

}

now he gives the supmit put as GET and on the page q I am n on the page I put for her to go

  • You can use Ajax to perform this procedure or if you prefer Axios Below is a link to both documents: http://api.jquery.com/jquery.ajax/ https://github.com/axios/axios

  • @Lucasbrogni, it worked thanks, but now it is submitting the page and did not want what happened to it ?

  • you are using ajax or Axios?

  • @Lucasbrogni am using. I will update the post to show

  • removes Submit --> $('form'). Submit(Function(e)

  • @Lucasbrogni is still giving Submit

Show 1 more comment
No answers

Browser other questions tagged

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