Jump to content

Need assistant on if else try catch after form submission


halben

Recommended Posts

I would like someone to please help look over these lines of codes and help me correct what's wrong with it. 

 

The first problem I found out on my registration form is when a user fills out a portion of the form, the WordPress client side and server side validation works as intended but that data gets processed/stored into my external database which it is not suppose to. So what I did to prevent it check if the user pressed the submit button, if it's pressed, check to see if the values aren't empty. If the values are empty, do something and stop the connection but If the values are not empty, run the connection to the external DB using the try/catch statement. 

 

I tested it out again and the validation works as intended, no data was processed but when the user completes the registration form correctly, their info gets stored into the WordPress DB but not my external database. 

 

Can anyone please help me with this issue?

<?php

$firstname = esc_attr($_POST['fname']);
$lastname= esc_attr($_POST['lname']);
$email= esc_attr($_POST['email']);
...

$error = false;
$required = array($firstname, $lastname, $email ...);

if (!isset($_POST['on-reg-submit'])) {
    // Do nothing
} else {
    foreach($required as $val) {
        if (empty($_POST[$val])) {
            $error = true;
        }
    }
    
    if ($error) {
	 // If errors, prevent from submmiting to external DB
    } else {
         // No errors
         try {
             // Here is where I connect to the external DB
             // processing the posts values into it and storing the post values into my external DB

         } catch {
             echo "ERROR!";
         }
    } ....

    


Edited by halben
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.