0

I have a javascript that goes trough a html table with fixed columns and different rows and assign the values from the textboxes to variables where one is an array. For every row I want to insert the values into a mysql table. For test purpose I create a small table call “teste” with a field call “nome”. Whenever the checkForm is called from a button it inserts the a new register under the table “teste” but the field “nome” is empty. Can anyone help me on this? Regards, Joaquim

<script type="text/javascript">

function checkForm() 
{    
var tabela = document.getElementById("t02")
var c=tabela.rows.length;
var cnt=0;
var arr = new Array(12);
for(var i=2; i<=c-1; i++)
{
    cnt++;  
    soc=document.getElementById("sc"+cnt).innerHTML;
    arr[0]=document.getElementById("st"+cnt).value;
    arr[1]=document.getElementById("ot"+cnt).value;
    arr[2]=document.getElementById("nv"+cnt).value;
    arr[3]=document.getElementById("dz"+cnt).value;
    arr[4]=document.getElementById("ja"+cnt).value;
    arr[5]=document.getElementById("fv"+cnt).value;
    arr[6]=document.getElementById("mr"+cnt).value;
    arr[7]=document.getElementById("ab"+cnt).value;
    arr[8]=document.getElementById("mi"+cnt).value;
    arr[9]=document.getElementById("ju"+cnt).value;
    arr[10]=document.getElementById("jl"+cnt).value;
    arr[11]=document.getElementById("obs"+cnt).value;

    for(var a =0; a<=11; a++)
    {
        if(!arr[a]=="")
        {
            //alert(soc);
            var nom = new XMLHttpRequest();
                nom.open("POST","gravaMensalidade.php",true);
                nom.onreadystatechange = function() 
                {
                    if( this.readyState != 4) return;
                    if( this.status != 200) return alert("ERROR      
    "+this.status+" "+this.statusText);
                    alert(this.responseText);
                };
                nom.send("nome="+soc);
           }
        }        
    }
 }
 </script>

 <?php

 /* 
 * To change this license header, choose License Headers in Project 
  Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

 include 'ligabd.php';
 $query="INSERT INTO TESTE(nome) VALUES('".$_POST['nome']."')";
 $res=mysqli_query($l, $query);

 ?>
  • Unsolicited advice, that query is at dire risk of a little bobby tables. Some good answers in ["How can I prevent SQL-injection in PHP?"](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Don McCurdy Dec 10 '15 at 03:46
  • Also, we can't see the value of `nome` you're trying to send, but if it's really HTML, you can't just do `"nome=" + soc`. It needs to be escaped. For example, use `encodeURIComponent` or `var data = new FormData(); data.append('nome', soc); nom.send(data);` – Don McCurdy Dec 10 '15 at 03:58

1 Answers1

0

Printing your $_POST value could most likely determine your issue, as i'm assuming it isn't sent properly.

I think you need to add a header to your POST request, so it knows how to look for the POST data sent along. try adding:

nom.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

before you use nom.send

If this does not solve your issue, i suggest you print_r($_POST), and see what you're actually getting along.

Avenar
  • 341
  • 1
  • 8
  • I change the code for the one below and now it works fine! if (answer) { var nom = new XMLHttpRequest(); nom.open("GET","gravaMensalidade.php?socio="+soc+"&escalao="+escal+"&genero="+sexo+"&epoca="+epo+"&ano="+ano+"&mes="+mes+"&valor="+arr[a]+"&obs="+arr[11], true); nom.send(); } Thanks for the replys Joaquim – Joaquim Pinto Dec 10 '15 at 06:25