<input type="text" id="nombre"> <input type="button" id="boton" value="Pinchar"> <div id="resultado"></div> <script> $(function () { $('#boton').click(function () { var datodelusuario = $('#nombre').val(); $.post("datos.php", {nombre: datodelusuario}, function (datos, status) { if (status == "success") { console.log(datos); var xmlDoc = $.parseXML(datos); console.log(xmlDoc); } }); }); }) </script>
<?php $nombre = filter_input(INPUT_POST, "nombre"); $server = "localhost"; $user = "root"; $password = ""; $db = "sakila"; try { $conn = new PDO("mysql:host=$server;dbname=$db", $user, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"]); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql="select * from actor where first_name like '%$nombre%'"; $res=$conn->query($sql); $actores=$res->fetchAll(); echo "<actores>"; foreach($actores as $actor){ echo "<actor><first_name>".$actor['first_name']. "</first_name><last_name>".$actor['last_name']. "</last_name></actor>"; } echo "</actores>"; } catch (Exception $ex) { echo $ex->getMessage(); }