Jump to content

How to ignore empty fields in form [RESOLVED]


AdRock

Recommended Posts

I have found a great image manipulation script which i want to use with my form.

In my form I have a 'file' field where i can select an image to upload and when I click submit the image is resized and the name stored in the database.

The problem I'm having is if I don't select an image then an error message is thrown up saying that I haven't chosen an image to resize.  Sometimes I don't want an image so I how do I bypass the part what does the image upload and resize?

Here is my entire script whcih works if i select an image

[code]// This is displayed when the email has been sent
$thankyou_message = "<p class='style3'>Thankyou. Article successfully added.</p><p>Click <a class='two' href='index.php'>here</a> to go back to Jack Godfrey Honeylands Support Fund Admin Area main menu.</p>";


//This is the directory where images will be saved
$target = "../images/";
$target = $target . basename( $_FILES['image']['name']);

//This gets all the other information from the form
$name = stripslashes($_POST['txtName']);
$message = stripslashes($_POST['txtMessage']);
$pic=($_FILES['image']['name']);

$th_file_name = 'images/thumbs/' . $_FILES['image']['name'];

if ($_POST) {
    // No image?
    if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
        die ('<strong>Invalid image uploaded.  Please go back and try again.</strong>');
    }

    $imagepath = $_FILES['image']['tmp_name'];

    // Load image
    $image = open_image($imagepath);

    if ($image == false) {
        die ('<strong>You uploaded an invalid image. Please go back and try again.</strong>');
    }

    // Get original width and height
    $width = imagesx($image);
    $height = imagesy($image);

    // Percentage?
    if (!empty($_POST['percent']) AND is_numeric($_POST['percent'])) {
        $percent = floatval($_POST['percent']);
        $percent = $percent/100;

        $new_width = $width * $percent;
        $new_height = $height * $percent;

    // New width? Calculate new height
    } elseif (!empty($_POST['new_width']) AND is_numeric($_POST['new_width'])) {
        $new_width = floatval($_POST['new_width']);
        $new_height = $height * ($new_width/$width);

    // New height? Calculate new width
    } elseif (!empty($_POST['new_height']) AND is_numeric($_POST['new_height'])) {
        $new_height = floatval($_POST['new_height']);
        $new_width = $width * ($new_height/$height);

    // New height and new width
    } elseif (!empty($_POST['height']) AND is_numeric($_POST['height']) AND !empty($_POST['width']) AND is_numeric($_POST['width'])) {
        $new_height = floatval($_POST['height']);
        $new_width = floatval($_POST['width']);
    } else {
        die ('<strong>You didn\'t specify any resizing options.</strong>');
    }     

    // Resample
    $image_resized = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    // Display resized image 
    imagejpeg($image_resized,$th_file_name,100);
    die();
}

// Display the upload form:


if (!isset($_POST['txtName'])) {

?>

<h2>Add a general article</h2>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    <p class="style1">Please enter a title for the general article.
    <input class="form" type="text" title="Please enter a title for the general article" name="txtName" size="30"/></p>

    <p class="style1">Please enter the content for the general article.
    <textarea class="form" title="Please enter the content for the general article" name="txtMessage" rows="25" cols="30"></textarea></p>

    <fieldset>

    <legend><b>Image Dimensions</b></legend>

    <div><label for "image">Image:</label>
    <input type="file" name="image"></br></div>

    <div><label for "percent">Resize to:</label>
<input type="text" name="percent" size="1" /> % (percentage)</div>

    <div><label for "new_width">OR new width:</label>
    <input type="text" name="new_width" size="1" /> pixels (height will be calculated automatically)</br></div>
   
    <div><label for "new_height">OR new height:</label>
    <input type="text" name="new_height" size="1" /> pixels (width will be calculated automatically)  </br></div>

<div><label class="left" for "both">OR new height and new width:</label>
<input type="text" name="width" size="1" /> pixels</br>
     
<input type="text" name="width" size="1" /> pixels</br></div>
    </fieldset>
    <p class="style3">
    <input class="submit-button" style="margin-left:0"  type="Submit" value="Submit"><input class="submit-button" style="margin-left:2px" type="reset" value="Reset">
</form>
<?php
}

elseif (empty($name) || empty($message)) {

    echo $empty_fields_message;

}

else {

    // Stop the form being used from an external URL
    // Get the referring URL
    $referer = $_SERVER['HTTP_REFERER'];
    // Get the URL of this page
    $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    // If the referring URL and the URL of this page don't match then
    // display a message and don't send the email.
    if ($referer != $this_url) {
        echo "You do not have permission to use this script from another URL.";
        exit;
    }

    include_once("../includes/connection.php");
    // The URLs matched so send the email

    $query = "INSERT INTO articles VALUES ('','$name','$message',now(),'$pic')";
    mysql_query($query);

    mysql_close();

    // Display the thankyou message
    echo $thankyou_message;
   
}
function open_image ($file) {
    # JPEG:
    $im = @imagecreatefromjpeg($file);
    if ($im !== false) { return $im; }

    # GIF:
    $im = @imagecreatefromgif($file);
    if ($im !== false) { return $im; }

    # PNG:
    $im = @imagecreatefrompng($file);
    if ($im !== false) { return $im; }

    # GD File:
    $im = @imagecreatefromgd($file);
    if ($im !== false) { return $im; }

    # GD2 File:
    $im = @imagecreatefromgd2($file);
    if ($im !== false) { return $im; }

    # WBMP:
    $im = @imagecreatefromwbmp($file);
    if ($im !== false) { return $im; }

    # XBM:
    $im = @imagecreatefromxbm($file);
    if ($im !== false) { return $im; }

    # XPM:
    $im = @imagecreatefromxpm($file);
    if ($im !== false) { return $im; }

    # Try and load from string:
    $im = @imagecreatefromstring(file_get_contents($file));
    if ($im !== false) { return $im; }

    return false;
}
?>[/code]

and this is the bit that I think is causing the problems and the bit that needs to be bypassed if required

[code]if ($_POST) {
    // No image?
    if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
        die ('<strong>Invalid image uploaded.  Please go back and try again.</strong>');
    }[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.