Good afternoon, citizens. Not a big mind writing to you. This is all very new to me, and I am trying to blind myself like a landing page. And the question arose of sending data from the site form to a wordpress database. First, show the form code

<form method="POST" action="http://www.progressivetrade.org/wp-content/themes/zerif-pro/events/csv.php"> <div class="clear"></div> <input class="inp1" type="text" name="add_phone" placeholder="Ваш телефон"> <div class="clear"></div> <input class="inp1" type="text" name="add_sname" placeholder="Имя и Фамилия"> <div class="clear"></div> <input class="inp1" type="text" name="add_email" placeholder="E-mail"> <div class="clear"></div> <button id="btn_info" class="forex_button"> Я участвую</button> </form> 

and the csv.php file itself

 <?php<br> CONST HOST = 'localhost'; <br> CONST USER = 'u0258277_default';<br> CONST PSWD = 'PyE26in8';<br> CONST DB = 'u0258277_kaloos';<br> $mysqli = new mysqli(HOST, USER, PSWD, DB);<br> $mysqli->query("SET NAMES UTF8"); <br> $datetime = date("Ymd H:i:s");<br> $name = $_POST['add_sname'];<br> $email = $_POST['add_email'];<br> $phone = $_POST['add_phone'];<br> $url = $_SERVER['HTTP_REFERER'];<br> $user_agent = $_SERVER['HTTP_USER_AGENT']; <br> $ip = $_SERVER['REMOTE_ADDR'];<br> $query = "INSERT INTO leads (`fsname`,`email`,`phone`,`datetime`, `user-agent`, `ip`,`url`) VALUES($name,$email, $phone , $datetime, $user_agent, $ip, $url)"; $result = $mysqli->query($query);<br> echo $query;<br> print_r($_POST);<br> ?> 

And a little script in the header:

 <script> var MyAjax = {"ajaxurl":"http:\/\/progressivetrade.org\/wp-admin\/admin-ajax.php"}; jQuery.get(MyAjax.ajaxurl, { name:name, email:email, tel:tel, },function(data){ }); //для неавторизованных пользователей add_action( 'wp_ajax_nopriv_test', 'my_ajax_guest_lure'); //для авторизованных пользователей add_action( 'wp_ajax_test', 'my_ajax_guest_lure'); </script> 

And when entering data, the form is such a thing:

 INSERT INTO leads (`fsname`,`email`,`phone`,`datetime`, `user-agent`, `ip`,`url`) VALUES(Валерка Пупкин,email@email.ru, 0961245657 , 2016-12-02 16:22:56, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36, 176.241.107.18, http://progressivetrade.org/events/)Array ( [add_phone] => 0961245657 [add_sname] => Валерка Пупкин [add_email] => email@email.ru ) 

It kind of collects data, but the database is not updated. Tell me where I strati? Thank you for reading)))

    2 answers 2

    If you use WP there is nothing easier than writing in the database,

     <form method="POST" action=""> <div class="clear"></div> <input class="inp1" type="text" name="add_phone" placeholder="Ваш телефон"> <div class="clear"></div> <input class="inp1" type="text" name="add_sname" placeholder="Имя и Фамилия"> <div class="clear"></div> <input class="inp1" type="text" name="add_email" placeholder="E-mail"> <div class="clear"></div> <button id="btn_info" name="contact-form" class="forex_button"> Я участвую</button> </form> 

    code put in fuinctions.php

     function theme_form_init() { if( ! isset($_POST['contact-form']) ) return false; global $wpdb; $args = array( 'fsname' => isset($_POST['add_sname']) ? esc_attr( trim($_POST['add_sname']) ) : '', 'email' => isset($_POST['add_phone']) ? esc_attr( trim($_POST['add_phone']) ) : '', 'phone' => isset($_POST['add_email']) ? esc_attr( trim($_POST['add_email']) ) : '', 'datetime' => date('Ymd H:i:s'), 'user-agent' => $_SERVER['HTTP_USER_AGENT'], 'ip' => $_SERVER['REMOTE_ADDR'], 'url' => $_SERVER['HTTP_REFERER'], ); $result = $wpdb->insert( $wpdb->prefix . 'leads', $args ); var_dump($result); } add_action('wp', 'theme_form_init'); 
    • Thanks for the advice. Removed the action from the form code, and added the sent code in function.php, when entering data into the form and clicking on the button, an error is not generated, but no entry is made to the database. As it was 0 lines, it remains. What is isset ($ _ POST ['contact-form'])? I do not have an element named contact-form - Andrei
    • This is the name of the button I wrote this, you can put it on. It will signal the hook that the request is being made from the contact form. Everything I wrote works on php. If you need ajax, then there is a little different way to catch - Vitaliy Kukin

    I do not know exactly what the reason is, but maybe in the film version, try instead of the line:

     $query = "INSERT INTO leads (`fsname`,`email`,`phone`,`datetime`, `user-agent`, `ip`,`url`) VALUES($name,$email, $phone , $datetime, $user_agent, $ip, $url)"; 

    Write the following:

     $query = 'INSERT INTO leads (`fsname`,`email`,`phone`,`datetime`, `user-agent`, `ip`,`url`) VALUES("'.$name.'","'.$email.'", "'.$phone.'", "'.$datetime.'", "'.$user_agent.'", "'.$ip.'", "'.$url.'")'; 
    • I tried, and now produces: Parse error: syntax error, unexpected T_VARIABLE in /var/www/u0258277/data/www/progressivetrade.org/wp-content/themes/zerif-pro/events/csv.php on line 17. 17 the string is: $ result = $ mysqli-> query ($ query) ;. And if I understand correctly, do not like the form of the record? - Andrei