Guest User

Untitled

a guest
Nov 29th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. xmlhttp=new XMLHttpRequest();
  2.  
  3. <form id="formulario" action="" method="POST">
  4. <div class="form-group">
  5. <label for="idnombre">Nombre:</label>
  6. <input type="text" class="form-control" id="idnombre" name="namenombre" placeholder="Ingresar Nombre">
  7. </div>
  8.  
  9. <div class="form-group">
  10. <label for="idapellido">Apellido:</label>
  11. <input type="text" class="form-control" id="idapellido" name="nameapellido" placeholder="Ingresar Apellido">
  12. </div>
  13.  
  14. <div class="form-group">
  15. <label for="idedad">Edad:</label>
  16. <input type="text" class="form-control" id="idedad" name="nameedad" placeholder="Ingresar Edad">
  17. </div>
  18.  
  19. <button type="submit" id="button" class="btn btn-default">Registrar</button>
  20. </form>
  21.  
  22. <?php
  23. $servername = "localhost";
  24. $username = "username";
  25. $password = "";
  26. $dbname = "demo";
  27.  
  28. // Create connection
  29. $conn = new mysqli($servername, $username, $password, $dbname);
  30. // Check connection
  31. if ($conn->connect_error) {
  32. die("Connection failed: " . $conn->connect_error);
  33. }
  34.  
  35. $nombre = $_POST['namenombre'];
  36. $apellido = $_POST['nameapellido'];
  37. $edad = $_POST['nameedad'];
  38.  
  39.  
  40. $sql = "INSERT INTO persona (nombre, apellido, edad) VALUES ('$nombre', '$apellido', '$edad')";
  41.  
  42. if ($conn->query($sql) === TRUE) {
  43. echo "New record created successfully";
  44. } else {
  45. echo "Error: " . $sql . "<br>" . $conn->error;
  46. }
  47.  
  48. $conn->close();
  49.  
  50. ?>
  51.  
  52. <div class="alert alert-success">
  53. <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
  54. <strong>Success!</strong> This alert box could indicate a successful or positive action.
  55. </div>
  56.  
  57. <form name="miformulario" id="myfor">
  58. <input type="text" name="elemento1">
  59. <input type="text" name="elemento2">
  60. <input type="text" name="elemento3">
  61. <select name="options">
  62. <option value="1">choise 1</option>
  63. <option value="2">choise 2</option>
  64. <option value="3">choise 3</option>
  65. </select>
  66. </form>
  67.  
  68. <script type="text/javascript">
  69. $('#myform').on('submit', function(e)) {
  70. e.preventDefault(); // Evitas que haga el submit por default
  71.  
  72. var datosFormulario = $(this).serialize();
  73.  
  74. $.ajax({
  75. type: "POST",
  76. url: "insertar.php",
  77. data: datosFormulario,
  78. success: function(data) {
  79. }
  80. });
  81. });
  82. </script>
  83.  
  84. Y desde el php utilizas la función parse_str para convertir los datos a un array que puedas manejar.
  85.  
  86. <?php
  87. $params = array();
  88. parse_str($_GET, $params);
  89.  
  90. echo $params['elemento1'];
  91. echo $params['elemento2'];
  92.  
  93. ?>
  94.  
  95. function agregaRegistro(){
  96. var url = 'insertar.php';
  97. $.ajax({
  98. type:'POST',
  99. url:url,
  100. data:$('#formpost').serialize(),
  101. success: function(registro){
  102. $('#formulario')[0].reset();
  103. $("#cargando").html(data);
  104. }
  105. });
  106. return false;
  107. }
  108.  
  109. <form id="formulario" action="" onsubmit="return agregaRegistro();" method="POST">
  110. <div class="form-group">
  111. <label for="idnombre">Nombre:</label>
  112. <input
  113. type="text"
  114. class="form-control"
  115. id="idnombre"
  116. name="namenombre"
  117. placeholder="Ingresar Nombre"
  118. >
  119. </div>
  120. <div class="form-group">
  121. <label for="idapellido">Apellido:</label>
  122. <input
  123. type="text"
  124. class="form-control"
  125. id="idapellido"
  126. name="nameapellido"
  127. placeholder="Ingresar Apellido"
  128. >
  129. </div>
  130. <div class="form-group">
  131. <label for="idedad">Edad:</label>
  132. <input
  133. type="text"
  134. class="form-control"
  135. id="idedad"
  136. name="nameedad"
  137. placeholder="Ingresar Edad"
  138. >
  139. </div>
  140. <button type="submit" id="button" class="btn btn-default">Registrar</button>
  141. </form>
  142.  
  143. $.ajax({
  144. url:"/agregarUsuario.php", //direccion de tu php o java(Utiliza servlets)
  145. type:'POST', // metodo POST es equivalente a <form method="POST" >
  146. data:{ParametroIDombre:$("#namenombre").val()},//parametros que resiviras en el php en metodo post $_POST["ParametroIDombre"] <-- obtienes de acuerdo a como lo declaraste en el data no con el name del form
  147. success: function( resp ) {
  148. alert(resp);
  149. //por medios de el atributo id="" puedes pintar información en tu html
  150. //o puedes crear otras funciones o tomar decisiones cuando la respuesta fue OK
  151.  
  152. },
  153. error:function(error){
  154. console.error(error);
  155. }
  156. });
  157.  
  158. }
Add Comment
Please, Sign In to add comment