1

I'm trying to write a little script within my footer.php (which I'll later transform into a plugin) that send two form fields to a custom table (wp_newsletter). I'm already sending the form and writing to the table correctly, but I don't know how I can send a success or fail message back to the user. My current code is as follows:

<form method="post">
  <input type="text" name="user_name">Name
  <input type="text" name="user_email">Email
  <input type="submit">
  <?php echo $message;  ?>
</form>

<?php
global $wpdb;
$table   = $wpdb->prefix . "newsletter";
$name    = sanitize_text_field( $_POST["user_name"] );
$email   = sanitize_email( $_POST["user_email"] );
$message = "";


if( isset($_POST["submit"]) ) {
  if ( is_email($email) && isset($name)) {
    if ( $wpdb->insert( $table, array("name"  => $name, "email" => $email)) != false ) {
      $message = "Your subscription was sent.";
    }
  }
  else {
    if ( !is_email($email) ) {
        $message = "Invalid email address.";
    } elseif ( !isset($name) ) {
        $message = "The field name is mandatory.";
    } else {
        $message = "Both name and email fields are mandatory.";
    }
  }
} else {
    $message = "Please, try again later.";
}
?>


<?php wp_footer(); ?>

</body>
</html>

I (think) am testing it right, accordingly to the $wpdb docs which says that:

This function returns false if the row could not be inserted. Otherwise, it returns the number of affected rows (which will always be 1).

  • Where did you want the message to appear? You have your echo $message before the $message is created. – czerspalace Jul 20 '16 at 19:22
  • Hm, I was not sure if I could used it before declaration... But I wanted to show it under the form. – vcamargo Jul 20 '16 at 20:03
  • 1
    use this !== FALSE and try – user93819 Jul 21 '16 at 2:24
1

When I realized that PHP is an acronym for "PHP Hypertext Preprocessor" -- emphasis on "preprocessor" -- I finally understood that I can't mix PHP and HTML and expect any kind of interactivity with the user. When a web page is served, the PHP creates the HTML and then the HTML is displayed by the browser. If the user does something the PHP must respond to, it must trigger a new PHP/HTML page to provide some sort of response. (An alternate would be to use AJAX to send data back and forth without loading a new page. Wordpress works well with AJAX and there are tutorials a google search away.)

For a simple form like yours, I would use javascript for error checking. If the form is not filled in, prevent form submission with JS. If the form is complete, the form's action can be a .php file that does the database insert and displays the success/failure message in HTML.

  • Thanks for the tip, scott. How could I create such .php file that would display the success/failure message to the user? – vcamargo Jul 21 '16 at 0:52
  • Sorry for the late reply. In a web environment, I would have the form's action be a .php file. The data is sent via GET to the target file. The data is then parsed and inserted into the DB. Then the .php file outputs HTML into a new web document that indicates success or failure. Note this does not give the user much opportunity to correct errors to achieve a better result once the new web page is loaded. – scott Jul 25 '16 at 16:38
  • In Wordpress, however, I would consider trying AJAX. There are many good tutorials, and I would look in the Codex too, but here are two that worked for me: smashingmagazine.com/2011/10/how-to-use-ajax-in-wordpress and premium.wpmudev.org/blog/using-ajax-with-wordpress AJAX allows DB access without loading a new web page. So you would have an element that has no HTML content until the AJAX returned with success/failure: document.getElementById("results-div").innerHTML = content.result; – scott Jul 25 '16 at 16:42
  • The logic that handles the form is inside the same file of the form: the footer.php. I'm just trying to make it work this way, then I can separate it better when I'm done with the tests. Despite this, I don't want to send my form's data through a GET. – vcamargo Jul 25 '16 at 20:09
  • My mistake. You can use POST. It sounds like you want to do AJAX inside footer.php so it doesn't trigger a page reload. You will have to use javascript to send/receive the AJAX data, but you can create a new .php file to handle the AJAX call. (I put my .php files inside a plugin so all the dependencies are handled by the WP system.) – scott Jul 25 '16 at 20:13
0

I came here via google and what helped me was using properties of $wpdb object:

$wpdb->last_error 

shows the last error if present

$wpdb->last_query 

shows the last query which gave the above error

0

It returns either the number of rows inserted or false on error.

Maybe you can get id of inserted recode or false if the insert fails:

refer link: https://developer.wordpress.org/reference/classes/wpdb/insert/#return

So you can check like below:

$result_check = $wpdb->insert( $table, array("name"  => $name, "email" => $email));
if($result_check){
   //successfully inserted.
}else{
  //something gone wrong
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.