Pass hidden values from a form to ajax

-1

I'm trying to pass the values of several input to a function in ajax, but it does not pick them up.

<input type="hidden" name="rooms" value="<?php echo $room ?>">
<input type="hidden" name="destination" value="<?php echo $id_destination ?>">
<input type="hidden" name="hotel" value="<?php echo $id_hotel ?>">
<input type="hidden" name="codigo" value="<?php echo $id_codigo ?>">
function enviarDatosEmpleado(){
    //recogemos los valores de los inputs
    email=document.cliente.email.value;
    idrooms=document.cliente.rooms.val;
    iddestination=document.cliente.destination.val;
    idhotel=document.cliente.hotel.val;
    idcodigo=document.cliente.codigo.val;
}

And if I change the hidden in the input per text, it does.

    
asked by Jesus 06.12.2016 в 03:47
source

2 answers

0

On the one hand there is a syntax error in how you retrieve the values of the hidden ones. You are using ".val" instead of ".value". On the other, to rule out other problems, I recommend you reference elements with ids / classes instead of names.

<input type="hidden" name="rooms" id="rooms" value="<?php echo $room ?>">
<input type="hidden" name="destination" id="destination" value="<?php echo $id_destination ?>">
<input type="hidden" name="hotel" id="hotel" value="<?php echo $id_hotel ?>">
<input type="hidden" name="codigo" id="codigo" value="<?php echo $id_codigo ?>">

function enviarDatosEmpleado(){
    //recogemos los valores de los inputs
    email=document.cliente.email.value;
    idrooms=document.getElementById("rooms").value;
    iddestination=document.getElementById("destination").value;
    idhotel=document.getElementById("hotel").value;
    idcodigo=document.getElementById("codigo").value;
}
    
answered by 15.12.2016 / 15:02
source
0
  

Archivo.Html

<form name="tuform" onsubmit="insert_empleado(); return false" >
<input type="hidden" name="rooms" id="rooms" value="<?php echo $room ?>">
<input type="hidden" name="destination" id="destination" value="<?php echo $id_destination ?>">
 </form>
  

Ajax.js

function objetoAjax(){
            var xmlhttp=false;
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {

            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
              xmlhttp = new XMLHttpRequest();
            }
            return xmlhttp;
        }

        function insert_empleado(){
              rooms=document.tuform.rooms.value;
              destination=document.tuform.destination.value;
              ajax=objetoAjax();
              ajax.open("POST", "tuarchivo.php",true);
              ajax.onreadystatechange=function() {
                    if (ajax.readyState==4) {
                        swal("Dato Agregado", "Correcto", "success")
                    }
             }
                ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                ajax.send("rooms="+rooms+"&destination="+destination)
        }
  

tuarchivo.php

<?php 
include("../php/conexion.php");
//posts
$rooms=$_POST['rooms'];
$destination=$_POST['destination'];
$orden=mysqli_query($conexion,"update tutabla set rooms="$rooms"where id='$id'");
?>
    
answered by 15.12.2016 в 15:18