0

I have a form where you have to add an image, either by adding a link or uploading an image. When you load the page there is an input text for the url, and there is a button I scripted to change the HTML of the input text to the input file one.

That works well, but the problem comes when submitting the form, the $_FILE array of that dynamic upload input doesn't exist.

This is the javascript code to swap the input:

function SwapImageMode()
{
    if(imageMode == 0)
    {
        imageMode = 1;
        $("#f_imagearea").html("<input type='file' name='addon_imgupld' id='f_upimgimput' name='addon_imgupld' style='margin-bottom:7px;' accept='image/*' onchange='inputFileChange();'/>");
    }
    else if(imageMode == 1)
    {
        imageMode = 0;
        $("#f_imagearea").html("<input type='text' name='addon_imgurl' class='styled_imput f_imgimput' onFocus=imgUrlImputFocus(); onBlur=imgUrlImputBlur(); />");  
    }
}

Php code when the form is submitted:

if(isset($_POST["addon_imgurl"]) && !isset($_POST["addon_imgupld"]))
{
    $formImgMethod = 1; // link
}
else if(isset($_POST["addon_imgupld"]) && !isset($_POST["addon_imgurl"]))
{
    $formImgMethod = 2; // upload
}

if($formImgMethod == 2)
{
    echo($_FILES["addon_imgupld"]["name"]);
}

And this is the error of php:

Notice: Undefined index: addon_imgupld in
C:\xampp\htdocs\addexp\agregar\index.php on line 49
3
  • It would help if you gave us the PHP code that is referred to in the error. Also, the code in inputFileChange() might have something to do with it, as well. Commented Oct 22, 2012 at 23:31
  • The php when the form is submited: echo($_FILES["addon_imgupld"]["name"]); Commented Oct 22, 2012 at 23:43
  • @Agusfn Let me know if my answer makes sense and helps. Commented Oct 22, 2012 at 23:57

1 Answer 1

1

The error is pretty straight-forward. You are trying to access an array key that doesn't exist, so you'll need to check for different types of form submissions.

Just write a simple conditional in your PHP form handler:

if( !empty($_FILES["addon_imgupld"]) ){
  //file input
} elseif( !empty($_REQUEST["addon_imgurl"]) ) {
  //text input
} else {
  //error?
}

You'll want to swap $_REQUEST with whatever type of request was made ($_POST, I assume...).

UPDATE::

This conditional is wrong:

else if(isset($_POST["addon_imgupld"]) && !isset($_POST["addon_imgurl"]))

Should be:

else if(isset($_FILES["addon_imgupld"]) && !isset($_POST["addon_imgurl"]))

UPDATE 2::

Did you set your form encoding type to multipart? If not, add this attribute to your form tag:

enctype="multipart/form-data"
Sign up to request clarification or add additional context in comments.

9 Comments

I have already made the conditional wether the method of adding the image was uploading or by url. In my code, if the method selected was uploading, php should show the name of the image that was properly selected from my pc in the form. I updated the code in this thread..
Looks like your conditional is looking for $_POST, when it should be looking for $_FILES.
Sorry for the late response, okay, i changed the conditional, but the problem is that when i submit the form >>with a valid file selected<< in the input file mode, it doesn't detect any file, it doesn't pass any of the two conditionals i stated before (not uploaded, not url). I will now verify if this has to do with the dinamic method of changing inputs with a static form in another page...
I would do var_dump($_POST, $_FILES) while you're debugging to ensure you don't miss something.
Okay i have checked and something here is wrong, definetly it is the fact that the text input can be changed itself for the file input one without refreshing the page, but i don't know why and how to resolve it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.