0

It's my first time validating a php form with prepared statements and i'm coming across some problems.

I'm using Form.php for the validation and insertion onto the database which is being called in the index.php form.

The problem is that the errors messages don't show up. I can press the button 'Submit' and the form is processed even without inserting data in the input boxes!

I've been stubborn, trying to do things on my own but since i've been stuck with this form for days and before i throw in the towel i feel i should ask for help at this point.

Here is the form code in Index.php:

<form id="form" name="contactForm" method="post" action="php/Form.php">
            <div>
                <label for="name">Your name</label> 
                <input type="text" id="name" name="name" maxlength="40" placeholder="Write your Name"   >
                <span class="error"><?php echo $nameError;  ?></span>
            </div>
            <div>
                <label for="email">Your email</label>
                <input type="email" id="email" name="user_mail" placeholder="email@example.com">
                <span class="error"><?php echo $emailError;  ?></span> 
            </div>                
            <div>
                <label for="topic">Select Topic</label>
                <select id="topic" name="topic">
                    <option selected disabled hidden value="">Choose a Topic</option>
                    <option value="link">Site Link</option>
                    <option value="copyright">Copyright</option>
                    <option value="errors">Site/Article errors</option>
                    <option value="feedback">Feedback</option>
                    <option value="other">Other</option>
                </select>
                <span class="error"><?php echo $topicError;  ?></span>
            </div>                
            <div>
                <label for="msg">Your message</label>
                <textarea id="msg" name="user_message" placeholder="Write your message"></textarea>
                <span class="error"><?php echo $msgError;  ?></span>
            </div>                
            <div class="button">
                <button type="submit" id="submit" name="submit"  value="true">Submit</button> 
                <span class="success"></span>
            </div>
        </form>

And here is the Form.php

<?php

$servername = "localhost:3306";
$username = "root";
$password = "";
$dbname = "site_comboios";


$nameError = "";
$emailError = "";
$topicError = "";
$msgError = "";

$name = $_POST['name'];
$email = $_POST['user_mail'];
$topic = $_POST['topic'];
$msg = $_POST['user_message'];

if( isset( $_POST['submit'])) {
if(empty( $name) && !isset($name) ) {
    $nameError = "Name is required";
}

if(empty( $email) && !isset($email)) {
    $emailError = "Email is required";
} elseif(filter_var($email,FILTER_VALIDATE_EMAIL)) {
  $emailError = "Please insert a correct email address";  
}  

if(empty( $topic) && !isset($topic) ) {
    $topicError = "Please choose a topic";
} 

if(empty( $msg) && !isset($msg) ) {
    $msgError = "Let us know your opinion";
}
}

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

//check connection
if($mysqli->connect_errno) {
echo 'Error connecting to database';
}

//Prepared Statement
$stmt = $mysqli->prepare("INSERT INTO contacts(Nome, Email, Topico,    Mensagem)  VALUES(?, ?, ?, ?)" );
$stmt->bind_param('ssss', $name, $email, $topic, $msg);
$stmt->execute();
Fab
  • 145
  • 3
  • 20
  • Btw in your if you should use || instead of &&. && means and: so both conditions at the same time. || means or: so one condition or the other – Lelio Faieta Jan 09 '17 at 22:53
  • @LelioFaieta I wanted to check both conditions, in this case, if the variable is not set and is empty. Isn't supposed to check both? I might be wrong of course... – Fab Jan 09 '17 at 23:16
  • if it is not set it is already empty by definition. But if it is empty it may be set (but you should stop also). Your condition match the first case but miss the second (set but empty) – Lelio Faieta Jan 10 '17 at 08:38
  • I've changed my code using your advise but the errors aren't displaying in the index.php page yet. How can i pass the errors messages from form.php to index.php? That is the problem. – Fab Jan 11 '17 at 01:03

0 Answers0