0

I was trying to send data from my Form to My data base, but i keep on having the same error at the Textarea part.

I am not sure whether i has to do with data type in MYSQL, but i tried that also by setting it to text, medium, text, long text and etc.

The connection is successful. All the other information are input successful as well except of The textarea thingy.

Using XAMPP.

The reason why i put form action as a link is because i want it to find my file.

Below are my codes

Code for HTML;

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="utf-8"/>
        <title> Competition Organizer </title>
        <link rel="stylesheet" href="main.css">
    </head>

    <body>
        <ul> 
            <li><a href="">Log Out</a></li>
            <li><a>Notification</a></li>
            <li><a href="Report.html">Generate Report</a></li>
            <li><a href="Admin.html">Home</a></li>
            <li style="float:left;color:white;">Competition Management</li>
        </ul>
        <form action="http://localhost/GCSweb/add.php" method="POST">
            <div>
                <br>
                <br>
                <br>

                <label><b>Competition Name</b></label>
                <input type="text" placeholder="Enter Competiton Name" name="compname" required>

                <label><b>Competition Mentor</b></label>
                <input type="text" placeholder="Enter Competiton Mentor Name" name="compmenname" required>

                <label><b>Competiton Description</b></label>
                <textarea rows="20" cols="50" name="compdescription" form="CompForm" placeholder="Enter Description Here..." required></textarea>

                <br>

                <input type="submit" value="Submit" name="submit">
            </div>
        </form>

    </body>

</html>

Code for PHP;

Connection to SQL;

<?php

$DB_host = "localhost";
$DB_user = "root";
$DB_password = "";
$DB_name = "competition_system";

try{
    $connection=new PDO("mysql:host=$DB_host;dbname=$DB_name",$DB_user.$DB_password);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>

Send data;

<?php

include "connection.php";

$compname = $_POST['compname'];
$compmenname = $_POST['compmenname'];
$compdescription = $_POST['compdescription']; //THis doesnt work


if(isset($_POST["submit"])){

    try{
    $sql = "INSERT INTO competition(Name,Host,Description) VALUES ('".$compname."','".$compmenname."','".$compdescription."')";

    if ($connection->query($sql)){
        echo"<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
    }else{
        echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
    }

    $connection = null;
    }

    catch(PDOException $e){
        echo $sql . "<br>" . $e->getMessage();
    }
}
?>

Thank you

Ivan Sim
  • 305
  • 4
  • 15
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) –  Sep 14 '17 at 03:15

1 Answers1

1

you're defining a non-existent form to include the textarea in - form="CompForm"

remove that and your textarea will be included when you submit that form.

to add, you only put in the form attribute for the textarea if the textarea is located outside the form tags

example:

<form id="my_form">
  <!-- other form elements here -->
</form>

<textarea name="my_txt" form="my_form"></textarea>
roninblade
  • 1,882
  • 15
  • 15