javascript not hiding form - javascript

The error messages on this form ('after') should be getting hidden by default and only showing when the user presses "Register" submit button. However all the error messages are being shown by default - could any help suggest why this isnt working? The javascript file is printing to the console so it can find the file. Ive put the PHP for the form and the javascript below - any help would be very much appreciated, thank you,
This is the PHP to display the form:
<div class="span5 pull-right reg-form hidden">
<?php
echo $this->Form->create('Users.Register', array(
'url' => '/register',
'class' => 'span12 pull-right'));
echo $this->Form->hidden('current_url', array('value' => $this->here));
echo $this->Form->input('username', array(
'class' => 'reg-input',
'label' => array('text' => 'Email:', 'class' => 'reg-label main-color'),
'div' => array('class' => 'span12 reg-div'),
'required' => FALSE,
'after' => '<span class="hidden show-email-err show-err">This email address is already taken</span>
<span class="hidden show-emailvalid-err show-err">This email is not valid</span>'
));
echo $this->Form->input('confirm_username', array(
'class' => 'reg-input',
'label' => array('text' => 'Confirm Email:', 'class' => 'reg-label main-color'),
'div' => array('class' => 'span12 reg-div'),
'required' => FALSE,
'after' => '<span class="hidden show-user-err show-err">Re-email is incorrect</span>'
));
echo $this->Form->input('password', array(
'class' => 'reg-input',
'label' => array('text' => 'Password:', 'class' => 'reg-label main-color'),
'div' => array('class' => 'span12 reg-div'),
'required' => FALSE,
'after' => '<span class="hidden show-pass-long show-err">Password must be at least 6 characters</span>'
));
echo $this->Form->input('confirm_password', array(
'class' => 'reg-input',
'type' => 'password',
'label' => array('text' => 'Confirm Password:', 'class' => 'reg-label main-color'),
'div' => array('class' => 'span12 reg-div'),
'required' => FALSE,
'after' => '<span class="hidden show-pass-err show-err">Re-password is incorrect</span>'
));
echo $this->Form->input('firstname', array(
'class' => 'reg-input',
'label' => array('text' => 'Name:', 'class' => 'reg-label main-color'),
'div' => array('class' => 'span12 reg-div'),
'required' => FALSE,
'after' => '<span class="hidden show-firstname-err show-err">This field must not empty</span>'
));
echo $this->Form->input('lastname', array(
'class' => 'reg-input',
'label' => array('text' => 'Last Name:', 'class' => 'reg-label main-color'),
'div' => array('class' => 'span12 reg-div'),
'required' => FALSE,
'after' => '<span class="hidden show-lastname-err show-err">This field must not empty</span>'
));
?>
<?php echo $this->Form->end(array('label' => 'Register', 'div' => false, 'id' => 'RegSubmit', 'class' => 'btn-sign pull-right')); ?>
</div>
This is the javascript:
(function($){
/** $(document).ready(function(){}) */
$(function(){
console.log('cover');
/*
$('#btn-reg').on('click', function(){
$('#RegisterUsername').val($('#LoginUsername').val());
$('.reg-form').removeClass('hidden');
});
*/
$('#RegSubmit').on('click', function(e) {
//console.log(e);
if($('.show-user-err').attr('class').indexOf('hidden') < 0)
$('.show-user-err').addClass('hidden');
var username = $('#RegisterUsername').val(),
re_username = $('#RegisterConfirmUsername').val(),
password = $('#RegisterPassword').val(),
re_password = $('#RegisterConfirmPassword').val(),
firstname = $('#RegisterFirstname').val(),
lastname = $('#RegisterLastname').val();
var check = 0;
var email_regex = /^([a-z0-9_\.-]+)#([\da-z\.-]+)\.([a-z\.]{2,6})$/;
$.each(user_emails, function(index, value){
if(value==username){
check = 1;
}
});
if(check == 1){
$('.show-err').addClass('hidden');
$('.show-email-err').removeClass('hidden');
return false;
}
if(!email_regex.test(username) || username == '') {
$('.show-err').addClass('hidden');
$('.show-emailvalid-err').removeClass('hidden');
return false;
}
if(username != re_username){
$('.show-err').addClass('hidden');
$('.show-user-err').removeClass('hidden');
return false;
}
if(password.length < 6){
$('.show-err').addClass('hidden');
$('.show-pass-long').removeClass('hidden');
return false;
}
if(password != re_password){
$('.show-err').addClass('hidden');
$('.show-pass-err').removeClass('hidden');
return false;
}
if(firstname == ''){
$('.show-err').addClass('hidden');
$('.show-firstname-err').removeClass('hidden');
return false;
}
if(lastname == ''){
$('.show-err').addClass('hidden');
$('.show-lastname-err').removeClass('hidden');
return false;
}
});
});
})(jQuery);

