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).
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:
Share Improve this question asked Jul 20, 2016 at 19:10 vcamargovcamargo 1311 gold badge1 silver badge2 bronze badges 3 |This function returns false if the row could not be inserted. Otherwise, it returns the number of affected rows (which will always be 1).
3 Answers
Reset to default 5It 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/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
}
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.
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
echo $message
before the$message
is created. – czerspalace Commented Jul 20, 2016 at 19:22!== FALSE
and try – user93819 Commented Jul 21, 2016 at 2:24