Skip to content
Advertisement

Update MySQL record from PHP using AJAX & modal

Iam trying to make update option with modal form, I already made “ADD New” – works fine, but have alot problems with Update – I want to see actual data on modal form to edit this. Can u maybe help me with this one? I need help only with function part.

Thats my code for Add New one:

  public function dodajAction()
{
    $request = $this->getRequest();
    $jsonModel = new JsonModel();

    if ($request->isPost()) {
        $dane = $request->getPost();

        $formDodaj = new FormAdmOfertaForm();
        $formDodaj->setData($dane);

        if ($formDodaj->isValid()) {
            // dodaj
            $this->oferta->dodaj($dane);

            $jsonModel->setVariables(['wynik' => true]);
        } else {
            // w razie błędu renderuj formularz
            $viewModel = new ViewModel(['form_dodaj' => $formDodaj]);
            $viewModel->setTerminal(true);
            $viewModel->setTemplate('admin/oferty/dodaj');

            $jsonModel->setVariables(['wynik' => false, 'html' => $this->renderer->render($viewModel)]);
        }
    }

    return $jsonModel;
}

And thats modal form:

<div class="modal fade" id="modalDodajOferte" tabindex="-1" role="dialog" aria-labelledby="modalDodajOferteLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="modalDodajOferteLabel">Dodaj ofertę</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <?=$this->partial('admin/oferty/dodaj', ['form_dodaj' => $this->form_dodaj]) ?>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="btnDodajOferte">Dodaj</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Zamknij</button>
            </div>
        </div>
    </div>
</div>

Advertisement

Answer

Just an advice from a fellow Pole, PLEASE!!! use english words in your code. This improves readability, especially that later on you might find tools that help you by using defaults if you follow naming standards. e.g. $form->offer->add() is way more readable to anyone on stack overflow, than our native ->oferta->dodaj().

Coming back to the question though, you have not supplied us with any way to see how the update is performed on the system

I believe that

if ($formDodaj->isValid()) {
        // dodaj
        $this->oferta->dodaj($dane);

        $jsonModel->setVariables(['wynik' => true]);

is responsible for sending the actual data to the database, however code for updating that should be completely in a different function, same for viewing.

It sounds more like an XY problem here, so you might need to rethink what you actually need us to help you with.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement