Skip to main content
Participant
September 21, 2021
Question

Getting Contact form to work with SQL database

  • September 21, 2021
  • 2 replies
  • 167 views

When I run my contact form, it redirects to the form-handler.php, but gives me a blank page and nothiing has been inserted to the database.  

Contact.html:

 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Contact UMMA</title>
<link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body><!--
Replace form-handler.php with URL where to send the form-data when the form is submitted
Goto http://w3c.github.io/html-reference/input.text.html#input.text for more information on all attributes that can be used with text input field
-->




<center><img src="UMMA Logo 2.png" width="200" height="186" alt=""/></center>
<div class="box">
<h1><center>Contact UMMA!</center>
</h1>
<center>
<form method="post" action="form-handler.php" name="ContactUs" id="ContactUs">
<div>
<label for="Country" ><strong>Country:</strong> </label><span class="required">*</span>
<label for="Country">United States </label>
<input type="radio" name="Country" id="US" tabindex="1" value="1" checked>
<label for="NotUS">Other</label>
<input type="radio" name="Country" id="NotUS" tabindex="2" value="NULL">


</div>
<div>
<label for="Last"><strong>Last Name:</strong> <span class="required">*</span> </label>
<input type="text" id="LName" name="LName" value="" placeholder="Your last name" required="required" autofocus />
</div>

<div>

<label for="First"><strong>First Name: </strong><span class="required">*</span> </label>
<input type="text" id="FName" name="FName" value="" placeholder="Your first name" tabindex="3" required="required" autofocus />
</div>
<div>
<label for="Orgn"><strong>Organization:</strong> <span class="required">*</span> </label>
<input type="text" id="Org" name="Org" value="" placeholder="Your School or Organization" tabindex="4" required="required" autofocus />
</div>
<div>
<label for="email"><strong>Email Address:</strong> <span class="required">*</span> </label>
<input type="email" id="email" name="email" value="" placeholder="youraddress@email.com" tabindex="5" required="required" />
</div>
<div>
<label for="Phone"><strong>Preferred Phone No. </strong><span class="required">* </span> </label>
<input type="Phone" id="Phone" name="Phone" value="" placeholder="Phone #" required="required" /><br>
&#40;Numbers only, Country code if outside USA&#41;


</div>
<div>

<label for="subject"><strong>Subject:</strong> </label>
<select id="subject" name="subject" tabindex="4">
<option value="General ">General Inquiry</option>
<option value="Join">Joining UMMA</option>
<option value="Compliment">A compliment for UMMA</option>
<option value="Concern">I have a concern</option>
</select>
</div>

<div>
<label for="message"><strong>Message:</strong> <span class="required">*</span> </label>
<textarea id="message" name="message" rows="7" cols="50" placeholder="Write your message here." required tabindex="5" maxlength="255"></textarea>
</div>

<div>
<h4>Would you like us to respond?</h4>
<label for="radio-choice-1">Yes</label>
<input type="radio" name="radio-choice" id="radio-choice-1" tabindex="6" value="1" checked>
<label for="radio-choice-2">No</label>
<input type="radio" name="radio-choice" id="radio-choice-2" tabindex="7" value="NULL">
</div>
<div>
<input type="submit" value="Submit" id="submit" />
<br><br>
</div>
</form>
</center>
</div>

</body>
</html>

 

 

form-handler.php:

 

<?php
//creating connection to database
$con=mysqli_connect("[removed by moderator]","[removed by moderator]","","[removed by moderator]") or die(mysqli_error());

//check whether submit button is pressed or not
if((isset($_POST['submit'])))
{
//fetching and storing the form data in variables
$Country = $con ->real_escape_string($_POST['country']);
$UFName = $con ->real_escape_string($_POST['FName']);
$ULName = $con->real_escape_string($_POST['LName']);
$UGuestID = $con->real_escape_string($_POST['Email']);
$Phone = $con->real_escape_string($_POST['Phone']);
$Org = $con->real_escape_string($_POST['Orgn']);
$USubject = $con->real_escape_string($_POST['subject']);
$UMessage = $con->real_escape_string($_POST['message']);
$UReply = $con->real_escape_string($_POST['radio-choice']);

//query to insert the variable data into the database
$sql="INSERT INTO 'guests' ('id','UGuestid','Country','Phone', 'ULName', 'UFName', 'Org', 'USubject', 'UMessage', 'UReplyReq') VALUES (NULL,'.$UGuestID','.$Country','.$Phone','.$ULName','.$UFName','.$Org','.$USubject','.$UMessage','UReply')";

//Execute the query and returning a message
if(!$result = $con->query($sql)){
die('Error occured [' . $conn->error . ']');
}
else
echo "Thank you! We will get in touch with you soon";
}

?>

 

How do I get the form handler to perform the sql query?

 

[Sensitive log-in data removed by moderator for your protection.]

This topic has been closed for replies.

2 replies

Nancy OShea
Community Expert
Community Expert
September 21, 2021

Most hosting providers recommend using PHPMailer:

https://github.com/PHPMailer/PHPMailer

 

Example for use with MySQL database:

https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.php

 

Nancy O'Shea— Product User & Community Expert
Legend
September 21, 2021

Try giving your input submit a 'name' attribute:

 

<input type="submit" name="submit" value="Submit" id="submit" />

 

The php script won't run as it's looking to see if the 'submit' button has been clicked - php does that by looking for  the 'name' attribute.

 

if((isset($_POST['submit'])))

EDITED: Actually you dont require the -  if(isset($_POST['submit']) {    } which wraps around your php form processing script as the php script is in a stand-alone page. You only require it IF the script is located in the same page as the form to stop it running onpage load, so you can remove it, otherwise the php script will not run once the submit button (currently WITHOUT a name attribute in your posted code) re-directs to the form processing script page. The script NEEDS to run onpage load, so no real need to give the submit input a 'name' attribute.

 

Also I just happen to notice that the name of one of your form fields is 'email' (lower case e) yet in the script you have it as 'Email' (Uppercase E) that won't work, both must be the same.

 

$UGuestID = $con->real_escape_string($_POST['Email']);

 

If the suggestions do not solve your problems I would advise that you start with a simple form, maybe a 'name' and  'email' and get that working then gradually add the other required form input fields, testing as you go.