How to run an external script from a WordPress form?

2

I just installed the latest version of WordPress and created a page with a simple form.

<form class="" method="post" action="insert.php">
    <input name="name" placeholder="Name">
    <input name="email" placeholder="E-Mail">
    <input name="company" placeholder="Company">
    <button name="submit" type="submit">OK</button>
</form>

I would then like to insert the data from this form into a database table. I researched and found that a possible code to do this would be:

<?php
    if(isset($_POST['submit']))
    {
        global $wpdb;

        $wpdb->insert
            ('users', // Nome da tabela no banco de dados.
                array
                (
                    'name'=>$_POST['name'],
                    'email'=>$_POST['email'],
                    'company'=>$_POST['company'],
                    'registration'=>now()]
                )
            );

        echo 'Usuário inserido com sucesso!'
    }
?>

The questions would be:

Should I insert this code just below the form code or save everything to an external file?

And if that is the case, where should I save the insert.php file so that it is rotated by clicking the submit button?

    
asked by anonymous 02.11.2015 / 12:05

1 answer

1

You can leave this code in the same file or in a separate file. I would advise to leave in a separate file the logic that is inside if , then you just give include of the file inside your if by checking post . Example:

if(isset($_POST['submit'])){
    include('meuArquivo.php');
}

And within meuArquivo.php

global $wpdb;

$wpdb->insert
  ('users', // Nome da tabela no banco de dados.
    array
    (
      'name'=>$_POST['name'],
      'email'=>$_POST['email'],
      'company'=>$_POST['company'],
      'registration'=>now()]
    )
  );
echo 'Usuário inserido com sucesso!'
    
02.11.2015 / 15:08