我正在尝试从文本框获取数据并在数据库中搜索并在文本框下方显示结果

时间:2018-10-04 04:08:46

标签: javascript php html ajax

<!----- 
getCedula.php 
Here I get the data from the script and start the search and send the result
---------------------------- - -->
<?php
require("conexion.php");

$cedula=$_REQUEST["cedula"];
//$cedula="0922615646";
echo $cedula;
$query="SELECT * FROM clientes WHERE cedula=".$cedula."";
$result = mysqli_query($con,$query);
while (($fila = mysqli_fetch_array($result)) != NULL) {
    echo ''.$fila["cedula"].' '.$fila["nombres"].'';
}
// Liberar resultados
mysqli_free_result($result);

// Cerrar la conexión
mysqli_close($con);
?>

function gocedula(Text){  

                        var form_data = {
                                is_ajax: 1,
                                cedula: val(Text)
                        };

                        $.ajax({
                                type: "POST",
                                url: "getCedula.php",
                                data: form_data,
                                success: function(response)
                                {
                                    $('.actualizar-formulario').html(response).fadeIn();
                                }
                        });


             
}
            </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!--
In the textbox with the id coger-cedula I put the numbers that i want to search and send them to getCedula.php
Somehow it does nothing
-->
<form>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Cédula</label>
<div class="col-sm-10">
<input id="coger-cedula" type="text" class="form-control rounded" name="cedula" placeholder="Cédula" maxlength="10"
onchange="gocedula($(this).val());" onkeyup="gocedula($(this).val());" onpaste="gocedula($(this).val());" oninput="gocedula($(this).val());">
</div>
</div>
<div class="actualizar-formulario">
</div>
</form>

javascript无法从文本框中获取数据。如果发出警告,我可以看到数据通过了该函数,但没有发送到ajax“ form_data”中。我尝试添加它,因为它以带有select标签的形式工作。 假定每次有人在文本框中键入内容时,javascript函数都必须将其发送到“ getCedula.php”并在数据库中进行查找,因此,如果发现某些内容,它将显示结果。

1 个答案:

答案 0 :(得分:0)

您在js代码中有错误!为什么使用val(Text)??? JavaScript中没有这样的功能。 也许您混入了$('.class_name').val()??? 有一个可行的选择。

<!DOCTYPE html>
<html>
<head>
	<title>Ansver!</title>
</head>
<body>
	<form>
		<div class="form-group row">
			<label class="col-sm-2 col-form-label">Form</label>
			<div class="col-sm-10">
				<input id="coger-cedula" type="text" class="form-control rounded" name="cedula" placeholder="text" maxlength="10" onchange="gocedula($(this).val());" onkeyup="gocedula($(this).val());" onpaste="gocedula($(this).val());" oninput="gocedula($(this).val());">
			</div>
		</div>
		<div class="actualizar-formulario">
		</div>
	</form>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<script>
	    function gocedula(text) {	
			var form_data = {
				is_ajax: 1,
				cedula: text
			};

			$.ajax({
				type: "POST",
				url: "/ajax.php",
				data: form_data,
				success: function(response) {
					$('.actualizar-formulario').html(response).fadeIn();
				}
			});
		}
	</script>
</body>
</html>

ajax.php

<?php
$cedula=$_REQUEST["cedula"];
echo $cedula;
?>