简体   繁体   中英

Ajax doesn't send variables to PHP by POST

I need to send only 1 variable to my .php file using AJAX (using method POST) and show it (with php). Here is my HTML code:

<script type = "text/javascript">
            var XMLHttpRequestObject = false; //LO INICIALIZAMOS A FALSO PARA DESPUES COMPROBAR QUE ESTA CREADO CORRECTAMENTE
            if (window.XMLHttpRequest) {
                XMLHttpRequestObject = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
                XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); //PARA INTERNET EXPLORER

            }

            function irweb(idDiv) {

                if (XMLHttpRequestObject) {
                    var objeto = document.getElementById(idDiv);
                    nom1="holaaaaaaa";
                  //  var nom1 = document.getElementById('nombre').value;                    
                //var com1 = document.getElementById('comentarios').value;

                    XMLHttpRequestObject.open("POST", "p2.php?");
                    XMLHttpRequestObject.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                    XMLHttpRequestObject.onreadystatechange = function() {
                        if (XMLHttpRequestObject.readyState == 4 &&
                                XMLHttpRequestObject.status == 200) {
                            objeto.innerHTML = XMLHttpRequestObject.responseText;
                        }
                    }
                    XMLHttpRequestObject.send("n="+nom1);
            }
        }
        </script>
    </head>
    <body>
        <form  method="post" id="formulario">

            <input type="submit" value="Enviar" onclick ="irweb('contenedor')" id="enviar"/>
        </form>
        <div id="contenedor" style="background-color:#99FF66;text-align:center;"></div>        
    </body>

Here is my PHP code:

<?php
$cabe = <<< 'EOD'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<head>
</head>
<html>
<body>  
<p>hola</p>
</body>
</html>
EOD;
$pasado1=$_POST["n"];
$pasado2=$_POST["c"];
echo "El parametro pasado es -->".$pasado1;
echo "El parametro pasado es -->".$pasado2;
?>

I called some alerts in the JS code, to chek if I get the values correctly, and it works, but when I call the php file, nothing happens

When you click on the submit button, you:

  • Run the JavaScript
  • Submit the form

You don't see a result from the JS because the form submits (reloading the page) before the readystate has reached 4.

Stop the form submitting if the JS runs:

onclick="irweb('contenedor'); return false;"

… and fix your server side script so it can handle the POST data if the JS fails for any reason and the form does submit.

Have you tried using jquery? If not, try it for easy coding and maintenance http://api.jquery.com/jQuery.post/

The ajax post code is simply as below :

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM