Pablo
Pablo

Reputation: 1435

Download a file with alert box using PHP

I'm having a submit form where the form includes the following text fields

When user enters the general password of "DEFAULTDOC" it will automatically download the file. But if not then it will show an alert box that says "Sorry your password is incorrect".

My codes is working fine. But the alert box in

"You are now verified the file will automatically download."

Doesn't show but the downloading of file is working also the inserting of data is working too. Except of showing the alert box. What could be the reason why the alert box doesn't show up?

here's my code: (dlform.php)

<?php
$database = 'db';
$host = 'localhost';
$username = 'username';
$password = 'password';
$e = $_POST['e'];
$p = $_POST['p'];
$f = $_POST['f'];
$l = $_POST['l'];

if($_POST['p'] == "DEFAULTDOC"){
    try
    {
        $connect = new PDO("mysql:host=$host;dbname=$database", $username, $password);
        $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $query = "INSERT INTO dlform VALUES('','$p','$f','$l','$e')";
        $data = $connect->query($query);
        header('Content-type: application/vnd.ms-word');
        header('Content-disposition: attachment; filename=InformationSheet.docm');
    ?>
        <script>
            alert('You are now verified the file will automatically download.');
            window.location.href='downloadform.php';
        </script>
    <?php
    }
    catch(PDOException $error)
    {
        $error->getMessage();
    }
}else{
?>
    <script>
        alert('Sorry your password is incorrect.');
        window.location.href='downloadform.php';
    </script>
<?php
}
?>

Here's my html code:(downloadform.php)

<div class="container" style="margin: 0 auto; width: 600px;margin-top: 10%;">
    <center>
    <img src="images/menu_bar_logo.png" alt ="" style="width: 300px;"/></center>
    <br>
    <div class="row">
        <b>Note:</b>Fill up the following fields to get your form<br>
     <form method="post" action="dlform.php">
    <br>
    <div class="input-group">
      <span class="input-group-addon">First name</span>
      <input id="msg" type="text" class="form-control" name="f" placeholder="First name">
    </div>
    <div class="input-group">
      <span class="input-group-addon">Last name</span>
      <input id="msg" type="text" class="form-control" name="l" placeholder="Last name">
    </div>
    <Br>
    <div class="input-group">
      <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
      <input id="email" type="email" class="form-control" name="e" placeholder="Email">
    </div>
    <div class="input-group">
      <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
      <input id="password" type="password" class="form-control" name="p" placeholder="Password">
    </div>
    <br>
     <input type="submit" name="submit" class="btn btn-primary" value="Submit">
  </form>
</div>

Upvotes: 4

Views: 1460

Answers (1)

david
david

Reputation: 3228

You try to use html tag while dlform.php only has php code. It won't work. Try to set the message into SESSION.

Your code

<script>
    alert('You are now verified the file will automatically download.');
    window.location.href='downloadform.php';
</script>

Change your code into php

$_SESSION['message'] = 'You are now verified the file will automatically download.';
header('Location: downloadform.php');

The same goes for incorrect password one.

And in your downloadform.php, add this code.

<?php if (isset($_SESSION['message'])): ?>
  <div class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">Message</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <p><?php echo $_SESSION['message']; ?></p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary">Save changes</button>
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>
  <?php unset($_SESSION['message']); ?>
<?php endif; ?>

This code is in bootstrap style. This code is not an alert, but a modal similar to alert. It will show up if there is a message from dlform.php. After the message is shown, destroy the message session so it won't appear each time downloadform.php is reload, unless you submit the form.

Upvotes: 2

Related Questions