使用变量执行php文件的Javascript

Javascript executing php file with variables

本文关键字:文件 Javascript php 执行 变量      更新时间:2023-09-26

我有一个javascript,它遍历具有固定列和不同行的html表,并将文本框中的值分配给变量,其中一个是数组。对于每一行,我都想将值插入到mysql表中。出于测试目的,我创建了一个小表调用"teste"和一个字段调用"nome"。每当从按钮调用checkForm时,它都会在表"teste"下插入一个新寄存器,但字段"nome"为空。有人能帮我吗?当做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);
 ?>

打印$_POST值很可能会决定您的问题,因为我假设它没有正确发送。

我认为您需要在POST请求中添加一个标头,这样它就知道如何查找发送的POST数据。尝试添加:

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

在使用nom.send 之前

如果这还不能解决你的问题,我建议你打印_r($_POST),看看你到底过得怎么样。