0

I am making a Mail Compose Page.

I want to give the user an option to attach a file. I also want to make the attachment button as an image which I already have.

I have made an image button but the image is acting like a submit button. But I want it to be an Browse button.

My form is:

<form method="post" action="" enctype="multipart/form-data">
    To: <span class="error">* <?php $receiverIdErr ?></span>
    <select id="receiverId" name="receiverId">
        <?php
            $conn = connect();
            $receiverIdQuery = "SELECT Email FROM Users";
            $receiverIdResult = $conn->query($receiverIdQuery);
            if($receiverIdResult->num_rows > 0) {
                while($row = $receiverIdResult->fetch_assoc()) {
                    $Email = $row["Email"];
                    echo "<option value=\"$Email\">" . $row["Email"] . "</option>";
                }
            }
        ?>
    </select>
    <br /><br />
    Subject: <input type="text" name="Subject" required>
    <br /><br />
    Body: <span class="error">* <?php $BodyErr ?></span>
    <textarea name="Body" rows="10" cols="100" maxlength="300" required>
    </textarea>
    <br /><br />
    Attachment:
    <input type="image" src="paperclip4_black.png" width="20" height="20" name="myFile" id="myFile">
    <br /><br />
    <input type="submit" name="Send" value="Send">
</form>

And my PHP is:

<?php
$BodyErr = $receiverIdErr = "";
$Body = $Subject = $receiverId = $filePath = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $Subject = test_input($_POST["Subject"]);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["Body"]))
        $BodyErr = "Body is required";
    else
        $Body = test_input($_POST["Body"]);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["receiverId"]))
        $receiverIdErr = "Choose a receiver";
    else
        $receiverId = test_input($_POST["receiverId"]);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_FILES["myFile"]["name"])) {
        $target_dir = "asset/attachments/" . time();
        $target_file = $target_dir . basename($_FILES["myFile"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

        if ($uploadOk == 0) {
            $message = "Sorry, your file was not uploaded.<br />";
        }
        else {
            if (move_uploaded_file($_FILES["myFile"]["tmp_name"], $target_file)) {
                $filePath = $target_file;
            }
            else {
                $filePath = $target_file;
            }
        }   
    }
}
?>

1 Answers1

0

Might be you are looking for this :

<input type="file" name="fileToUpload" id="fileToUpload">
TIGER
  • 2,864
  • 5
  • 35
  • 45
Michael
  • 405
  • 4
  • 10