Connect form to database

16 replies · opened Aug 27, 2019

CcoconghaileAug 27, 2019

Do you have code to connect the form to a database?

CcoconghaileAug 27, 2019

This is the code I'm using. To start off with I'm setting a very simple form and will add to it once I get it working.


           
          
          
            
              
              
            
            
              
              
                        

            
            
						
							
						
            
 
						
							Submit
						            
          
         

And this is the PHP file:

connect_error);
  
   return $conn;
  }  

  $conn = Connect();
  $template_contactform_firstname = $conn->real_escape_string($_POST['template_contactform_firstname']);
  $template_contactform_surname = $conn->real_escape_string($_POST['template_contactform_surname']);

  
  $query = "INSERT into clarlann (template_contactform_firstname,template_contactform_surname) 
  VALUES('" . $template_contactform_firstname . "','" . $template_contactform_surname . "')";
  $success = $conn->query($query);
  
  if (!$success) {
      die("Couldn't enter data: ".$conn->error);  
  }
  
  $conn->close();

?>
CcoconghaileAug 27, 2019

Sorry...here's an update on the code. What do I need to change? The datbase is working but the form isn't sending the data to the thankyou.php file.

Form:


           
          
          
            
              
              
            
            
              
              
                        

            
            
						
							
						
            
 
						
							Submit
						            
          
        

thankyou.php file:

connect_error);
  
   return $conn;
  }  

  $conn = Connect();
  $firstname = $conn->real_escape_string($_POST['template-contactform-firstname']);
  $surname = $conn->real_escape_string($_POST['template-contactform-surname']);

  
  $query = "INSERT into clarlann (template-contactform-firstname,template-contactform-surname) 
  VALUES('" . $firstname . "','" . $surname . "')";
  $message_success = $conn->query($query);
  
  if (!$message_success) {
      die("Couldn't enter data: ".$conn->error);  
  }
  
  $conn->close();

?>
CcoconghaileAug 27, 2019

I finally got the form to a database working but I'm having a couple of issues. I had to do a few changes i.e. the name and ID tags I changed from;

``

to ``

as MySQL PHP couldn't pick up on the - , so I needed to change them to _.

When I submit the form, the form doesn't clear. The data stays in the input boxes as if nothing was sent. Do I need to do some changes in the JS due to the changes?

SSemicolon WebSTAFFAug 28, 2019

Hello,

You are using a completely Custom Functionality on the Form which is not included with Canvas. Any further customization or feature/functionality enhancements is your responsibility as we currently do not provide Customization related Support according to the Item Support Policy: https://themeforest.net/page/item_support_policy .

Additionally, you have not followed the Message Format available in the include/form.php File. Make sure your Success and Error Messages are in the following format:

echo '{ "alert": "success", "message": "Success Message!" }';

Also, make sure that every message (error or success) you return from the PHP File, is in the above format.

Let us know if we can help you with anything else or if you find any further issues.

CcoconghaileAug 28, 2019

Thank you, I appreciate the response. I understand that I have changed the format of the form. Can I just ask, what files and folders are needed for the original form to work and I will change accordingly.

These are the folders in include:
ajax
campaign-monitor
getresponse
phpmailer
rs-plugin
twitter

And here are the PHP files in the folder include:
form.php
getresponse.php
subscribe.php
subscribe-cm.php
subscribe-email.php

SSemicolon WebSTAFFAug 28, 2019

Hello,

Thanks for Understanding! :)

You will only need the include/phpmailer and the include/form.php File for the Forms to work.

Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

CcoconghaileAug 29, 2019

Is the include/phpmailer only needed if you're using the form to send to an email. As this is going to a database....would I need it?

Also, can I ask what the form.php file does?

Could you also tell me where the code is to clear the form on completion?

Last question....I promise: What does teh botcheck code do?

SSemicolon WebSTAFFAug 29, 2019

Hello,

  1. If you are not using the Email Functionality for the Form, the include/phpmailer Folder and the include/form.php File is not required.

  2. The include/form.php File processes the Inputs submitted through the Form and sends the properly formatted content to the Email.

  3. You can find the code in the js/functions.js File inside the ajaxForm: function() Function Block:
    <pre class="lang:default decode:true " >$(form).resetForm();

  4. The Botcheck Code is implemented to prevent SPAM.

Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

