1
votes

I'm really battling with this and I've tried every youtube / google page I could find about this, nothing seems to fix the problem.

When clicking submit the database collects only 'idContactForm' but not the rest (name/email/subject/message)

I've tried everything I could find using the if(isset)$_POST line, but I always seem to get the "Undefined index: " errors on ($name / $email / $subject / $message)

What am I doing wrong?

 <?php

    include 'config.php';

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

        $name = trim(mysqli_escape_string($conn,$_POST['Name']));
        $email = trim(mysqli_escape_string($conn,$_POST['Email']));
        $subject = trim(mysqli_escape_string($conn,$_POST['Subject']));
        $message = trim(mysqli_escape_string($conn,$_POST['Message']));

    } else {
    ## connect mysql server
        $mysqli = new mysqli('localhost', 'root', '******', 'tomepublications');
        # check connection
        if ($mysqli->connect_errno) {
            echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
            exit();
        }


            # insert data into mysql database
            $sql = "INSERT  INTO `contactform` (`idContactForm`, `Name`, `Email`, `Subject`, `Message`) 
                    VALUES (NULL, '{$name}', '{$email}', '{$subject}', '{$message}')";

            if ($mysqli->query($sql)) {
                //echo "New Record has id ".$mysqli->insert_id;
                echo "<p>Registred successfully!</p>";
            } else {
                echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>";
                exit();
            }
        }

    ?>  

HTML

 <form method="post" action="contactf.php">
                                    <div class="form-group">
                                        <input type="text" name="name" id="Name" class="form-control" placeholder="Name" required>
                                    </div>
                                    <div class="form-group">
                                        <input type="email" name="email" id="Email"class="form-control" placeholder="Email" required>
                                    </div>
                                    <div class="form-group">
                                        <input type="text" name="subject" id="Subject"class="form-control" placeholder="Subject" required>
                                    </div>
                                    <div class="form-group">
                                        <textarea name="message" class="form-control" id="Message" rows="15" placeholder="Message" required></textarea>
                                    </div>
                                    <input type="hidden" name="submitted" value="true">
                                    <input type="submit" value="submit" class="btn btn-primary">Send Message</>
                                </form>
3

3 Answers

3
votes

The variables are case sensitive. Your form uses name="name", all lower case, but your looking for, $_POST['Name'] upper case N

2
votes

Your if is wrong, $name, $email, $subject and $message are not in the scope of the else.

<?php

include 'config.php';

if (isset($_POST['submitted'])) {

    ## connect mysql server
    $mysqli = new mysqli('localhost', 'root', '******', 'tomepublications');
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }

    $name = trim(mysqli_escape_string($conn,$_POST['name']));
    $email = trim(mysqli_escape_string($conn,$_POST['email']));
    $subject = trim(mysqli_escape_string($conn,$_POST['subject']));
    $message = trim(mysqli_escape_string($conn,$_POST['message']));


    # insert data into mysql database
    $sql = "INSERT  INTO `contactform` (`idContactForm`, `Name`, `Email`, `Subject`, `Message`) 
                VALUES (NULL, '{$name}', '{$email}', '{$subject}', '{$message}')";

        if ($mysqli->query($sql)) {
            //echo "New Record has id ".$mysqli->insert_id;
            echo "<p>Registred successfully!</p>";
        } else {
            echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>";
            exit();
        }
    }

?>  
1
votes

Those variables($name, $email, $subject, $message) are all still undefined when the SQL query is built in the else block. The variables are only defined in the if block.