My webpage doesn't redirect and echo the pop-up note i want

<?php
// Database connection details
$host = 'localhost';
$dbName = '';
$username = '';
$password = '';
$tableName = 'nft';
$uploadDirectory = 'uploads/';

require_once "authe.php";
// Assuming you have a user authentication system in place,
// retrieve the username of the logged-in user from the "users" table
$loggedInUserId = $_SESSION['username']; // Replace with the appropriate session variable for the logged-in user's ID
$loggedInUser = '';

try {
    // Establish a connection to the database
    $db = new PDO("mysql:host=$host;dbname=$dbName", $username, $password);

    // Create the table if it doesn't exist
    $db->exec("CREATE TABLE IF NOT EXISTS $tableName (id INT AUTO_INCREMENT PRIMARY KEY, filename VARCHAR(255), url VARCHAR(255), nftby VARCHAR(255), fileprice VARCHAR(255), filedescription VARCHAR(255), filesize VARCHAR(255), fileprop VARCHAR(255), filetype VARCHAR(255), fileroyal VARCHAR(255), filecoll VARCHAR(255), filebid VARCHAR(255))");

    // Retrieve the logged-in user's username from the "users" table
    $statement = $db->prepare("SELECT username FROM users WHERE username = ?");
    $statement->execute([$loggedInUserId]);
    $result = $statement->fetch(PDO::FETCH_ASSOC);

    if ($result) {
        $loggedInUser = $result['username'];
    }

    // Handle the file upload
    $file = $_FILES['file'];
    $filename = $_POST['filename'];
    $fileprice = $_POST['fileprice']; // Replace with the actual name of the additional field
    $filedescription = $_POST['filedescription']; // Replace with the actual name of the additional field
    $filesize = $_POST['filesize']; // Replace with the actual name of the additional field
    $fileprop = $_POST['fileprop']; // Replace with the actual name of the additional field
    $filetype = $_POST['filetype']; // Replace with the actual name of the additional field
    $fileroyal = $_POST['fileroyal']; // Replace with the actual name of the additional field
    $filecoll = $_POST['filecoll']; // Replace with the actual name of the additional field
    $filebid = $_POST['filebid']; // Replace with the actual name of the additional field

    // Generate a unique file name
    $extension = pathinfo($file['name'], PATHINFO_EXTENSION);
    $newFilename = uniqid() . '.' . $extension;

    // Move the uploaded file to the uploads directory with the new file name
    $destination = $uploadDirectory . $newFilename;
    if (move_uploaded_file($file['tmp_name'], $destination)) {
        // Store the filename, URL, username, and additional fields in the table
        $statement = $db->prepare("INSERT INTO $tableName (filename, url, nftby, fileprice, filedescription, filesize, fileprop, filetype, fileroyal, filecoll, filebid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $url = 'https://example.com/translated/user/' . $destination;  // Replace with your actual domain or URL
        $statement->execute([$filename, $url, $loggedInUser, $fileprice, $filedescription, $filesize, $fileprop, $filetype, $fileroyal, $filecoll, $filebid]);

         // Display a pop-up message and redirect to a different page
        echo '<script>alert("File uploaded successfully."); window.location.href = "http://example.com/success-page.php";</script>';
        exit;
    } else {
        echo 'Error uploading file.';
    }

    // Close the database connection
    $db = null;
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
?>

Instead of displaying the pop-up message and redirecting to the new page, it just echos it as text after uploading.

I don’t see session_start(); at the top of the page. It should be at the top before output to the browser and before trying to get or set any session values.

It is in the authe.php file

The alert and redirect work for me. Therefore, there must be something going on that’s preventing the javascript from being seen as javascript.

What does the ‘view source’ of the web page in your browser show?

I tested with a small image and the file was uploaded, database insert successful, popup shown and directed to your success page. When testing a larger file, it failed simply because the file size exceeded my local settings.

When testing, isolate sections of code especially any redirects so you can echo things to the page and see any errors that might be shown. I started by commenting out the move_uploaded_file() section and checked to see that the new DB table was being created. I then printed out POST and FILES to see what is being sent.

echo "<pre>";	
print_r($_POST);
print_r($_FILES); 
echo "</pre>";
			 
		/*
    if (move_uploaded_file($file['tmp_name'], $destination)) {
        // Store the filename, URL, username, and additional fields in the table
        $statement = $db->prepare("INSERT INTO $tableName (filename, url, nftby, fileprice, filedescription, filesize, fileprop, filetype, fileroyal, filecoll, filebid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $url = 'https://xclusivedesign.io/translated/user/' . $destination;  // Replace with your actual domain or URL
        $statement->execute([$filename, $url, $loggedInUser, $fileprice, $filedescription, $filesize, $fileprop, $filetype, $fileroyal, $filecoll, $filebid]);

         // Display a pop-up message and redirect to a different page
        echo '<script>alert("File uploaded successfully."); window.location.href = "http://example.com/success-page.php";</script>';
        exit;
    } else {
        echo 'Error uploading file.';
    }
		*/

I noticed $_FILES['file']['error'] was showing a value of 1.
You might be having the same issue.

https://www.php.net/manual/en/features.file-upload.errors.php

Also Instead of echoing “Error uploading file.” you might give the user a more meaningful message and direct them back to the FORM page. I just used index.php in this example.

echo '<script>alert("The uploaded file exceeds the max allowed size. Please try again."); window.location.href = "index.php";</script>';

Also noting that we don’t see your form code so double check those form input names against the names in the processing section, if you are still having problems.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.