Duda con ajax

Mensajes
220
Puntuación de reacción
0
Buenas , quería preguntar un problema que estoy teniendo en adaptar un form con un validador ajax , el caso es que venía con unos requerimientos obligatorios pero yo he querido quitarlos.

Este seria el form : Anadir Tarea/Examen: Agenda Comercio y Marketing

Su código actual (CON FORMS DE IMAGEN , EMAIL , NOMBRE Y LINK RETIRADOS) :
Código:
<?php if(!defined('PARENT')) { exit; }

/* EVENTS - ADD TEMPLATE
----------------------------------*/

?>

    <div class="row">
      <div class="col-lg-9 col-md-8">

        <h1><?php echo $this->TEXT[0]; ?></h1>

        <div class="panel panel-default">
          <div class="panel-body">
            <?php echo $this->TEXT[1]; ?>
          </div>
        </div>

        <form method="post" action="index.php" id="sform" enctype="multipart/form-data">
         

          <div class="panel panel-default">
            <div class="panel-heading">
             <i class="fa fa-file-text-o fa-fw"></i> <?php echo $this->TEXT[4]; ?>
            </div>
            <div class="panel-body">
              <div class="form-group">
                <label>Título de la tarea/exámen</label>
                <input type="text" class="form-control" name="fm[title]" value="">
              </div>
              
              <div class="form-group">
                <label>Descripción de la tarea/exámen</label>
                <textarea name="fm[description]" rows="4" cols="20" class="form-control evdesc"></textarea>
              </div>
            </div>
          </div>

          <div class="panel panel-default">
            <div class="panel-heading">
               <i class="fa fa-indent fa-fw"></i> <?php echo $this->TEXT[8]; ?>
            </div>
            <div class="panel-body">
              <div class="form-group">
                <?php
                // CATEGORIES
                // html/checkbox.htm
                echo $this->CATEGORIES;
                ?>
              </div>
            </div>
          </div>

          <div class="panel panel-default">
            <div class="panel-heading">
               <i class="fa fa-calendar-check-o fa-fw"></i> <?php echo $this->TEXT[10]; ?>
            </div>
            <div class="panel-body">
              <div class="form-group">
                <label><?php echo $this->TEXT[11]; ?></label>
                <div class="form-group input-group">
                  <span class="input-group-addon"><i class="fa fa-calendar fa-fw"></i></span>
                  <input type="text" class="form-control mswdatepicker" name="fm[fromdate]" value="">
                </div>
              </div>
              <div class="form-group">
                <label><?php echo $this->TEXT[12]; ?></label>
                <div class="form-group input-group">
                  <span class="input-group-addon"><i class="fa fa-calendar fa-fw"></i></span>
                  <input type="text" class="form-control mswdatepicker" name="fm[todate]" value="">
                </div>
              </div>
            </div>
          </div>

          
          

          
          <div class="panel panel-default">
            <div class="panel-heading">
               <i class="fa fa-cog fa-fw"></i> <?php echo $this->TEXT[20]; ?>
            </div>
            <div class="panel-body">
              <div class="form-group">
                <div class="checkbox">
                  <label><input type="checkbox" name="fm[encomm]" value="yes" checked="checked"> <?php echo $this->TEXT[21]; ?></label>
                </div>
              </div>
            </div>
          </div>

          <div class="text-center" style="margin-bottom:30px">
            <?php
            // DO NOT remove this field. It checks if a bot has tried to submit form
            // If this field contains any value at all, the form fails.
            // Easy spam protection without captchas. :))
            // If spam is still a problem, enable the CleanTalk API
            ?>
            <input type="hidden" name="fm[a_code]" value="">
            <button class="btn btn-success" type="button" onclick="mswEvnt('add')"><i class="fa fa-plus fa-fw"></i> <?php echo $this->TEXT[22]; ?></button>
          </div>
          </div>
        </form>

      </div>
      <div class="col-lg-3 col-md-4">
        <?php
        include(dirname(__file__) . '/right-panel.tpl.php');
        ?>
      </div>
    </div>

