javascript input

Asked

Viewed 897 times

-1

I have a table with the INCLUDE button containing the following code:

<td align="center"><input type="button" class="add" onclick="add_row(<?php echo($id_usuario); ?>, 0);" value="INCLUIR"></td>

The.js table file contains func add_row() with the following code:

function add_row(pid_usuario,pdia_da_semana)
{
    var inicio=document.getElementById("inicio").value; 
    var fim=document.getElementById("fim").value;   
    window.alert('ENTROU'); 

    //INCLUIR_REGISTRO(pid_usuario, pdia_da_semana, inicio, fim);

    var table=document.getElementById("data_table");
    var table_len=(table.rows.length)-1;
    var row = table.insertRow(table_len).outerHTML="<tr id='row"+table_len+"'><td id='inicio_row"+table_len+"'>"+inicio+"</td><td id='fim_row"+table_len+"'>"+fim+"</td><td align='center'> <input type='button' value='EXCLUIR' class='delete' onclick='delete_row("+table_len+")'></td></tr>";

    document.getElementById("inicio").value=""; 
    document.getElementById("fim").value="";

}

Basically what I do with the include button is add a new row to table.
So I thought.. Since I’m adding the line why not include the record at this point.
Here comes the lack of experience.. I have no idea how to do the job INCLUIR_REGISTRO().

Could one of my more experienced colleagues help me?

  • Good afternoon ISAC.. In the add_row function I include a line in the <TABLE> object. If you notice, when Gero a <tr> and <td> they have different IDS that was passed by the TABLE_LEN variable. So I generated this line and now I want to GENERATE INSERT in the appropriate table.

  • Correct. And what do you mean by record? The function INCLUIR_REGISTRO() What exactly are you supposed to do?

  • Ola. a função INCLUIR_REGISTRO(pid_usuario, pdia_da_semana, pinicio, pfim) seria algo do tipo: Insert into tabela (id_usuario, dia_da_semana, inicio, fim) values (pid_usuario, pdia_da_semana,pinicio, pfim). execsql;

  • For this it is necessary to use Ajax and send the data to the php page that will take them insert into the corresponding table and return the result of the operation

1 answer

0

Good morning to all..

I leave here the solution to the proposed problem.

In the table.js file another function was created that was placed below:

function incluir_registro(id_usuario,dia_da_semana,inicio, fim) {
    dia_da_semana + ' inicio..: '+ inicio + '  fim..: '+ fim);
    $.ajax({
        type: 'post',
        url: 'm_h_02_i_horario.php',
        data: {
            inserir_registro: 'inserir_registro',
            pid_usuario :id_usuario,
            pdia_da_semana: dia_da_semana,
            pinicio: inicio, 
            pfim: fim 
        }, success: function(response) {
            if(response=="success") {
                window.alert('SUCESSO ' + id_usuario);   
            }
        }
    });
}

So the logic is as follows:

The button in the html part calls the add_row() function with the id_user and weekday_day parameters.
The function add_row(id_user,dia_da_week) in turn calls the function insert record(id_user,dia_da_week,start, end).

My difficulty was in making the inclu_record function, because I am very weak in ajax script. I was not understanding how the $.ajax worked.

From what I can understand of this function it works as follows:

  1. type : post -> generates the post event
  2. url : 'm_h_02_i_horario.php' -> is the php page that actually inserts the record
  3. data:{inserir_registro:'inserir_registro',pid_usuario:id_usuario, pdia_da_semana: dia_da_semana , pinicio : inicio, pfim : fim } -> is where the parameters are passed to the url page (m_h_02_i_horario.php)
  4. success:function(response){if(response=="success"){window.alert('SUCESSO ' + id_usuario)}} -> if . php is executed correctly executes what is in this function

I hope these lines can be useful to the community.

Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.