I'd guess that one of the other classes like show-err has display:block and that's overriding the hidden class. If you inspect the element in either Chrome or Firefox then it should show you the CSS rules that are being applied.

Related

How to submit form without ID in PHP oops

I'm new in Moodle development I'm stuck in Moodle form submission.
Mustache Template Code
<button type="button" onclick="request_access(this)" id="{{ data }}"
class="btn btn-primary col mr-1" data-action="save">
{{#str}} savechanges {{/str}}</button>
Form created in PHP
$editorhtml = '';
$editor = editors_get_preferred_editor();
$editor->use_editor("usernotes", array('autosave' => false));
$editorhtml .= html_writer::start_tag('div', array('class' => 'ltoolusernotes'));
$editorhtml .= html_writer::start_tag('form', array('method' => 'post', 'action' => $args['pageurl'], 'id' => 'notes-from', 'class' => 'mform'));
$editorhtml .= html_writer::tag(
'textarea',
'',
array('id' => "usernotes", 'name' => 'ltnoteeditor', 'class' => 'form-group', 'rows' => 20, 'cols' => 100)
);
$editorhtml .= html_writer::tag('input', '', array(
'type' => 'hidden',
'name' => 'course',
'value' => $args['course'],
));
$editorhtml .= html_writer::tag('input', '', array(
'type' => 'hidden',
'name' => 'contextid',
'value' => $args['contextid'],
));
$editorhtml .= html_writer::tag('input', '', array(
'type' => 'hidden',
'name' => 'contextlevel',
'value' => $args['contextlevel'],
));
$editorhtml .= html_writer::tag('input', '', array(
'type' => 'hidden',
'name' => 'pagetype',
'value' => $args['pagetype'],
));
$editorhtml .= html_writer::tag('input', '', array(
'type' => 'hidden',
'name' => 'pagetitle',
'value' => $args['pagetitle'],
));
$editorhtml .= html_writer::tag('input', '', array(
'type' => 'hidden',
'name' => 'pageurl',
'value' => $args['pageurl'],
));
$editorhtml .= html_writer::tag('input', '', array(
'type' => 'hidden',
'name' => 'user',
'value' => $args['user'],
));
$editorhtml .= html_writer::end_tag('form');
$editorhtml .= html_writer::end_tag('div');
$editorhtml .= ltool_note_load_context_notes($args);
Firstly, I'm confused, button and form both are located in different locations nevertheless they are connected how?
So I can't use the form id in the button's file.
I have tried a solution which is reload()(event) the page on reload data submit but sometimes reload event is fired before the form submission. so data can't submit.
what is the solution? I want to solve it using JS.

I need to redirect an email form in PHP Script to another page once the form is sent

friends of Stack overflow, what happens is that I have an email form in PHP Script and the idea is that once one sent the message successfully, I redirect you to a page, in my case I want to redirect users to a "Thank you" page.
I have managed that once the message is sent, a green box appears saying "Thank you for sending your message" and when it is not sent, an error box comes out, but I have not managed to redirect once it was successfully sent to a "Thank you" page.
This is the code
<?php
require_once('FormProcessor.php');
$form = array(
'subject' => 'New Form Submission',
'email_message' => 'You have a new form submission',
'success_redirect' => '',
'sendIpAddress' => true,
'email' => array(
'from' => '',
'to' => 'myemail#gmail.com'
),
'fields' => array(
'name' => array(
'order' => 1,
'type' => 'string',
'label' => 'Name',
'required' => true,
'errors' => array(
'required' => 'Field \'Name\' is required.'
)
),
'email' => array(
'order' => 2,
'type' => 'email',
'label' => 'Email',
'required' => true,
'errors' => array(
'required' => 'Field \'Email\' is required.'
)
),
'message' => array(
'order' => 3,
'type' => 'string',
'label' => 'Message',
'required' => true,
'errors' => array(
'required' => 'Field \'Message\' is required.'
)
),
)
);
$processor = new FormProcessor('');
$processor->process($form);
?>
this is the html
Name
Email
Message
Submit
Thank you! Your message has been sent.
Unable to send your message. Please fix errors then try again.
use this header("Location: https://somewahere.com/thankYouPage.php");die();

Drupal disable button after clicking 3 times in drupal custom module

I'm new to Drupal
I built custom module similar to webform
my module page contains two submit buttons and 2 textbox as below :
function contact_request($form, &$form_state) {
$form ['info'] =array(
'#markup' => "<div id='pageRequestDiv'>",
);
$form['number'] = array(
'#prefix' => '<div class="webform-container-inline2">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Number'),
'#size' => 20,
'#maxlength' => 255,
'#attributes'=>array('class'=>array('txtli')),
'#required'=>true,
);
$form['send_vcode'] = array(
'#prefix' => '<div style="margin-top:30px;">',
'#suffix' => '</div><br/><br/>',
'#type' => 'submit',
'#value' => t('Send Verification Code'),
'#ajax' => array(
'callback' => 'send_Verification_code_callback',
'wrapper' => 'ContactUsMsgDiv',
'method'=>'replace',
'effect'=>'fade',
),
);
$form['verification_code'] = array(
'#prefix' => '<div class="webform-container-inline2">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Verification Code'),
'#size' => 20,
'#maxlength' => 255,
'#attributes'=>array('class'=>array('txtli')),
'#required'=>true,
);
$form['sendmail'] = array(
'#prefix' => '<div style="margin-top:30px;">',
'#suffix' => '</div></div>',
'#type' => 'submit',
'#value' => t('Send Request'),
'#ajax' => array(
'callback' => 'get_contact_us_callback',
'wrapper' => 'ContactUsMsgDiv',
'method'=>'replace',
'effect'=>'fade',
),
);
return $form;
}
the user enter number then first button send him sms to mobile after that he typed sms in second text box then click send.
I want to disable first button after user clicking it 3 times.
I tried the JS below but it's disable both buttons after one click. I want to disable first button only after 3 clicks
Drupal.behaviors.hideSubmitButton = {
attach: function(context) {
$('form.node-form', context).once('hideSubmitButton', function () {
var $form = $(this);
$form.find('input.form-submit').click(function (e) {
var el = $(this);
el.after('<input type="hidden" name="' + el.attr('name') + '" value="' + el.attr('value') + '" />');
return true;
});
$form.submit(function (e) {
if (!e.isPropagationStopped()) {
$('input.form-submit', $(this)).attr('disabled', 'disabled');
return true;
}
});
});
}
};
Updated code :
function send_Verification_code_callback($form, &$form_state){
some code to send sms
return $form;
}
function contact_us_request($form, &$form_state) {
$form['#prefix'] = '<div id="my-form-wrapper">';
$form['#suffix'] = '</div>';
$form ['info'] =array(
'#markup' => "<div id='pageAbiliRequestDiv'>",
);
$form['contact_number'] = array(
'#prefix' => '<div class="webform-container-inline2">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Contact Number'),
'#size' => 20,
'#maxlength' => 255,
'#attributes'=>array('class'=>array('txtabili')),
'#required'=>true,
);
$form['verification_code'] = array(
'#prefix' => '<div class="webform-container-inline2">',
'#suffix' => '</div>',
'#type' => 'textfield',
'#title' => t('Verification Code'),
'#size' => 20,
'#maxlength' => 255,
'#attributes'=>array('class'=>array('txtabili')),
'#required'=>true,
);
$form['send_vcode'] = array(
'#prefix' => '<div style="margin-top:30px;">',
'#suffix' => '</div><br/><br/>',
'#type' => 'submit',
'#value' => t('Send Verification Code'),
'#ajax' => array(
'callback' => 'send_Verification_code_callback',
'wrapper' => 'my-form-wrapper',
'method'=>'replace',
'effect'=>'fade',
),
);
$form['sendmail'] = array(
'#prefix' => '<div style="margin-top:30px;">',
'#suffix' => '</div></div>',
'#type' => 'submit',
'#value' => t('Send Request'),
'#ajax' => array(
'callback' => 'get_contact_us_callback',
'wrapper' => 'ContactUsMsgDiv',
'method'=>'replace',
'effect'=>'fade',
),
);
$form['clicks'] = array(
'#type' => 'value',
);
if (isset($form_state['values']['clicks'])) {
if ($form_state['values']['clicks'] == 3) {
$form['send_vcode']['#disabled'] = TRUE;
} else {
$form['clicks']['#value'] = $form_state['values']['clicks'] + 1;
}
} else {
$form['clicks']['#value'] = 0;
}
I'd use AJAX callback rather than JavaScript check.
Wrap the whole form with some div:
$form['#prefix'] = '<div id="my-form-wrapper">';
$form['#suffix'] = '</div>';
and set
$form['send_vcode'] = array(
...
'#ajax' => array(
'wrapper' => 'my-form-wrapper',
...
(and also include your message area into the form). In your send_Verification_code_callback function return the whole form.
The trick is then to add value component to the form which will contain number of clicks:
$form['clicks'] = array(
'#type' => 'value',
);
if (isset($form_state['values']['clicks'])) {
if ($form_state['values']['clicks'] == 3) {
$form['send_vcode']['#disabled'] = TRUE;
} else {
$form['clicks']['#value'] = $form_state['values']['clicks'] + 1;
}
} else {
$form['clicks']['#value'] = 0;
}
After 3 clicks on send_vcode button it will be disabled.
=== UPDATE ===
Here is working code (without all unnecessary stuff), which shows amount of clicks left in the pageAbiliRequestDiv div:
function contact_us_request($form, &$form_state) {
$form['#prefix'] = '<div id="my-form-wrapper">';
$form['#suffix'] = '</div>';
$form['clicks'] = array(
'#type' => 'value',
);
$clicks_max = 3;
if (isset($form_state['values']['clicks'])) {
$form['clicks']['#value'] = $form_state['values']['clicks'] + 1;
$clicks_left = $clicks_max - 1 - $form_state['values']['clicks'];
} else {
$form['clicks']['#value'] = 0;
$clicks_left = $clicks_max;
}
$form ['info'] =array(
'#markup' => '<div id="pageAbiliRequestDiv">'
. t('Clicks left: #clicks_left', array('#clicks_left' => $clicks_left))
. '</div>',
);
$form['send_vcode'] = array(
'#prefix' => '<div style="margin-top:30px;">',
'#suffix' => '</div><br/><br/>',
'#type' => 'button',
'#value' => t('Send Verification Code'),
'#ajax' => array(
'callback' => 'send_Verification_code_callback',
'wrapper' => 'my-form-wrapper',
'method'=>'replace',
'effect'=>'fade',
),
);
if ($clicks_left == 0) {
$form['send_vcode']['#disabled'] = TRUE;
}
return $form;
}
function send_Verification_code_callback($form, &$form_state) {
return $form;
}

Calculate value auto into form field using JS

I have six fields which are editable in my Yii2 form widget. What i want to do is once i input integer values into editable fields calculate "Green leaf net weight" automatically based on the given input. my coding are work perfectly. But the problem is if i change already input value in the fields it'll not update the the "Green leaf net weight" read only field.
$(document).ready(function(){
var A = $('#A');
var B = $('#B');
var C = $('#C');
var D = $('#D');
var E = $('#E');
E.change(function(e){
var result = ((A.val() - B.val()) * C.val())/100;
var result2 = ((A.val() - B.val()) * D.val()) / 100;
var final = ((A.val() - B.val()) - result - result2) - E.val();
Math.round(final);
$('#F').val(final);
})
});
my form widget coding (updated code)
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'Gross_weight',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',
]
])->textInput(['id' => 'A', **'class' => 'factors'** ]) ?>
<div class="panel panel-primary" style="padding: 10px;">
<div style="text-align: center; color: #008000;"><b>Deductions</b></div>
<?= $form->field($model, 'Bags_count',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',
]
])->textInput() ?>
<?= $form->field($model, 'Bags_weight',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',
]
])->textInput(['id' => 'B', **'class' => 'factors'** ]) ?>
<?= $form->field($model, 'Course_leaf',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',
]
])->textInput(['id' => 'C', **'class' => 'factors'**]) ?>
<?= $form->field($model, 'Water',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',
]
])->textInput(['id' => 'D', **'class' => 'factors'**]) ?>
<?= $form->field($model, 'Boiled_leaf',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',
]
])->textInput(['id' => 'E',**'class' => 'factors'**]) ?>
</div>
<?= $form->field($model, 'Greanleaf_net_weight',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',
]
])->textInput(['disabled' => 'disabled', 'id' => 'F']) ?>
Thanks for everyone.
Inorder to execute the calculation on the change of all the input fields you have to add a class to all the input fields and use that class as the jQuery selector for change event.
<input id="A" class="factors" ... />
...
...
$(".factors").change(function(e) {
...
...
}
See Demo

Toggle field with jquery and woocommerce function

I have a input like this on my first page <input type="number" id="people" name="ppl" min="1" class="uniform-input number" value="7" required="">
I wanted to use the value to toggle some fields on the woocommerce checkout. I want to show 2 name fields when the input value="2" and so on. But the code deletes all the custom fields. I don't know if I classed and targeted the div correctly Does anyone know what the problem is?
header.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function () {
toggleFields();
$("#people").on("keyup change", function () {
toggleFields();
});
});
function toggleFields() {
$("#personen1, #personen2, #personen3, #personen4, #personen5, #personen6, #personen7, #personen8").hide();
if ($("#people").val() < 9)
$("#personen" + $("#people").val()).show();
}
</script>
functions.php
function wordimpress_custom_checkout_field( $checkout ) {
//Check if Book in Cart (UPDATE WITH YOUR PRODUCT ID)
$book_in_cart = wordimpress_is_conditional_product_in_cart( 759 );
//Book is in cart so show additional fields
if ( $book_in_cart === true ) {
echo '<div class="container" id="personen1"><div class="jumbotron"><h2 style="color:red;">' . __( 'Buchungsinformationen' ) . '</h2>';
woocommerce_form_field( 'person1_name', array(
'type' => 'text',
'class' => array( 'person1 form-row-wide' ),
'placeholder' => 'Vorname und Nachname eingeben',
'label' => __( '1. Person' ),
), $checkout->get_value( 'person1_name' ) );
echo '</div>';
echo '<div class="container" id="personen2"><div class="jumbotron">';
woocommerce_form_field( 'person1_name', array(
'type' => 'text',
'class' => array( 'person1 form-row-wide' ),
'placeholder' => 'Vorname und Nachname eingeben',
'label' => __( '1. Person' ),
), $checkout->get_value( 'person1_name' ) );
woocommerce_form_field( 'person2_name', array(
'type' => 'text',
'class' => array( 'person2 form-row-wide' ),
'placeholder' => 'Vorname und Nachname eingeben',
'label' => __( '2. Person' ),
), $checkout->get_value( 'person2_name' ) );
echo '</div>';
}
}

Categories

Resources