[英]Unknown problem with (probably) POST method
This task is quite simple but I just can't seem to get it to work.这个任务很简单,但我似乎无法让它工作。 What the outcome should be is explained in the picture below.
下图解释了结果应该是什么。 I guess that there is some kind of a problem with POST in php, but I'm not sure.
我想 php 中的 POST 存在某种问题,但我不确定。 Do you see what's the problem that's causing this not to work?
您是否看到导致此操作不起作用的问题是什么?
Problem: No results are shown when there should be.问题:应该有结果时没有显示结果。
<?php
$conn = mysqli_connect("localhost", "root", "", "podaci");
if($conn === false){
die("Konekcija nije uspešna. " . mysqli_connect_error());
}
$number = $_POST['number'];
$result_array = array();
/* SQL query to get results from database */
$sql = "SELECT brojKartice, imeVlasnika, prezimeVlasnika, adresaVlasnika,
ostvareniBodovi, ostvareniPopust FROM podatak WHERE brojKartice = '".$number."' ";
$result = $conn->query($sql);
/* If there are results from database push to result array */
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
array_push($result_array, $row);
}
}
/* send a JSON encded array to client */
echo json_encode($result_array);
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<meta charset="UTF-8">
</head>
<body>
<div class = "container" >
<strong>Unesite broj kartice: </strong><input id="number" name="number" required/>
<input type="button" id="getusers" value="Provera"/> <br><br>
<div id="records"></div>
</div>
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(function() {
$('#getusers').click(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'provera.php',
data: {
number: $('#number').val()
}
});
});
});
$(function(){
$("#getusers").on('click', function(){
$.ajax({
method: "GET",
url: "provera.php",
}).done(function( data ) {
var result= $.parseJSON(data);
var string='<table width="100%"><tr><th>#</th><th>Korisnik</th><th>Adresa</th><th>Bodovi</th><th>Popust</th><tr>';
/* from result create a string of data and append to the div */
$.each( result, function( key, value ) {
string += "<tr> <td>"+value['brojKartice'] + "</td><td>"+value['imeVlasnika']+' '+value['prezimeVlasnika']
+ "</td><td>"+value['adresaVlasnika']+ "</td><td>"+value['ostvareniBodovi']+ "</td><td>"
+value['ostvareniPopust']+"</td> </tr>";
});
string += '</table>';
$("#records").html(string);
});
});
});
</script>
</body>
</html>
CREATE DATABASE podaci;
CREATE TABLE podatak (
brojKartice VARCHAR(10) NOT NULL,
imeVlasnika VARCHAR(20) NOT NULL,
prezimeVlasnika VARCHAR(30) NOT NULL,
adresaVlasnika VARCHAR(50),
ostvareniBodovi VARCHAR(10) NOT NULL,
ostvareniPopust VARCHAR(10) NOT NULL,
rokVazenja DATE NOT NULL
);
INSERT INTO podatak VALUES
('0123456','Đorđe','Anđelković',NULL,'15','150','2021-1-9'),
('6543210','Snežana','Bojović',NULL,'20','200','2021-5-3'),
('9876543','Goran','Stojadinović',NULL,'10','100','2021-9-7'),
('3456789','Bojana','Marković',NULL,'25','250','2021-12-15');
Welcome.欢迎。 Here are a few pointers to help you identify the source of the issue:
这里有一些提示可以帮助您确定问题的根源:
<?php print_r($_POST); ?>
Answer: your PHP code reads the number from the POST request, but your Javascript code only supports responses from the GET request.答:您的 PHP 代码从 POST 请求中读取数字,但您的 Javascript 代码仅支持来自 GET 请求的响应。
Simplify your Javascript by moving the callback (the function
block passed as argument of the .done()
method) from the GET request to the POST request.通过将回调(作为
.done()
方法的参数传递的function
块)从 GET 请求移动到 POST 请求来简化您的 Javascript。 Then delete the Javascript code dealing with the GET request.然后删除处理GET请求的Javascript代码。
Alternatively, replace $_POST with $_GET and remove the POST call.或者,将 $_POST 替换为 $_GET 并删除 POST 调用。
First of all, you have two event handler for #getusers button.首先,您有两个用于#getusers按钮的事件处理程序。 This doesn't make sense in the first place.
这首先没有意义。 Whenever you assign multiple event handler on a html element only the last one will trigger.
每当您在 html 元素上分配多个事件处理程序时,只有最后一个会触发。 In this case, it's only the ajax call with the
GET
method.在这种情况下,它只是使用
GET
方法的 ajax 调用。 And on your server side (php), this variable $number = $_POST['number'];
在你的服务器端(php),这个变量
$number = $_POST['number'];
will not have a value because the method was GET
.不会有值,因为方法是
GET
。
So to solve this you have two options I can think of:所以要解决这个问题,你有两个我能想到的选择:
#1. #1。 Move the first ajax call that you have where you have the
POST
request and then place it where you have the ajax call with GET
method.将您拥有的第一个 ajax 调用移动到您有
POST
请求的位置,然后将其放置在您使用GET
方法的 ajax 调用的位置。 In short, replace GET
with POST
event.简而言之,将
GET
替换为POST
事件。
#2. #2。 Change your php code to use
$_GET
and update your ajax call for GET
to adhere with the PHP change.将您的 php 代码更改为使用
$_GET
并更新您的 ajax 调用以使GET
遵守 PHP 更改。
You can remove either of the event handler of your button and just bind only one.您可以删除按钮的任一事件处理程序,只绑定一个。
Here is for a quick reference on javascript event handler and addEventListeners:https://gist.github.com/belmer/5e433aabbab24086af5c12ce0fb7323c以下是有关 javascript 事件处理程序和 addEventListeners 的快速参考:https://gist.github.com/belmer/5e4132ceaabb0cafab2
Best regards,此致,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.