Beta Particle Beta Particle - 12 days ago 5
HTML Question

PHP/MySQLi Contact Form Not Working with $_POST

So my form seems to work when i put test values in the php file like so:

$name = 'name';
$email = 'test@gmail.com';
$telephone = '123456789';
$message = 'test message';


However when i replace the test values with the php _POST like so:

$name = $_POST['name']


everything seems to break and im not sure why.

Got a very simple contact-us form setup in a file contact.html:

<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-sm-9 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-8 col-lg-offset-2">
<div class="form-header">
<h8>CONTACT US FORM</h8>
</div>

<form method="post" action="php/contact.php">
<div class="row">
<div class="form-group col-xs-4 col-sm-4 col-md-4 col-lg-4">
<label>Name:</label>
<input type="text" name="name" class="form-control" onchange="capitalise()">
</div>
<div class="form-group col-xs-4 col-sm-4 col-md-4 col-lg-4">
<label>Email Address:</label>
<input type="email" class="form-control" name="email">
</div>
<div class="form-group col-xs-4 col-sm-4 col-md-4 col-lg-4">
<label>Phone Number:</label>
<input type="tel" class="form-control" name="tel">
</div>

<div class="clearfix"></div>

<div class="form-group col-xs-12 col-sm-12 col-md-12 col-lg-12">
<label>Message:</label>
<textarea class="form-control" rows="6"></textarea>
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-12 col-lg-12">
<input type="hidden" name="msg" value="contact">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>


Here is the PHP contact form using prepared statements.

<?php

$servername = "*******";
$username = "********";
$password = "********";
$dbname = "*********";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}

$stmt = $conn -> prepare("INSERT INTO contact (name, email, telephone, message) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $name, $email, $telephone, $message);

$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['tel'];
$message = $_POST['msg'];
$stmt->execute();

echo "New records created successfully";

$stmt->close();
$conn->close();

?>

Answer

You'll want to declare your variables before inserting them into your bind:

<?php
    $servername = "*******";
    $username = "********";
    $password = "********";
    $dbname = "*********";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error){
        die("Connection failed: " . $conn->connect_error);
    }

    $stmt = $conn -> prepare("INSERT INTO contact (name, email, telephone, message) VALUES (?, ?, ?, ?)");

    $name = $_POST['name'];
    $email = $_POST['email'];
    $telephone = $_POST['tel'];
    $message = $_POST['msg'];

    $stmt->bind_param("ssss", $name, $email, $telephone, $message);

    $stmt->execute();

    echo "New records created successfully";

    $stmt->close();
    $conn->close();
?>
Comments