CcoconghaileSep 1, 2019

Do you think you'll ever create code to match the code you've done were the form connects to a database?

SSemicolon WebSTAFFSep 2, 2019

Hello,

Unfortunately, no. Since Canvas is a Frontend Template, the Backend Integrations is the responsibility of the User. We assure you that honestly this is not very difficult to integrate as you can change the default codes to match your preference and make it work with the Databases (we have done this before easily). We strongly suggest you to hire a Freelancer or consider searching for codes on StackOverflow.

Let us know if we can help you with anything else or if you find any further issues.

CcoconghaileSep 2, 2019

I'm able to code but it's limited. The form is working and sending my input details to the database. I had to change the structure slightly from template-contactform- to template_contactform_as my database couldn't read the - value. I'm just struggling to clear the form and to get the success message afterwards. Could it be because the structure is slightly changed? Would I have to change the to I'll keep at it. I appreciate all your help to date.

I'm just looking at the new code for sending an email - form.php. There has been a very big change to this from the last code.

SSemicolon WebSTAFFSep 3, 2019

Hello,

We understand. However, as we mentioned before, if you would like to use the exiting AJAX Form, you will need to output the following message to clear the Form:

echo '{ "alert": "success", "message": "Success Message!" }';

or this error message:

echo '{ "alert": "error", "message": "Error Message!" }';

as these can be easily integrated based on the results of the Database Integrations. If the User submission is successful, show the success message and if the user submission has an error or there is an issue from the Database, show the error message in the above format. This would make the default Form functionality included with Canvas work as expected.

If you are still having difficulties integrating the above codes, simply send us the File you are using for Database Connection through semicolonweb@gmail.com and we will try to integrate the messages for you.

Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

CcoconghaileSep 4, 2019

I tried and tried but no joy. I've sent that on. Thank you again for having a look. I'm happy with HTML, CSS and some JavaScript but i struggle with placement of PHP.

CcoconghaileSep 5, 2019

Did you happen to have a look at the PHP code and where I need to put that correct success and error code?

connect_error);
     
      // Check connection
      if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
      }
        echo "Connected successfully to the database";
    
     return $conn;
  }  

  $conn = Connect();
  
  $firstname = $conn->real_escape_string($_POST['template-contactform-firstname']);
  $surname = $conn->real_escape_string($_POST['template-contactform-surname']);
  $email = $conn->real_escape_string($_POST['template-contactform-email']);  
  $phone = $conn->real_escape_string($_POST['template-contactform-phone']);
  
  $address = $conn->real_escape_string($_POST['template-contactform-address']);
  
  $county = $conn->real_escape_string($_POST['template-contactform-county']);
  $code = $conn->real_escape_string($_POST['template-contactform-code']);
  $country = $conn->real_escape_string($_POST['template-contactform-country']);
  
  $interest1 = $conn->real_escape_string($_POST['template-contactform-interest-1']);
  $interest2 = $conn->real_escape_string($_POST['template-contactform-interest-2']);
  $interest3 = $conn->real_escape_string($_POST['template-contactform-interest-3']);
  $interest4 = $conn->real_escape_string($_POST['template-contactform-interest-4']);
  $interest5 = $conn->real_escape_string($_POST['template-contactform-interest-5']);
  
  $qualification = $conn->real_escape_string($_POST['template-contactform-qualification']);
  $experience = $conn->real_escape_string($_POST['template-contactform-experience']);
  
  $vimeo = $conn->real_escape_string($_POST['template-contactform-vimeo']);
  $youtube = $conn->real_escape_string($_POST['template-contactform-youtube']);  
  
  $nameOne = $conn->real_escape_string($_POST['template-contactform-name-test-one']);
  $emailOne = $conn->real_escape_string($_POST['template-contactform-email-test-one']);
  $phoneOne = $conn->real_escape_string($_POST['template-contactform-phone-test-one']);
  $nameTwo = $conn->real_escape_string($_POST['template-contactform-name-test-two']);
  $emailTwo = $conn->real_escape_string($_POST['template-contactform-email-test-two']);
  $phoneTwo = $conn->real_escape_string($_POST['template-contactform-phone-test-two']);
  
  $gdpr = $conn->real_escape_string($_POST['template-contactform-gdpr']);
  
  $gaeilge = $conn->real_escape_string($_POST['template-contactform-gaeilge']);
  $approveOne = $conn->real_escape_string($_POST['template-contactform-approveOne']);
  $approveTwo = $conn->real_escape_string($_POST['template-contactform-approveTwo']);
  $approved = $conn->real_escape_string($_POST['template-contactform-approved']);
  
  $query = "INSERT into table_name (firstname,surname,email,phone,address,county,code,country,interest_1,interest_2,interest_3,interest_4,interest_5,qualification,experience,vimeo,youtube,name_test_one,email_test_one,phone_test_one,name_test_two,email_test_two,phone_test_two,gdpr,gaeilge,approveOne,approveTwo,approved) 
  VALUES('" . $firstname . "','" . $surname . "','" . $email . "','" . $phone . "','" . $address . "','" . $county . "','" . $code . "','" . $country . "','" . $interest1 . "','" . $interest2 . "','" . $interest3 . "','" . $interest4 . "','" . $interest5 . "','" . $qualification . "','" . $experience . "','" . $vimeo . "','" . $youtube . "','" . $nameOne . "','" . $emailOne . "','" . $phoneOne . "','" . $nameTwo . "','" . $emailTwo . "','" . $phoneTwo . "','" . $gdpr . "','" . $gaeilge . "','" . $approveOne . "','" . $approveTwo . "','" . $approved . "')";
  $message_success = $conn->query($query);
  
  if (!$message_success) {
    echo '{ "alert": "error", "message": "Error Message!" }';  
  }
  
  else {
    echo '{ "alert": "success", "message": "Success Message!" }';
  }
  
  $conn->close();

