1

I'm having issues (and concerns, will explain near the end) when trying to send form data to a database table. On submit, it doesn't do anything, no new rows are being generated in my table.

I have the following folder structure:

template-parts
   form
      form.php
      send-to-database.php
snippets
    form-step-1.php
    form-step-2.php
    form-step-3.php

In my database, I've created a table called attendants. Any form submit data will be sent here.

form.php

<div class="form" id="rsvp-form">
    <div class="container">
        <div class="row">
            <div class="col-12">
                <form id="rsvpForm" action="send-to-database.php" method="post">
                    <?php get_template_part('template-parts/snippets/form-step-1'); ?>
                    <?php get_template_part('template-parts/snippets/form-step-2'); ?>
                    <?php get_template_part('template-parts/snippets/form-step-3'); ?>
                </form>
            </div>
        </div>
    </div>
</div>

form-step-1.php

<fieldset>
    <input type="text" name="fname" placeholder="First name*" required />
    <input type="text" name="lname" placeholder="Surname*" required />
    <input type="text" name="email" placeholder="Email address*" required />
    <textarea name="address" placeholder="Address*" required></textarea>
    <input type="button" name="next" class="next action-button" value="Next" />
</fieldset>

form-step-2.php

<fieldset>
    <div class="attending">
        <p>Which events will you be attending?</p>
        <div class="checkbox">
            <input type="checkbox" id="event1" name="jago" value="Jago">
            <label for="event1">Jago</label>
        </div>
        <div class="checkbox">
            <input type="checkbox" id="event2" name="wedding" value="Wedding">
            <label for="event2">Wedding</label>
        </div>
        <input type="text" name="attendee" placeholder="Attendee name">
    </div>
    <div class="coaches">
        <p>Will you be travelling by the coaches provided?</p>
        <div class="radio">
            <label>
                <input type="radio" id="travel_yes" name="travel" value="Yes">
                <span>Yes</span>
            </label>
        </div>
        <div class="radio">
            <label>
                <input type="radio" id="travel_no" name="travel" value="No">
                <span>No</span>
            </label>
        </div>
    </div>
    <input type="button" name="previous" class="previous action-button" value="Previous" />
    <input type="button" name="next" class="next action-button" value="Next" />
</fieldset>

form-step-3.php

send-to-database.php

<?php

// fieldset 1
$first_name     = $_POST['fname']; // varchar
$last_name      = $_POST['lname']; // varchar
$email          = $_POST['email']; // varchar
$address        = $_POST['address']; // varchar

// fieldset 2
$jago           = $_POST['jago']; // boolean
$wedding        = $_POST['wedding']; // boolean
$attendee_name  = $_POST['attendee']; // varchar
$travel         = $_POST['travel']; // boolean

// fieldset 3
$note           = $_POST['other']; // varchar

// connect to database
$host = "localhost"; // Host name 
$username = "****"; // Mysql username 
$password = "****"; // Mysql password 
$db_name = "rsvp"; // Database name 

// Connect to server and select databse. 
$dbconn = mysqli_connect($host, $username, $password)or die("cannot connect"); 
mysqli_select_db($dbconn, $db_name)or die("cannot select DB"); 


$query = "INSERT INTO `attendants` (`first_name`, `last_name`, `email`, `address`, 'attending_jago', 'attending_wedding', 'attendee_name', 'additional_note') VALUES ('$first_name', '$last_name','$email', '$address', '$jago', '$wedding', '$attendee_name', '$travel', '$additional_note' )";  

var_dump($query);

mysqli_query($dbconn, $query);

?>

I'm also concerned that the connection details to the database are present in the theme itself, something I'd like to avoid, but I haven't seen any other way to do this?

Freddy
  • 683
  • 4
  • 35
  • 114
  • 1
    You have no error checking. You should be catching errors for the `mysqli` functions. Start there. Read the [manual](https://www.php.net/manual/en/class.mysqli.php). – JBES May 19 '20 at 20:14
  • Ordinary button types has no default values (as submit has). Maybe this helps? https://stackoverflow.com/questions/37736056/button-type-button-vs-submit – bestprogrammerintheworld May 19 '20 at 20:21

0 Answers0