Refresh page if successful

Asked

Viewed 52 times

1

This is my php code:

if(isset($_POST['menu'])){


$menu               = $_POST['menu'];
$dateFoundation     = $_POST['date'];
$cnpj               = $_POST['cnpj'];
$latitude           = $_POST['latitude'];
$longitude          = $_POST['longitude'];
$link               = $_POST['link'];
$site               = $_POST['site'];
$facebook           = $_POST['facebook'];
$instagram          = $_POST['instagram'];
$googleplus         = $_POST['googleplus'];
$pinterest          = $_POST['pinterest'];


$stmtUpdateMoreInfo = $conn->prepare("SELECT menu.fundation_date, menu.cnpj, menu.link, address.lat, address.lng
                                     FROM menu
                                     LEFT JOIN public.address ON menu.address_id = address.address_id
                                     WHERE menu_id = :menu");

$stmtUpdateMoreInfo->bindValue(":menu", $menu);
$stmtUpdateMoreInfo->execute();
$informations = $stmtUpdateMoreInfo->fetch(PDO::FETCH_OBJ);


echo'to aquii no php';


if ($informations->fundation_date !== $dateFoundation) {

    $newDate = ($informations->fundation_date !== $dateFoundation) ? correctBadWords($dateFoundation) : $informations->fundation_date;
    echo'variável do banco de dados? '.$informations->fundation_date;
    echo'variável nova '.$newDate;
}


if ($informations->cnpj !== $cnpj) {

    $newCnpj = ($informations->cnpj !== $cnpj) ? correctBadWords($cnpj) : $informations->cnpj;
    echo'cnpj que está no banco de dados?  '.$informations->cnpj;
    echo'cnpj novo?  '.$cnpj;

}

if ($informations->fundation_date !== $dateFoundation || $informations->cnpj !== $cnpj) {
    $stmtUpdateInformations = $conn->prepare("UPDATE menu SET fundation_date = :date, cnpj = :cnpj WHERE menu_id = :menu");
    $stmtUpdateInformations->bindValue(":date", $newDate);
    $stmtUpdateInformations->bindValue(":cnpj", $newCnpj);
    $stmtUpdateInformations->bindValue(":menu", $menu);

    if($stmtUpdateInformations->execute()){
        echo 'true';
    }else{
        echo'false';
    }
}

This my javascript:

 if (moreInformations) {
       $.ajax({
        url: './model/functions/more_informations.php',
        type: 'POST',
        data: {date: $("#date-foundation").val(), cnpj: $("#cnpj").val(),
        latitude: $("#latitude").val(), longitude: $("#longitude").val(),
        link: $("#link").val(), site: $("#site").val(), facebook: $("#facebook").val(),
        instagram: $("#instagram").val(), googleplus: $("#googleplus").val(), pinterest: $("#pinterest").val(),
        menu: $("#menuid").val()},
        success: function (data) {
            alert('to aqui'+data);
            if(data === 'true'){
                location.reload(true);
            }else{
                alert('deu pau');
            }
        }
    });
    }

});

How do I execute my bank command, reload the page? What should I put in the return of ajax? And I have one more question, in my php file, when one of the things change or the date or cnpj I enter the command and update, if not change nothing does nothing, and if change one another remains the same. How to make the other remain the same? It picks up and is leaving null when it updates one and does not update the other.

2 answers

1

When you write

echo'to aquii no php';

You return a page to the user, which the $.ajax recognizes how success - that is, anything you write will count as success. You can only give echo something after the transaction with the database is complete.

To inform the browser if there was a success or error, use

http_response_code(501) //erro interno ou 
http_response_code(200) //sucesso

On the other fields, you can include the attribute required in html to force empty values to be sent - is a surface protection, as it will still be possible to send null values via js. The validation of these values should also be done on the server before entering in the database.

-1

<?php 

echo '<script>location.reload();</script>';

?>

Browser other questions tagged

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