?>
CcoconghaileSep 5, 2019

I tried it also with the old php file you had for form.php. It fills the database but the message don't appear and the form does reset either. Am I missing some files? I doubt it because it works when I send it to an email.

connect_error);
     
      // Check connection
      if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
      }
        echo "Connected successfully to the database";
    
     return $conn;
  }  

  $conn = Connect();
    
  // Form Processing Messages
  $message_success = 'Go raibh maith agat as diarratas ar chl&aacute;r&uacute;! Beidh an Cl&aacute;raitheoir i dteagmhail leat go luath ina l&eacute;ith.';
  
  // Add this only if you use reCaptcha with your Contact Forms
  $recaptcha_secret = 'reCaptcha Secret'; // Your reCaptcha Secret
  
  if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  	if( $_POST['template-contactform-email'] != '' ) {
  
      $firstname = $conn->real_escape_string($_POST['template-contactform-firstname']);
      $surname = $conn->real_escape_string($_POST['template-contactform-surname']);
      $email = $conn->real_escape_string($_POST['template-contactform-email']);  
      $phone = $conn->real_escape_string($_POST['template-contactform-phone']);
      
      $address = $conn->real_escape_string($_POST['template-contactform-address']);
      
      $county = $conn->real_escape_string($_POST['template-contactform-county']);
      $code = $conn->real_escape_string($_POST['template-contactform-code']);
      $country = $conn->real_escape_string($_POST['template-contactform-country']);
      
      $interest1 = $conn->real_escape_string($_POST['template-contactform-interest-1']);
      $interest2 = $conn->real_escape_string($_POST['template-contactform-interest-2']);
      $interest3 = $conn->real_escape_string($_POST['template-contactform-interest-3']);
      $interest4 = $conn->real_escape_string($_POST['template-contactform-interest-4']);
      $interest5 = $conn->real_escape_string($_POST['template-contactform-interest-5']);
      
      $qualification = $conn->real_escape_string($_POST['template-contactform-qualification']);
      $experience = $conn->real_escape_string($_POST['template-contactform-experience']);
      
      $vimeo = $conn->real_escape_string($_POST['template-contactform-vimeo']);
      $youtube = $conn->real_escape_string($_POST['template-contactform-youtube']);  
      
      $nameOne = $conn->real_escape_string($_POST['template-contactform-name-test-one']);
      $emailOne = $conn->real_escape_string($_POST['template-contactform-email-test-one']);
      $phoneOne = $conn->real_escape_string($_POST['template-contactform-phone-test-one']);
      $nameTwo = $conn->real_escape_string($_POST['template-contactform-name-test-two']);
      $emailTwo = $conn->real_escape_string($_POST['template-contactform-email-test-two']);
      $phoneTwo = $conn->real_escape_string($_POST['template-contactform-phone-test-two']);
      
      $gdpr = $conn->real_escape_string($_POST['template-contactform-gdpr']);
      
      $gaeilge = $conn->real_escape_string($_POST['template-contactform-gaeilge']);
      $approveOne = $conn->real_escape_string($_POST['template-contactform-approveOne']);
      $approveTwo = $conn->real_escape_string($_POST['template-contactform-approveTwo']);
      $approved = $conn->real_escape_string($_POST['template-contactform-approved']); 
      
      $regDate = date('Y/m/d H:i:s');
      $updateDate = date('Y/m/d H:i:s');
  
  		if( $botcheck == '' ) {
  
        $query = "INSERT into table_name (firstname,surname,email,phone,address,county,code,country,interest_1,interest_2,interest_3,interest_4,interest_5,qualification,experience,vimeo,youtube,name_test_one,email_test_one,phone_test_one,name_test_two,email_test_two,phone_test_two,gdpr,gaeilge,approveOne,approveTwo,approved,regDate,updateDate) 
        VALUES('" . $firstname . "','" . $surname . "','" . $email . "','" . $phone . "','" . $address . "','" . $county . "','" . $code . "','" . $country . "','" . $interest1 . "','" . $interest2 . "','" . $interest3 . "','" . $interest4 . "','" . $interest5 . "','" . $qualification . "','" . $experience . "','" . $vimeo . "','" . $youtube . "','" . $nameOne . "','" . $emailOne . "','" . $phoneOne . "','" . $nameTwo . "','" . $emailTwo . "','" . $phoneTwo . "','" . $gdpr . "','" . $gaeilge . "','" . $approveOne . "','" . $approveTwo . "','" . $approved . "','" . $regDate . "','" . $updateDate . "')";
  
  			// Runs only when reCaptcha is present in the Contact Form
        if( isset( $_POST['g-recaptcha-response'] ) ) {
         
            $recaptcha_data = array(
                'secret' => $recaptcha_secret,
                'response' => $_POST['g-recaptcha-response']
            );
         
            $recap_verify = curl_init();
            curl_setopt($recap_verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
            curl_setopt($recap_verify, CURLOPT_POST, true);
            curl_setopt($recap_verify, CURLOPT_POSTFIELDS, http_build_query( $recaptcha_data ));
            curl_setopt($recap_verify, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($recap_verify, CURLOPT_RETURNTRANSFER, true);
            $recap_response = curl_exec($recap_verify);
         
            $g_response = json_decode( $recap_response );
         
            if ( $g_response->success !== true ) {
                echo '{ "alert": "error", "message": "N&iacute;or deimhn&iacute;odh an Captcha! Triail ar&iacute;s ar ball &eacute;, le do thoil." }';
                die;
            }
        }
  
  			// Uncomment the following Lines of Code if you want to Force reCaptcha Validation
  
  			  if( !isset( $_POST['g-recaptcha-response'] ) ) {
  			 	 echo '{ "alert": "error", "message": "Captcha not Submitted! Please Try Again." }';
  			 	 die;
  			  }
  
  			if ($conn->query($query) === TRUE):
  				echo '{ "alert": "success", "message": "' . $message_success . '" }';
  			else:
  				echo '{ "alert": "error", "message": "N&iacute;orbh f&eacute;idir r&iacute;omhphost a sheoladh ar ch&uacute;is &eacute;igin. Triail ar&iacute;s ar ball &eacute;, le do thoil.

**F&aacute;th:**
' . $mail->ErrorInfo . '" }';
  			endif;
  		} else {
  			echo '{ "alert": "error", "message": "Bot **Detected**.! Glan do Botster.!" }';
  		}
  	} else {
  		echo '{ "alert": "error", "message": "L&iacute;on isteach an fhoirm ar fad, le do thoil, agus triail ar&iacute;s &eacute;." }';
  	}
  } else {
  	echo '{ "alert": "error", "message": "Tharla fadhb gan choinne le d’ fhoirm. Triail ar&iacute;s ar ball &eacute;, le do thoil." }';
  } 

?>
SSemicolon WebSTAFFSep 5, 2019

Replied through Emails.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard