We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3217
    • 6 Posts
    Hello all! I’m developing a collage project, it’s about inserting and processing cllage carrer pensums. I’m using eForm for collect the data about the carrer and subject’s. Also use jQuery to add/repeat subject fields, so the user can dynamically add subject to the carrer. Let’s the code talk for itself...

    eForm template
    <h3>Nuevo pensum</h3>
    <form method="post" action="[~[*id*]~]" id="newPensum" name="newPensum">
    	<fieldset id="Catedras">
    		<h4>Sede y carrera</h4>
    		<input name="newPensum" type="hidden" value="NewPensum" />
    		<div id="Labels"><label for="uni_name">Sede:</label></div>
    		<select name="uni_name" id="uni_name" eform="Sede::1:Eliga una sede de la carrera:#SELECT id_uni, uni_name FROM {PREFIX}sedes WHERE id_stat=1">
    			[[getUni2Options]]
    		</select><br />
    		<div id="Labels"><label for="carrer_descp">Carrera:</label></div>
                  	<input type="text" name="carrer_descp" id="carrer_descp" size="40" eform="Carrera:string:1:Los numeros y simbolos no son aceptados:" /><br />
                  	<hr />
    		<h4>Catedra</h4>
    		<div id="Labels"><label for="id_mat">Cod. </label></div>
                  	<input type="text" name="id_mat" id="id_mat" size="15" eform="Cod. Catedra:integer:1:Solo numeros aceptados:" /><br />
    		<div id="Labels"><label for="mat_descp">Catedra:</label></div>
                  	<input type="text" name="mat_descp" id="mat_descp" size="40" eform="Catedra:string:1:Simbolos especiales no aceptados:" /><br />
    		<div id="Labels"><label for="mat_sem">Semestre:</label></div>
                  	<input type="text" name="mat_sem" id="mat_sem" size="5" eform="Semestre:integer:1:Letras y simbolos no son aceptados:" /><br />
    		<div id="Labels"><label for="mat_credit">Creditos: </label></div>
                  	<input type="text" name="mat_credit" id="mat_credit" size="5" eform="Creditos:integer:1:Letras y simbolos no son aceptados:" /><br />
                    <input type="checkbox" name="mat_basico" id="mat_basico" eform="Cat. Basica::::" />
                    <label for="mat_basico">¿Materia básica?</label><br />
    	</fieldset>
            <span><a href="#" onclick="return false;" id="otraCatedra">Añadir Catedra</a></span>
            <input name="Reset" type="reset" value="Cancelar" />
            <input name="Submit" type="submit" value="Aceptar" />
    </form>
    


    The getUni2Options is just a snippet who gets values from DB and print them as option select values, by a parse chunk:
    <?php
    global $table_prefix;
    $output = '';
    
    $sql = $modx->db->query('SELECT `id_uni`,`uni_name` FROM `'.$table_prefix.'sedes`');
    $resultArray = $modx->db->makeArray($sql);
    
    foreach($resultArray as $item) {
        $params['Name']=$item['uni_name'];
        $params['ID']=$item['id_uni'];
        $output.=$modx->parseChunk('getOptions', $params, '[+', '+]');
    }
    
    return $output;
    ?>
    


    eForm2db code
    <?php
    function insertPensum( &$fields ) {
          global $modx, $table_prefix;
          $tbl_carrera = $table_prefix.'carreras';
          $tbl_catedra = $table_prefix.'catedras';
          define('IDSTAT',1);
          
          //Busco los campos de las carreras
          $dbTable = array();
    
          $dbTable[id_stat] = IDSTAT;
          $dbTable[carrer_descp] = $fields[carrer_descp];
    
          // Run the db insert query
          $dbQuery = $modx->db->insert($dbTable, $tbl_carrera);
         
          //Busco los campos de las catedras
          $dbtbl_catds = array();
          $last_unid = array(); 
          $last_unid = $modx->db->query('SELECT `id_carrer` FROM `'.$tbl_carrera.'`'); //Select the last ID generated autoincrementally by last insert query
    
          $dbtbl_catds[id_stat] = IDSTAT;
          $dbtbl_catds[id_carrer] = array_pop($last_unid['id_carrer']); 
          $dbtbl_catds[id_mat] = $fields[id_mat];
          $dbtbl_catds[mat_descp] = $fields[mat_descp];
          $dbtbl_catds[mat_sem] = $fields[mat_sem];
          $dbtbl_catds[mat_credit] = $fields[mat_credit];
          $dbtbl_catds[mat_basico] = $fields["mat_basico"];
          
          // Run the db insert query
          $dbQuery = $modx->db->insert($dbtbl_catds, $tbl_catedra);
          
          return true;
    }
    ?>
    


    jQuery Snippet
    <?php
    $src="<script type=\"text/javascript\" charset=\"utf-8\">
    	$(document).ready(function(){
    		$(\"#otraCatedra\").click( function() {
    			$(\"#Catedras\").append('<hr /><div id=\"Labels\"><label for=\"id_mat\">Cod. </label></div><input type=\"text\" name=\"id_mat\" id=\"id_mat\" size=\"15\" /><br /><div id=\"Labels\"><label for=\"mat_descp\">Catedra:</label></div><input type=\"text\" name=\"mat_descp\" id=\"mat_descp\" size=\"40\" /><br /><div id=\"Labels\"><label for=\"mat_sem\">Semestre:</label></div><input type=\"text\" name=\"mat_sem\" id=\"mat_sem\" size=\"5\" /><br /><div id=\"Labels\"><label for=\"mat_credit\">Creditos: </label></div><input type=\"text\" name=\"mat_credit\" id=\"mat_credit\" size=\"5\" /><br /><input type=\"checkbox\" name=\"mat_basico\" id=\"mat_basico\" /><label for=\"mat_basico\"> ¿Materia básica?</label><br />');
    		});
    	}); 
    </script>";
    $modx->regClientStartupScript($src);
    ?>
    


    Obviously, the jQuery lib call
    <?php
    $modx->regClientStartupScript("assets/js/jquery-1.3.2.min.js");
    ?>
    


    And, last but not least, how I call all of them in the doc
    [[add_jQuery]]
    [[addCatedras]]
    [!insertPensum!]
    [!eForm? &noemail=`true` &formid=`newPensum` &eFormOnBeforeMailSent=`insertPensum` &tpl=`newPensum` &thankyou=`2`!]
    


    The problem is when I submit the form: It doesn’t insert the subject data in DB from the fields. If anyone can help me, I would appreciated. =)

    Betacar