Jump to content

[SOLVED] How to make PHP determine if POST variable is existant


siwelis

Recommended Posts

if (!$_POST['uploaded'] = NULL){

//If what was posted was NOTHING then

$treC = "0";

} else {

$treC = "2";

}

 

No matter what, it keeps making the variable equal to "0," even when something WAS posted. Does anyone know what I should be replacing "NULL" with? I tried with 0's, ||'s assigning initial values to the posting form such as 0, nothing has worked so far.

Link to comment
Share on other sites

First off you are using the assignment operator a single "=". Second off to check null values, php gives you a nice function www.php.net/is_null. Third you probably want to check if it was set.

 

<?php
if (isset($_POST['uploaded']) && !is_null($_POST['uploaded'])){
//If what was posted was NOTHING then
   $treC = "0";
} elseif (isset($_POST['uploaded']) {
  // it was set and was not null
  $treC = "2";
}else {
 // it wasn't set
 $treC = "0";

}

 

 

Link to comment
Share on other sites

<?php

if (isset($_POST['uploaded']) && !is_null($_POST['uploaded'])){

//If what was posted was NOTHING then

    $treC = "0";

} elseif (isset($_POST['uploaded'])) {

  // it was set and was not null

  $treC = "2";

}else {

  // it wasn't set

  $treC = "0";

 

}

//Thank you for your replies. The above needed an extra ")" - Even when the Form has data entered, it still returns from "else" (verified with echo- almost said TRACE, been taking flash in school). I'm going to try tweaking it some, if you have any more suggestions, please let me know, and when it's solved completely, I'll post that on here.

Link to comment
Share on other sites

You don't need the three cases:

 

<?php
if (!isset($_POST['uploaded']) || is_null($_POST['uploaded'])){
    //If it was not posted or posted value is empty
    $treC = "0";
} else {
   // It was set and was not null
   $treC = "2";
}
?>

Link to comment
Share on other sites

<?php
if (isset($_POST['uploaded'])) {
   if (is_null($_POST['uploaded'])) {
         echo 'Uploaded is null';
   }else {
         echo 'Uploaded is NOT null';
    }
}else {
  echo 'Post has not been set';
}
?>

 

Let's try that approach and insert the $treC where you want them to be.

 

Or you can do what mjdamato did, probably easier with his way. I am just posting this so you can kind of see the logic in a sense.

Link to comment
Share on other sites

I am sensing I am making a n00bie mistake. Since I am getting the

 

"Post has not been set"

 

... It will execute other commands before and after that script using that $_POST['uploaded']

(ie it will upload files to the site)

 

But still get the

 

"Post has not been set"

 

message.

Link to comment
Share on other sites

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target_path)) {

    echo "The file ".  basename( $_FILES['uploaded']['name']).

    " has been uploaded";

} else{

    echo "<b>No image was uploaded.</b>";

}

 

.. It works, I just only want it to work when there is a "uploaded" to upload. I just kind of rigged it up. I really appreciate all of your help!

Link to comment
Share on other sites

$target_path = "/".images."/".$_POST['cat_id']."/";

 

$target_path = $_SERVER['DOCUMENT_ROOT'].$target_path . basename( $_FILES['uploaded']['name']);

 

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target_path)) {

    echo "The file ".  basename( $_FILES['uploaded']['name']).

    " has been uploaded";

$folder = "website.com".$_POST['cat_id']."/".$_FILES['uploaded']['name'];

} else{

//do nothing

    echo "No Image File was uploaded.";

$folder = "0";

}

 

--------------------

SOLVED!

 

It seems two pieces of code may have been conflicting, so I combined the two. Once again I really appreciate your advice! Though this wasn't completed quite as I had planned, maybe it was my subconscious defeating my want for speed which would of created spaghetti code.

 

Thank you!!!!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.