The correct syntax to use the trim () function in a form that uses javascript and php

1

I have a problem using the function trim , the problem is to modify the value of an object by a variable that is influenced by the function trim .

var mat = $("#Name").val().trim();
var opc = $("#Name");

What I'm going for is: I created a variable in javascript that takes the original value of the object, so that by using the function replace() modify the original object by the value of the variable with trim :

pru = opc.replace(opc, mat);

But it does not change the value, it gives me the value of:

[Object, object]

I leave all the code used in this view of subjects:

<script>
  $(function() {
    $( ".field" ).change(function(e) {
      $( "#save" ).show();
    });

    $( "#save" ).click(function(e) {   
      e.preventDefault();
      var rows = $("#tablegroups tr").length - 3;
      var opc = $("#Name");
      var mat = $("#Name").val().trim();
      pru = opc.replace(opc, mat);
      //console.log("<" + mat+ ">");

      document.getElementById("frm_edit_subject").submit(); 
      //$( "frm_edit_subject" ).submit();
    });

    $( "#cancel" ).click(function(e) {
      e.preventDefault();
      $form = $('<form action="<?php echo base_url()?>index.php/gabby/materias" method="post"enctype="form-data" />'); 
      $form.appendTo("body").submit();
    });

    $( "#frm_edit_subject" ).validate({
      rules: {
        "Name": {required: true, maxlength: 50},
        "Scope": {required: true,},
        "Status": {maxlength: 1, required: true},               
      }
    });
  });
</script>

<?php
//print_r($materia);
echo '<form id="frm_edit_subject" action="'.base_url().'index.php/gabby/materias" method="post"enctype="form-data" />';
echo '<div class="gbyTable" style="width:480px;margin:15px"><table>';
echo '<tr><td>Campo</td><td>Valor</td></tr>';

if(isset($materia['Subject_ID']))
  echo '<tr><td>ID</td><td>'.(isset($materia['Subject_ID'])?$materia['Subject_ID']:"").'</td></tr>';
  echo '<tr><td>Nivel</td><td>'.$level['Name'].'<input type="hidden" name="Level_ID" value="'.$level['Level_ID'].'"><input type="hidden" name="indx" value="'.$indx.'"></td></tr>';
  echo '<tr><td>Nombre</td><td><input type="text" class="field" name="Name" id="Name" value="'.(isset($materia['Name'])?$materia['Name']:"").'"></td></tr>';
  echo '<tr><td>Ámbito</td><td>'.form_dropdown("Scope", array('' => 'Seleccione','S' => 'Materia','G' => 'Genérica','A' => 'Administrativo',),isset($materia['Scope'])?$materia['Scope']:"", 'class="field"').'</td></tr>';
  echo '<tr><td>Estatus</td><td>'.form_dropdown("Status", array('A' => 'Activo','I' => 'Inactivo'),isset($materia['Status'])?$materia['Status']:"", 'class="field"').'</td></tr>';
  echo '<tr><td style="background:#ddd" align="right" colspan="2"><button id="save" style="display:none">Guardar</button><button id="cancel">Cancelar</button></td></tr>';
  echo '</table></div>';

  if(isset($materia['Subject_ID']))
    echo '<input type="hidden" name="idu" value="'.$materia['Subject_ID'].'">';
  else
    echo '<input type="hidden" name="idi" value="">';
    echo "</form>";
?>
    
asked by Joe Castillo 04.08.2017 в 15:35
source

2 answers

0

I commented that I made some modifications to the code, I was going through a very difficult path, I converted the object to string by means of JSON, until I chose to throw everything I had done and eliminate the variables mat, opt, res. I used something simpler as it is:

$("#Name").val($("#Name").val().trim());

the code stayed the way:

<script>

$(function () {
    $(".field").change(function (e) {
        $("#save").show();
    });
    $("#save").click(function (e) {
        e.preventDefault();
        var rows = $("#tablegroups tr").length - 3;


        $("#Name").val($("#Name").val().trim());


        //console.log("<" + mat+ ">");

        document.getElementById("frm_edit_subject").submit();


        // $( "frm_edit_subject" ).submit();

    });
    $("#cancel").click(function (e) {
        e.preventDefault();
        $form = $('<form action="<?php echo base_url() ?>index.php/gabby/materias" method="post"enctype="form-data" />');
        $form.appendTo("body").submit();
    });
    $("#frm_edit_subject").validate({
        rules: {
            "Name": {required: true, maxlength: 50},
            "Scope": {required: true, },
            "Status": {maxlength: 1, required: true},
        }
    });
});

</script>

<?php
//print_r($materia);
echo '<form id="frm_edit_subject" action="' . base_url() . 
'index.php/gabby/materias" method="post"enctype="form-data" />';
echo '<div class="gbyTable" style="width:480px;margin:15px"><table>';
echo '<tr><td>Campo</td><td>Valor</td></tr>';
if (isset($materia['Subject_ID']))
echo '<tr><td>ID</td><td>' . (isset($materia['Subject_ID']) ? 
$materia['Subject_ID'] : "") . '</td></tr>';
echo '<tr><td>Nivel</td><td>' . $level['Name'] . '<input type="hidden" 
name="Level_ID" value="' . $level['Level_ID'] . '"><input type="hidden" 
name="indx" value="' . $indx . '"></td></tr>';
echo '<tr><td>Nombre</td><td><input type="text" class="field" name="Name" 
id="Name" value="' . (isset($materia['Name']) ? $materia['Name'] : "") . '">
</td></tr>';
echo '<tr><td>Ámbito</td><td>' . form_dropdown("Scope", array('' => 
'Seleccione', 'S' => 'Materia', 'G' => 'Genérica', 'A' => 
'Administrativo',), isset($materia['Scope']) ? $materia['Scope'] : "", 
'class="field"') . '</td></tr>';
echo '<tr><td>Estatus</td><td>' . form_dropdown("Status", array('A' => 
'Activo', 'I' => 'Inactivo'), isset($materia['Status']) ? $materia['Status'] 
: "", 'class="field"') . '</td></tr>';
echo '<tr><td style="background:#ddd" align="right" colspan="2"><button 
id="save" style="display:none">Guardar</button><button 
id="cancel">Cancelar</button></td></tr>';
echo '</table></div>';
if (isset($materia['Subject_ID']))
echo '<input type="hidden" name="idu" value="' . $materia['Subject_ID'] . 
'">';
else
echo '<input type="hidden" name="idi" value="">';
echo "</form>";
?>
    
answered by 04.08.2017 / 17:26
source
-1

I send you an example of simple jScript I hope it works for you.

var opc = "    Hola Mundo!    ";
alert("-" + opc +"-"  );
alert("-" + opc.trim() +"-"  );

var str = "Visit Microsoft!";
alert(str);
alert(str.replace("Microsoft", "W3Schools"));
    
answered by 04.08.2017 в 15:59