Quiero devolver datos de la base de datos usando AJAX aquí, el código AJAX para recuperar los datos, pero no puedo darle saparataly.
$(document).ready(function(){
$('#bathroom-select').change(function(){
var bathroom_option1 =$(this).val();
console.log(bathroom_option1);
$.ajax({
type:'POST',
data:({bathroom_option1}),
success:function(data){
price1=parseInt(data);
console.log(price1);
var rows;
$.each(data, function (key, name) { //this will not work
console.log(item[i]);
});
}
});
});
});
Aquí la imagen de la base de datos donde se almacenan los datos.
Alguien por favor explíqueme que soy nuevo en stackoverflow, así que si hay algún error, lo siento. Y gracias por responderme las respuestas.
Este es el procesamiento del sitio del servidor usando php
$con=mysqli_connect("localhost","root","","price");
if (isset($_POST['bathroom_option1'])) {
$query=mysqli_query($con,"select * from bathroom where number_of_bathrooms ='$_POST[bathroom_option1]'");
while ($row = mysqli_fetch_array($query)) {
echo json_encode($row['price'],JSON_NUMERIC_CHECK);
echo json_encode($row['hours'],JSON_NUMERIC_CHECK);
echo json_encode($row['minutes'],JSON_NUMERIC_CHECK);
}
}
2 respuestas
Ok, prueba esto tal vez:
1 / Tu llamada ajax:
$(document).ready(function(){
$('#bathroom-select').change(function(){
var bathroom_option1 =$(this).val();
console.log(bathroom_option1);
$.ajax({
type:'POST', // you send data with POST method
data:{ "bo1" : bathroom_option1}, //the data you send
dataType : "JSON", // what you will get as anwser data type
url : yourphp.php, // your .php where you send your data
success:function(response){
var data = response.data; // your anwser array with one row = one item with price, hours and minutes
// See why in my PHP, but here you can get the message and the type you send with "response.message" and "response.type", can be usefull if you want to do something different according to what happen in your .php
$.each(data, function (key, name) {
console.log(this); // one row
});
}
});
});
});
2 / Tu .php:
$result = array();
$con=mysqli_connect("localhost","root","","price");
if (isset($_POST['bo1'])) { // you get your data with post method here, with the key you used in your call, here 'bo1'
$query=mysqli_query($con,"select * from bathroom where number_of_bathrooms ='$_POST['bo1']'");
while ($row = mysqli_fetch_array($query)) {
// You add each row in your $result['data'] array
$result['data'][] = array(
"price" => $row['price'],
"hours" => $row['hours'],
"minutes" => $row['minutes']
);
} // end while
} // end if
$result['message'] = "All is ok !";
$result['type'] = "success";
echo json_encode($result);
// You can send back what you want as message or type for example, if you have some test to do in your php and it fails send back $result['type'] = "warning" for example
¿Es esto lo que estás buscando?
Esto probablemente porque el elemento no está definido, intente esto
$.each(data, function () {
console.log($(this));
});
Preguntas relacionadas
Nuevas preguntas
javascript
Para preguntas sobre la programación en ECMAScript (JavaScript / JS) y sus diversos dialectos / implementaciones (excepto ActionScript). Incluya todas las etiquetas relevantes en su pregunta; por ejemplo, [node.js], [jquery], [json], etc.