I have a site with a few forms that’s existed for about a year and has always worked when I submit the forms it until the past couple weeks. When the required info is filled out upon submission, the form would previously refresh and display a success message to the user. Now, when I click submit, I am redirected to a blank page.
After some trouble shooting, I found out that if I add an image, everything redirects as it should. However, if the image field is left blank, I am directed to a blank page even though the url in the browser is displaying correctly. I found that if I also remove the image upload portion of the PHP/HTML, the redirect works.
If anyone could give me any suggestions as to how to rectify this issue, I’d greatly appreciate it. I’m hoping it’s a simple fix for some of these that I just happen to be overlooking. I will be happy to clarify anything that isn’t explained well enough the first time around.
Both the HTML and PHP exist in create.php
. I will include all PHP and only the pertinent HTML. create.php
is using the database table post
.
create.php PHP
<?php
session_start();
$msg = "";
//connect to the database
include('../includes/db_connect.php');
if (!isset($_SESSION['user_id'])) {
header('Location: ../login.php');
exit();
}
$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];
$url = $_SESSION['url'];
if(isset($_POST['submit'])){
$title = $_POST['title'];
$link = $_POST['link'];
$description = $_POST['description'];
$title = $db->real_escape_string($title);
$link = $db->real_escape_string($link);
$description = $db->real_escape_string($description);
$start = date('Y-m-d', strtotime($_POST['start']));
$end = date('Y-m-d', strtotime($_POST['end']));
$user_id = $_SESSION['user_id'];
if($title && $description){
$query = $db->query("INSERT INTO post (user_id, title, link, description, start, end) VALUES('$user_id', '$title', '$link', '$description', '$start', '$end')");
if($query){
$_SESSION['message'] = "Offer posted! Click <a href='myoffers.php'>here</a> to view your current offer overview.";
}else{
$_SESSION['message'] = "error";
}
}else{
$_SESSION['message'] = "Please make sure all required fields are filled out!";
}
$postid = $db->insert_id;
// IMAGE UPLOAD CODE
for($i=0; $i<count($_FILES["images"]["name"]); $i++)
{
$filetmp = $_FILES["images"]["tmp_name"][$i];
$filetype = $_FILES["images"]["type"][$i];
$allowed_types =array("jpg", "png", "jpeg", "mp4", "mov", "wmv", "flv", "avi", "gif");
$filename = $_FILES["images"]["name"][$i];
$error = null;
// Get the file extension
$extension = pathinfo($filename, PATHINFO_EXTENSION);
// Search the array for the allowed file type
if (in_array($extension, $allowed_types, false) != true) {
$error = "ERROR: ILLEGAL FILE TYPE";
return $error; // or use exit;
}
$temp = explode(".", $_FILES["images"]["name"]);
$newfilename = time() . '_' . $filename . end(explode($_FILES["images"]["name"]));
$filepath = "images/".$newfilename;
move_uploaded_file($filetmp, $filepath);
$sql = "INSERT INTO images (img_name, img_path, img_type, post_id) VALUES ('$newfilename', '$filepath', '$filetype', '$postid')";
$result = mysqli_query($db, $sql);
}
// END IMAGE UPLOAD CODE
}
?>
create.php form HTML
<div class="alert alert-error">
<?php echo $_SESSION['message']; ?>
</div>
</center>
<article>
<form action="create.php" method="post" enctype="multipart/form-data">
<label>Campaign Title *</label><br>
<input type="text" name="title" class="form-control" placeholder="Please enter the title of this offer" required>
<br>
<label>Description *</label><br>
<textarea type="textarea" name="description" id="description" class="form-control" placeholder="Please enter a brief description of this offer" required></textarea>
<br>
<label>Link</label><br>
<input type="text" name="link" class="form-control" placeholder="Include a link to an external site if you would like">
<br>
<label>Image</label><br>
<input type="hidden" name="size" value="1000000">
<input multiple="multiple" name="images[]" type="file" />
<br>
<label>Campaign Start Date *</label><br>
<input type="date" name="start" value="<?php echo date('Y-m-d'); ?>" required>
<br><br>
<label>Campaign End Date *</label><br>
<input type="date" name="end" value="<?php echo date('Y-m-d'); ?>" required>
<br><br>
<div class="required">
* indicates a required field
</div>
<button type="submit" name="submit" value="Submit"class="btn btn-default">Submit</button>
</form>
</article>