I implemented a simple class like:
class my_form {
function __construct()
{
add_shortcode('my_form_custom', array($this, 'show_custom_form'));
}
function registration_template_form() {
?>
<div class="popup_canvas_container">
<form id="my_form" class="my_form" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>">
<p><label for="first_field">Field1:</label> <input type="text" name="first_field" id="first_field" value="<?php echo(isset($_POST['first_field']) ? $_POST['first_field'] : null); ?>" required /></p>
[...]
<div class="confirmation_box">
<p><button name="post_form" id="post_form" type="submit"><?php _e('Submit', 'my_form_textdomain'); ?></button></p>
</div>
</form>
<div id="messages_box" class="messages_box">
<p><div id="message_label"></div></p>
</div>
</div>
<?
}
function form_validation() {
if ( strlen($this->first_field) < 4 ) {
$error_message = 'Too short!';
return false;
}
}
function save_to_db() {
if ( !$this->form_validation() ) {
?>
<script language="javascript">$("#message_label").text("<?php echo $error_message; ?>"); $('#messages_box').dialog();</script>
<?php
} else {
}
}
function show_custom_form() {
ob_start():
if (isset($_POST['post_form'])) {
$this->save_to_db();
}
$this->show_template_form();
return ob_get_clean();
}
}
and , as like as I found almost everywhere on Google, to pass content’s variable to the message box div with jquery and I just did an echo so then I should show the error message displaying a dialog message.
But I keep getting in Console Log this error :
<script language="javascript">$("#message_label").text("<br />
<b>Notice</b>: Undefined variable: error_message in <b>..../my_form.php</b> on line <b>108</b><br />
"); $('#messages_box').dialog();</script>
at this point what would be the right way to pass variable’s content to the div? Thanks in advance! Cheers, Luigi