2016-04-18 12 views
0

Ich habe ein Bearbeitungsformular, wo ein Administrator Buchdetails bearbeiten kann. Nach dem Senden dieses Formulars werden die Werte in der Datenbank aktualisiert, und die Seite sollte die aktualisierten Werte in das Formular laden (ohne die Seite zu aktualisieren/neu zu laden).Laravel/Ajax Grab Werte aus der Datenbank nach dem Update

Ich habe die Werte eingegeben werden in der Datenbank in Ordnung immer aktualisiert, aber sie gehen nicht in die Eingabefelder geladen .. nur mein Code, um die „alten“ Werte packt und steckt sie wieder in den Bereichen ..

Nur um es Schritt für Schritt zu erklären, verwenden wir "Titel" als das Feld, das wir für das Beispiel ändern werden. Ich habe ein Buch namens "Old Book". Ich gebe auf dem Bearbeitungsformular einen neuen Titel "Neuer Titel" ein. Ich klicke auf "Senden". Die Seite scrollt zurück nach oben (wie es sollte), aber das Eingabefeld zeigt immer noch "Alter Titel" an. Wenn ich dann die Seite aktualisiere (F5), zeigt das Eingabefeld jetzt "Neuer Titel" an.

Ich möchte das oben genannte passieren, ohne die Seite zu aktualisieren.

Hier ist der Bearbeitungsseite Javascript/Ajax:

$(document).ready(function() 
{ 
    $('#editbook_form').bootstrapValidator(
    { 
     feedbackIcons: 
     { 
      valid: 'glyphicon glyphicon-ok', 
      invalid: 'glyphicon glyphicon-remove', 
      validating: 'glyphicon glyphicon-refresh' 
     }, 
     fields: 
     { 
      eb_title: 
      { 
       validators: 
       { 
        notEmpty: 
        { 
         message: 'You must enter a book title' 
        } 
       } 
      }, 
      eb_insoft: 
      { 
       validators: 
       { 
        notEmpty: 
        { 
         message: 'You must enter a value for In Soft' 
        } 
       } 
      }, 
      eb_inhard: 
      { 
       validators: 
       { 
        notEmpty: 
        { 
         message: 'You must enter a value for In Hard' 
        } 
       } 
      } 
     }, 
    }) 
    .on('success.form.bv', function(e) 
    { 
     // Prevent form submission 
     e.preventDefault(); 

     // Get the form instance 
     var $form = $(e.target); 

     // Get the BootstrapValidator instance 
     var bv = $form.data('bootstrapValidator'); 


     title = $("#title").val(); 
     m_keywords = $("#eb_mkeywords").val(); 
     m_description = $("#eb_mdescription").val(); 
     description = $("#eb_description").val(); 
     electronic_price = $("#eb_electronicprice").val(); 
     audio_price = $("#eb_audioprice").val(); 
     soft_price = $("#eb_softprice").val(); 
     hard_price = $("#eb_hardprice").val(); 
     in_soft = $("#eb_insoft").val(); 
     in_hard = $("#eb_inhard").val(); 
     status_id = $("#eb_statusid").val(); 
     isbn = $("#eb_isbn").val(); 
     date_published = $("#eb_datepublished").val(); 
     notes = $("#eb_notes").val(); 

     console.log("BEFORE AJAX CALL"); 

     $.ajax(
     { 
      type: "POST", 
      //url: base_url+"/book/updateBook", 
      url: "[[URL::to('book/updateBook')]]", 
      dataType : 'json', // expected returned data format. 
      data: 
      { 
       book_id: window.book_id, 
       title: title, 
       m_keywords: m_keywords, 
       m_description: m_description, 
       description: description, 
       electronic_price: electronic_price, 
       audio_price: audio_price, 
       soft_price: soft_price, 
       hard_price: hard_price, 
       in_soft: in_soft, 
       in_hard: in_hard, 
       status_id: status_id, 
       isbn: isbn, 
       date_published: date_published, 
       notes: notes, 
      }, 
      success: function(data) 
      { 
       if(data.valid==true) 
       { 
        console.log("DATA VALID IS TRUE"); 
        //alert("VALID: " + data.valid + "\nTITLE: " + data.title); 


        $("#edit_err").removeClass('text-danger').addClass('text-success'); 
        $("#edit_err").html(data.message); 

        oldInSoft = ""; 

        $('#editbook_form').data('bootstrapValidator').resetForm(); 
        $('#editbook_form')[0].reset(); 

        //location.reload(); 
        window.scrollTo(0,0); 

        //$('#eb_message').html("Book successfully updated!"); 

        $('#title').html("[[ $book->title ]]"); 
        $('#eb_mkeywords').html("[[ $book->m_keywords ]]"); 
        $('#eb_mdescription').html("[[ $book->m_description ]]"); 
        $('#eb_description').html("[[ $book->description ]]"); 
        $('#eb_electronicprice').html("[[ $book->electronic_price ]]"); 
        $('#eb_audioprice').html("[[ $book->audio_price ]]"); 
        $('#eb_softprice').html("[[ $book->soft_price ]]"); 
        $('#eb_hardprice').html("[[ $book->hard_price ]]"); 
        $('#eb_insoft').html("[[ $book->in_soft ]]"); 
        $('#eb_inhard').html("[[ $book->in_hard ]]"); 
        $('#eb_statusid').html("[[ $book->status_id ]]"); 
        $('#eb_isbn').html("[[ $book->isbn ]]"); 
        $('#eb_datepublished').html("[[ $book->date_published ]]"); 
        $('#eb_notes').html("[[ $book->notes ]]"); 
       } 
       else 
       { 
        console.log("DATA VALID IS FALSE"); 
        $("#edit_err").addClass("text-danger"); 
        $("#edit_err").html(data.message); 
       } 
      }, 
      beforeSend:function() 
      { 
       console.log("INSIDE BEFORESEND"); 
       $("#edit_err").html("Loading..."); 
      } 
     }); 

     return false; 
    }); 
}); 

