0

I am having issues submitting content from my form to my database. When using isset function, it showing that the variables are not being set.

Here is my PHP code and html code.

PHP

<?PHP
$title = $_POST['title']['name']; 
if(isset($title)) {
    echo 'all is well';
} else {
    echo 'all is not well';
}

?>

HTML

<div class="container">
    <form action="newEmptyPHP.php" method="post" enctype="multipart/form-data">
        <input type="text" name="title" placeholder="title">
        <input type="text" name="artist" placeholder="artist">
        <input type="file" name="cover" placeholder="Upload Picture">
        <input type="file" name="song" placeholder="Upload Song">
    </form> 
</div>

When I refresh the browser I am receiving "all is not well". What am I missing?

4
  • It's $_POST['title']. Also, method = "POST"; Commented Mar 12, 2016 at 0:13
  • Also, you don't need to use isset, just $title is fine. Like this: if ($title) Commented Mar 12, 2016 at 0:14
  • You can cook it down with usage of a ternary operator, which is a one-liner for that, like this: echo (isset($_POST['title']) ? "All is well!" : "All is not well.."); -- your title in the form isn't an array, so you must use $_POST['title'] and nothing more to get that value. Commented Mar 12, 2016 at 0:17
  • The name attribute's value is the $_POST's index. Commented Mar 12, 2016 at 0:20

2 Answers 2

1

In the PHP, $_POST['title'] should be used to get the value of the title input.

Also, you should always check if the variable in $_POST is set before assigning it to a variable.

<?php

if(isset($_POST['title'])) {
    $title = $_POST['title'];
}

if($title) {
    echo 'all is well';
}else{
    echo 'all is not well';
}

?>
Sign up to request clarification or add additional context in comments.

Comments

1

This line caused an error: $title = $_POST['title']['name'];.

$_POST should be followed by the HTML name attribute, in your case, name="title".

Also, you should check if the form is posted before assigning it to a variable.

Thus, it should be:

if(isset($_POST['title'])) {
    $title = $_POST['title'];

Comments

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.