Page refresh if it works

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 do when to execute my bank command, page reload? What should I put in the ajax return? And I have one more question, in my php file, when one of the things change or the date or the cnpj I enter the command and update, if nothing changes nothing does, and if one changes the other remains the same. How to make others stay the same? It takes and is leaving null when it updates one and does not update the other one.

    
asked by anonymous 22.09.2016 / 13:36

2 answers

1

When you write

echo'to aquii no php';

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

To inform the browser of success or error, use

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

On the other fields, you can include the required attribute in html to force empty values to be sent - it is a superficial protection, since you can still send null values via js. Validation of these values should also be done on the server before inserting into the database.

    
22.09.2016 / 14:03
-1

<?php 

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

?>
    
15.12.2016 / 05:08