Foros aprenderaprogramar.com
Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: RAM47 en 01 de Diciembre 2016, 21:59
-
Tengo dos páginas PHP y estoy intentando pasar una variable entre ellas. Si lo hago con formulario no tengo problemas.
pagina1.php
<!DOCTYPE HTML>
<html lang="">
<head>
<meta charset="UTF-8">
<title>página 1</title>
</head>
<body>
<div class="container">
<!--inicio bucle for con variable de sesión-->
<?php for($a = 0; $a < 3; $a++): ?>
<!--botones y variables-->
<form action="pagina2.php" method="post">
<input type="hidden" name="var" value="<?php echo $a; ?>">
<input type="submit" name="submit" value="Index <?php echo $a; ?>">
</form>
<!--cierro bucle for-->
<?php endfor; ?>
</div>
</body>
</html>
pagina2.php
<?php
header('Content-Type: text/html; charset=utf-8');
//tomo variable de pagina1.php
$variable = $_POST['var'];
echo $variable;
?>
pero si intento hacer lo mismo con ajax, no logro pasar la variable
pagina1.php
<!DOCTYPE HTML>
<html lang="">
<head>
<meta charset="UTF-8">
<title>página 1</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('form').submit(function(e) {
e.preventDefault();
$.ajax({
url: "pagina2.php",
type: "POST",
data: $('form').serialize(),
dataType: "HTML",
success: function( data ) {
$('body').append(data);
},
error: function(jqXHR, data ) {
alert ('Ajax request Failed.');
}
});
});
});
</script>
</head>
<body>
<div class="container">
<!--inicio bucle for con variable de sesión-->
<?php for($a = 0; $a < 3; $a++): ?>
<!--botones y variables-->
<form action="variable2.php" method="post">
<input type="hidden" name="var" value="<?php echo $a; ?>">
<input type="submit" name="submit" value="Index <?php echo $a; ?>">
</form>
<!--cierro bucle for-->
<?php endfor; ?>
</div>
</body>
</html>
pagina2.php
<?php
header('Content-Type: text/html; charset=utf-8');
//tomo variable de pagina1.php
$variable = $_POST['var'];
echo $variable;
?>
cómo lo puedo hacer?
-
Buenas RAM47 mírate los ejemplos de este curso, quizás te sirvan de ayuda: https://www.aprenderaprogramar.com/index.php?option=com_content&view=category&id=83&Itemid=212
Salu2