Función add_event en JAVASCRIPT :
Código:
function mswEvnt(act, plan) {
  var flds = ['plan','title','location','description','fromdate','a_nm','a_em','u_nm','u_em'];
  for (var i=0; i<flds.length; i++) {
    // If no plan is attached to event, force skip..
    if (flds[i] == 'plan' && plan > 0) {
      flds[i] = 'skiplan';
    }
    if (jQuery('select[name="fm[' + flds[i] + ']"]').val() == '' || jQuery('select[name="fm[' + flds[i] + ']"]').val() == '0') {
      jQuery('select[name="fm[' + flds[i] + ']"]').focus();
      return false;
    }
    if (jQuery('input[name="fm[' + flds[i] + ']"]').val() == '') {
      jQuery('input[name="fm[' + flds[i] + ']"]').focus();
      return false;
    }
    if (jQuery('textarea[name="fm[' + flds[i] + ']"]').val() == '') {
      jQuery('textarea[name="fm[' + flds[i] + ']"]').focus();
      return false;
    }
  }
  if (jQuery('input[name="fm[ae_ct_ts]"]')) {
    jQuery('input[name="fm[ae_ct_ts]"]').remove();
  }
  var confirmSub = confirm(mswlang.aus);
  if (confirmSub) {
    // For cleantalk..append hidden var..
    // Only required on add page
    var d = new Date();
    jQuery('#sform').append('<input type="hidden" name="fm[ae_ct_ts]" value="' + d.getFullYear() + '">');
    jQuery(document).ready(function() {
      mswShowSpin();
      var fd = new FormData(jQuery('#sform')[0]);
      jQuery.ajax({
        type: 'POST',
        url: 'index.php?ajax-ops=' + act,
        data: fd,
        processData: false,
        contentType: false,
        cache: false,
        dataType: 'json',
        success: function (data) {
          mswCloseSpin();
          switch(data['status']) {
            case 'ok':
              if (data['rdr']) {
                window.location = data['rdr'];
              } else {
                mswDialog(data['txt'][0], data['txt'][1], data['status']);
              }
              break;
            default:
              mswDialog(data['txt'][0], data['txt'][1], data['status']);
              break;
          }
        }
      });
    });
    return false;
  } else {
    return false;
  }
}