Hier ist der Controller:

public function updateBook(Request $request) 
{ 
    $valid = false; 
    //$data = Input::all(); 
    //$message = ''; 

    $id = $request->input('book_id'); 
    $title = $request->input('title'); 
    $m_keywords = $request->input('m_keywords'); 
    $m_description = $request->input('m_description'); 
    $description = $request->input('description'); 
    $electronic_price = $request->input('electronic_price'); 
    $audio_price = $request->input('audio_price'); 
    $soft_price = $request->input('soft_price'); 
    $hard_price = $request->input('hard_price'); 
    $in_soft = $request->input('in_soft'); 
    $in_hard = $request->input('in_hard'); 
    $status_id = $request->input('status_id'); 
    $isbn = $request->input('isbn'); 
    $notes = $request->input('notes'); 
    $date_published = $request->input('date_published'); 


    $valid = DB::table('book')->where('book_id', $id)->update(
     [ 
      'title' => $title, 
      'm_keywords' => $m_keywords, 
      'm_description' => $m_description, 
      'description' => $description, 
      'electronic_price' => $electronic_price, 
      'audio_price' => $audio_price, 
      'soft_price' => $soft_price, 
      'hard_price' => $hard_price, 
      'in_soft' => $in_soft, 
      'in_hard' => $in_hard, 
      'status_id' => $status_id, 
      'isbn' => $isbn, 
      'date_published' => $date_published, 
      'notes' => $notes 
     ] 
     ); 

    if($valid) 
    { 
     return response()->json(array('valid' => true,'message' => 'Book successfully updated!')); 
    } 
    else 
    { 
     return response()->json(array('valid' => false,'message' => 'Book not updated, please fix any errors')); 
    } 

} 

Jede Hilfe wird sehr geschätzt!

Antwort

0

Ich denke, dass Sie die alten Daten auf dem Objekt, das Sie verwenden, nicht aktualisieren. Sie erstellen einfach eine neue Variable wie folgt. und sie an die Update-Methode senden. Sie verwenden jedoch die alten Daten des alten Objekts. Buch $

$id = $request->input('book_id'); 
    $title = $request->input('title'); 
    $m_keywords = $request->input('m_keywords'); 
    $m_description = $request->input('m_description'); 
    $description = $request->input('description'); 
    $electronic_price = $request->input('electronic_price'); 
    $audio_price = $request->input('audio_price'); 
    $soft_price = $request->input('soft_price'); 
    $hard_price = $request->input('hard_price'); 
    $in_soft = $request->input('in_soft'); 
    $in_hard = $request->input('in_hard'); 
    $status_id = $request->input('status_id'); 
    $isbn = $request->input('isbn'); 
    $notes = $request->input('notes'); 
    $date_published = $request->input('date_published'); 

versuchen, die Daten im Objekt aktualisiert $ this- mit> title = ... und so weiter.

+0

$ this Verwendung in der Editseiten Erfolg Block gibt nur einen Fehler 'nicht definierte Eigenschaft: Illuminate \ View \ Engines \ CompilerEngine :: $ title (Ansicht: C: \ wamp64 \ www \ resources \ views \ admin \ editBook ruft. blade.php) '. Wenn Sie es im Controller verwenden, um die Variablen zu deklarieren, erhalten Sie einen internen Fehler von 500 $ $ this -> $ title = $ request-> input ('title'); ' – WolfieeifloW

Verwandte Themen