PHP not receiving form data - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Tuesday, March 22, 2022

PHP not receiving form data

I'm trying to use a form with an ID (since i'll be having various forms that'll go to various tables inside the DB) to insert data to a database using php but it just won't work.

<form id="ELGEE">   
<h2>Modelo</h2><input type="text" name="mod"><br><br>
        <h2>Codigo Error</h2><input type="text" name="cod"><br><br>
            <h2>Descripcion</h2><input type="text" name="des"><br><br>
            <input type="submit" name="boton" value="Insertar" onclick="IngresarLG()">
</form>            

That's the form i'm using

function IngresarLG(){
                var mi_form= document.getElementById("ELGEE").value;
                if (mi_form.checkValidity()=true){
                                    
                    var datos_form=new FormData(mi_form);
                               
                    if (window.XMLHttpRequest) {
                        
                        xmlhttp = new XMLHttpRequest();
                    } 
                    else { 
                        
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
    
                    xmlhttp.open("POST" , "insertarlg.php", true );
                    xmlhttp.send(datos_form);
                        
                }
                else{
                    alert("Datos incorrect");
                }
}

This the function i'm using that validates the form

<?php
    $modelo = $_POST['mod'];                         
    $codigo = $_POST['cod'];
    $descripcion = $_POST['des'];
    $con = mysqli_connect('localhost','root');                     

    if (!$con) {
        die('No se pudo conectar: ' . mysqli_error($con));                                                          
    }
    mysqli_select_db($con,'airec');                
    $sql="insert into lg(modelo,codigo,descripcion)";
    $sql=$sql.  " values('".$modelo."','".$codigo."','".$descripcion."')";   
    $result = mysqli_query($con,$sql);                     
    mysqli_close($con);
?>

and this is the php code that it's using.

i've been stuck here for a while and have no idea why it won't send the form data to the db. i did a similar thing a while back and that works fine but this is just making me go crazy.



source https://stackoverflow.com/questions/71562258/php-not-receiving-form-data

No comments:

Post a Comment