ADD EVENT EN AJAX :
Código:
$fm = array(
          'plan' => (isset($_POST['fm']['plan']) ? (int) $_POST['fm']['plan'] : '0'),
          'title' => (isset($_POST['fm']['title']) && $_POST['fm']['title'] ? $_POST['fm']['title'] : ''),
          'location' => (isset($_POST['fm']['location']) && $_POST['fm']['location'] ? $_POST['fm']['location'] : ''),
          'description' => (isset($_POST['fm']['description']) && $_POST['fm']['description'] ? $_POST['fm']['description'] : ''),
          'cat' => (!empty($_POST['fm']['cat']) ? $_POST['fm']['cat'] : array()),
          'fromdate' => (isset($_POST['fm']['fromdate']) && $_POST['fm']['fromdate'] ? $DT->jstots($_POST['fm']['fromdate']) : $DT->ts()),
          'todate' => (isset($_POST['fm']['todate']) && $_POST['fm']['todate'] ? $DT->jstots($_POST['fm']['todate']) : '0'),
          'ifile' => (!empty($_FILES['fm']['tmp_name']['ifile']) ? $_FILES['fm']['tmp_name']['ifile'] : array()),
          'lprotocol' => (!empty($_POST['fm']['lprotocol']) ? $_POST['fm']['lprotocol'] : array()),
          'lurl' => (!empty($_POST['fm']['lurl']) ? $_POST['fm']['lurl'] : array()),
          'lname' => (!empty($_POST['fm']['lname']) ? $_POST['fm']['lname'] : array()),
          'name' => (isset($_POST['fm']['a_nm']) && $_POST['fm']['a_nm'] ? $_POST['fm']['a_nm'] : ''),
          'email' => (isset($_POST['fm']['a_em']) && mswEmVal($_POST['fm']['a_em']) == 'yes' ? $_POST['fm']['a_em'] : ''),
          'code' => (isset($_POST['fm']['a_code']) ? $_POST['fm']['a_code'] : 'xx'),
          'ct_ts' => (isset($_POST['fm']['ae_ct_ts']) ? $_POST['fm']['ae_ct_ts'] : 'fail'),
          'encomm' => (isset($_POST['fm']['encomm']) ? 'yes' : 'no')
        );
        $EVOP->log(
        '======================================================' . mswNL() .
        'START - ADD EVENT' . mswNL() .
        '======================================================' . mswNL() .
        'Event: ' . $fm['title'] . mswNL() .
        'Location: ' . $fm['location'] . mswNL() .
        'Contact Name: ' . $fm['name'] . mswNL() .
        'Contact Email: ' . $fm['email']);
        $plData = $EVENTS->getplan($fm['plan']);
        if ($fm['plan'] > 0 && isset($plData->id)) {
          if ($fm['title'] == '' || $fm['location'] == '' || $fm['description'] == '') {
            $formErrors[] = $msw_add_errors3;
          } else {
            if ($SETTINGS->dupes == 'yes' && $DB->db_rowcount('events', 'WHERE `title` = \'' . mswSQL($fm['title']) . '\'') > 0) {
              $formErrors[] = $msw_add_errors16;
            }
          }
          if (empty($fm['cat'])) {
            $formErrors[] = $msw_add_errors4;
          }
          if ($DT->date('Y-m-d', $fm['fromdate']) < $DT->date('Y-m-d', $DT->ts())) {
            $formErrors[] = $msw_add_errors5;
          }
          if ($fm['todate'] > 0 && $DT->date('Y-m-d', $fm['todate']) < $DT->date('Y-m-d', $fm['fromdate'])) {
            $formErrors[] = $msw_add_errors6;
          }
          $aFiles = $EVOP->checkfiles($fm, $plData);
          if ($aFiles[0] == 'none') {
            $formErrors[] = $msw_add_errors7;
          } elseif ($aFiles[0] == 'exceed') {
            $formErrors[] = str_replace('{count}', $plData->pics, $msw_add_errors12);
          } else {
            if ($aFiles[0] == 'fail') {
              $EVOP->clearfiles($fm);
              $formErrors[] = $msw_add_errors9;
            }
          }
          $aLinks = $EVOP->checklinks($fm, $plData);
          if ($aLinks[0] == 'none') {
            $formErrors[] = $msw_add_errors8;
          } elseif ($aLinks[0] == 'exceed') {
            $formErrors[] = str_replace('{count}', $plData->links, $msw_add_errors13);
          } else {
            if ($aLinks[0] == 'fail') {
              $formErrors[] = $msw_add_errors10;
            }
          }
          if ($fm['name'] == '' || $fm['email'] == '') {
            $formErrors[] = $msw_add_errors11;
          }
          if (!empty($formErrors)) {
            $EVOP->log('Add event terminating: ' . count($formErrors) . ' error(s)');
            $arr['txt'][1] = implode('<br>', $formErrors);
          } else {
            if ($fm['code'] == '' && $fm['ct_ts'] != 'fail') {
              $sendM = 'yes';
              if (!defined('SKIP_CLEANTALK')) {
                // Is cleantalk enabled?
                $api = $SCL->params('ctalk');
                if (isset($api['ctalk']['enable'],$api['ctalk']['key']) && $api['ctalk']['enable'] == 'yes' && $api['ctalk']['key']) {
                  include(PATH . 'control/classes/system/class.cleantalk.php');
                  $CTK           = new cleanTalk();
                  $CTK->settings = $SETTINGS;
                  $CTK->social   = $api;
                  $CTK->dt       = $DT;
                  // Check message submission..
                  $ctkc = $CTK->check(array(
                    'method' => 'check_message',
                    'email' => $fm['email'],
                    'name' => $fm['name'],
                    'comms' => $fm['description'],
                    'ct_ts' => $fm['ct_ts']
                  ));
                  if (!isset($ctkc['allow']) || $ctkc['allow'] == 0) {
                    $sendM = 'no';
                  }
                }
              }
              // Ok, we can continue..
              if ($sendM == 'yes') {
                // Images..
                $fm['files'] = $aFiles[1];
                // Links..
                $fm['links'] = $aLinks[1];
                // Add event..
                $eventID = $EVOP->addevent($fm);
                // Ops..
                switch($eventID[2]) {
                  case 'nopay':
                  case 'approve':
                  case 'live':
                    $EVOP->log('Event status: "' . $eventID[2] . '", redirecting to payment gateway (' . str_replace('{id}', $eventID[0], $GW->glog) . ')...');
                    $arr['status'] = 'ok';
                    $arr['rdr'] = $MODR->url(array(
                      $MODR->config['slugs']['cks'] . '/' . $eventID[0] . '-' . $eventID[1],
                      'checksend=yes&sID=' . $eventID[0] . '-' . $eventID[1]
                    ));
                    break;
                  case 'failed':
                    $arr['txt'][1] = $msw_add_errors15;
                    break;
                }
              } else {
                // If cleantalk is enabled, we`ll show a different error message..
                if (isset($api['ctalk']['enable'],$api['ctalk']['key']) && $api['ctalk']['enable'] == 'yes' && $api['ctalk']['key']) {
                  $arr['txt'][1] = $msw_add_errors14;
                }
              }
            }
          }
        } else {
          $arr['txt'][1] = $msw_add_errors2;
        }
        $EVOP->log(
        '======================================================' . mswNL() .
        'END - ADD EVENT' . mswNL() .
        '======================================================'
        , false);
        break;
      case 'add-prev':
        // Not implemented yet..
        break;
    }
    break;


Llevo muchas horas con esto y me estoy comiendo la cabeza :S

Gracias de antemano
 
Arriba