1

I have this code to show an alert when the text in the input is changed. the script works well. but anyone can explain to me why I don't see the success alert. but when I click on submit the text on video and on my database is changed only i don't see the success alert. And if I would like to insert instead of alert-success, for example, sweet alert, just replace alert-success with Swal.fire(....) or not?

    $(document).ready(function() {
        $('#updForm').submit(function(e) {
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: 'config/locale.php',
                data: $(this).serialize(),
                success: function(response)
                {
                    var jsonData = JSON.parse(response);

                    // user is logged in successfully in the back-end
                    // let's redirect
                    if (jsonData.success == "1")
                    {
                        alert('perfect!!');
                    }
                    else
                    {
                        alert('Invalid Credentials!');
                    }
               }
           });
         });
    });

    <form id="updForm" method="post" >
    <div class="form-group">
  <label>Descrizione locale</label>
<textarea rows="10" class="form-control" id="description" name="description" spellcheck="true"><?php echo htmlspecialchars($user['description']); ?></textarea>
</div>
  <button type="submit"  class="btn btn-primary"><i class="fa fa-save"></i> Salva dati</button>
    </form>
        <p>
            <a href="reset-password.php" class="btn btn-warning">Reset Your Password</a>
            <a href="logout.php" class="btn btn-danger">Sign Out of Your Account</a>
        </p>
    </body>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

this is php file

require_once "config.php";

// Initialize the session
session_start();

// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
}


$id = $_SESSION["username"];
$sql = "SELECT id_user, username, restaurant_name, description , logo , id_user_type    FROM vm_users WHERE username = ?";       
$statement = $link->prepare($sql);
$statement->bind_param('s', $id);
$statement->execute();
$result = $statement->get_result();
$user = $result->fetch_array(MYSQLI_ASSOC);
$description = $_POST['description'];




if(isset($description)){
$id = $_SESSION["username"];

$query = $link->prepare("UPDATE vm_users SET description= ? WHERE username = ? ");
$query->bind_param('ss', $description, $id);

echo json_encode(array('success' => 1));
$result = $query->execute();

}
else {
    echo json_encode(array('success' => 0));
}


$result->close();
$statement->close();
$link->close();
  • Do you get the 'Invalid Credentials!' alert? Have you checked your browser's network inspector to view the raw response? – Patrick Q Apr 07 '20 at 12:51
  • 1. Can you check the network tab and trace your `XHR` request and response? 2. Yes, you can change the native js `alert` method with any 3rd party you like – Meghdad Hadidi Apr 07 '20 at 13:54
  • no, I don't give anything . the script works but i don't get anything – Fabio Koichi Begnini Apr 07 '20 at 13:55
  • I have checked my xhr and the status code is: 500 internal server error. but the response is {"success":1} – Fabio Koichi Begnini Apr 07 '20 at 13:59
  • 500 status means check your error logs – Patrick Q Apr 07 '20 at 14:51
  • I understood that the error is when I insert success echo in the php file because the error echo works perfectly – Fabio Koichi Begnini Apr 07 '20 at 15:41
  • i have removed the last 3 lines. and now works.. but i don't understand why.. – Fabio Koichi Begnini Apr 07 '20 at 15:47
  • Your error logs would have told exactly you why. Most likely `close()` is not a function on two of those three classes. – Patrick Q Apr 07 '20 at 15:53
  • sorry for my little experience. but from the inspection of chrome. how do i see errors exactly? – Fabio Koichi Begnini Apr 07 '20 at 15:56
  • This has nothing to do with Chrome. The error would be in your PHP error log. [See here](https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel) for some examples of where they _might_ be (you could have different settings). [See here](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) and [here](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) to learn how to see the errors in the output. – Patrick Q Apr 07 '20 at 17:36

0 Answers0