Are you first timer at lazacode.org? Why not join us and start exchange your programming knowledge with everyone. Register today!

Drupal - create form and validation

0 votes
20 views
added Sep 6 in Drupal by LC Marshal Lieutenant (14,570 points)
$account = user_load($user->uid);

<a class="tell-me-more" href="#tellMeMore" data-toggle="modal">Tell me more about this unit</a>

<div class="modal fade bd-contact-modal" id="tellMeMore" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <button type="button" class="close-modal" data-dismiss="modal" aria-label="Close">
      <i class="material-icons" style="color:white;font-size: 30px;">clear</i>
  </button>
  <div class="modal-dialog contact-modal-dialog">
      <div class="modal-content">
          <div class="container-fluid">
              <div class="row">
                <div class="contact-modal">
                  <h3 class="text-center m-b-5">Get in touch with us</h3>
                  <p class="text-center login-desc">You have questions. We have answers.<br> Chat one-on-one with a consultant about this project.</p>
                      <?php 
                      // print $_SESSION['login_error'];
                      // unset($_SESSION['login_error']);
                      //print $GLOBALS['messages']; 
                      ?>
                    <form id="tellMeMore" method="POST">
                      <div class="login-outer">
                      <div class="login-inner">
                        <div id="tellMeMoreMsg"></div>
                        <div class="form-item form-item-name form-type-textfield form-group"> 
                          <label class="control-label" for="tmm-name">Name (as per IC)</label>
                          <input class="form-control form-text required" title="" type="text" id="tmm-name" name="name" value="<?php print $account->field_name_as_ic['und'][0]['value'];?>" size="60" maxlength="60" data-original-title="Name (as per IC)" placeholder="Name (as per IC)">
                        </div>
                        <div class="form-item form-item-name form-type-textfield form-group"> 
                          <label class="control-label" for="tmm-contact">Contact number</label>
                          <input class="form-control form-text required" title="" type="text" id="tmm-contact" name="contact" value="<?php print $account->field_phone_number['und'][0]['value'];?>" size="60" maxlength="60" data-original-title="Contact number" placeholder="Contact number">
                        </div>
                        <div class="form-item form-item-name form-type-textfield form-group"> 
                          <label class="control-label" for="tmm-email">Email</label>
                          <input class="form-control form-text required" title="" type="text" id="tmm-email" name="email" value="<?php print $user->mail;?>" size="60" maxlength="60" data-original-title="Email" placeholder="Email">
                        </div>
                        <div class="form-actions form-wrapper form-group">
                          <input type="hidden" id="property-id" name="property-id" value="<?php print $node->nid;?>">
                          <input type="hidden" id="user-id" name="user-id" value="<?php print $user->uid;?>">
                          <button class="btn btn-success form-submit icon-before btn form-submit" type="submit" name="op" value="Get in touch">Get in touch</button>
                        </div>                         
                      </div>
                      </div>
                    </form>
                </div>
              </div>
          </div>
      </div>
  </div>
</div>

<script>
  (function($){
    $('#tellMeMore').submit(function(event) {
      // get the form data
      // there are many ways to get this data using jQuery (you can use the class or id also)
      var formData = {
          'name'              : $('input#tmm-name[name=name]').val(),
          'email'             : $('input#tmm-email[name=email]').val(),
          'contact'           : $('input#tmm-contact[name=contact]').val(),
          'user-id'           : $('#user-id').val(),
          'unit-id'           : $('#units-available').val(),
          'property-id'       : $('#property-id').val()
      };
      var buyrequesturl = Drupal.settings.basePath + 'api/buy-request';
      // process the form
      $.ajax({
          type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
          url         : location.protocol + '//' + location.host + buyrequesturl, // the url where we want to POST
          data        : formData, // our data object
          dataType    : 'json', // what type of data do we expect back from the server
          encode      : true
      })
      // using the done promise callback
      .done(function(data) {
        if (!data.success) {
          if(data.status == "failed"){
          $('#tellMeMoreMsg').replaceWith('<div class="alert" id="tellMeMoreMsg">' + data.message + '</div>');
          $('#tellMeMoreMsg').addClass('alert-danger');
          $('#tellMeMoreMsg').removeClass('alert-success');
          }else if(data.status == "success"){
          $('#tellMeMoreMsg').replaceWith('<div class="alert" id="tellMeMoreMsg">' + data.message + '! One of our representatives will contact you shortly.</div>');
          $('#tellMeMoreMsg').addClass('alert-success');
          $('#tellMeMoreMsg').removeClass('alert-danger');
          }
        } else {
          //console.log(data);
        }
    });
    // stop the form from submitting the normal way and refreshing the page
    event.preventDefault();
    });

  })(jQuery);
</script>

 

Please log in or register to response this reference.

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...