简体   繁体   English

(可能)POST方法的未知问题

[英]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:这里有一些提示可以帮助您确定问题的根源:

  1. Is your browser making the HTTP requests that you expect?您的浏览器是否发出您期望的 HTTP 请求? By glancing at your Javascript, you expect both a POST and a GET.通过查看您的 Javascript,您会期望 POST 和 GET。 If you are using Google Chrome, check Dev tools - Network如果您使用的是 Google Chrome,请查看 开发工具 - 网络
  2. What HTTP requests is the server receiving?服务器收到什么 HTTP 请求? You can debug your PHP code by following the official debugging documentation and reading its comments.您可以按照官方调试文档并阅读其注释来调试您的 PHP 代码。 Especially:尤其:
<?php print_r($_POST); ?>
  1. Is the server answering the data you are expecting?服务器是否在回答您期望的数据? Check the HTTP verb (GET or POST) used to get the number (hint: line 8 of your code).检查用于获取数字的 HTTP 动词(GET 或 POST)(提示:代码的第 8 行)。 Now check on which HTTP call (GET or POST) you put the Javascript callback to handle the server response.现在检查您放置 Javascript 回调以处理服务器响应的 HTTP 调用(GET 或 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.

相关问题 JavaScript中可能存在明显的问题 - Probably obvious problem in JavaScript post方法的问题(使用fetch和express) - A problem with a post method (using fetch and express) 快速服务器 POST 方法和登录时获取未定义值的问题 - Problem with express server POST method and fetch, undefined values on a log in nodemailer 从未被读取且 method='POST' 从未发送,路由问题 - nodemailer is never read and method='POST' never sent, routing problem Ajax post 方法有问题,在 asp.net 核心项目中 - Ajax post method works with problem, in asp.net core project 从.JSON访问值的问题(可能是路径问题) - Problem with accessing value from .JSON (path issue probably) Mediawiki 的 api 中的数据问题(可能是 js 语法) - Problem with data in Mediawiki's api (probably in js syntax) 错误:这可能不是 npm 的问题。 这可能是上面的附加日志输出 - error: this is probably not a problem with npm. This is likely additonal logging output above HTTP POST无法正常工作(可能是一些无法确定的愚蠢错误) - HTTP POST not working (probably some silly mistake unable to determine) NodeJS MonogoDB Postman Post Request 抛出错误可能是 objectID 错误 - NodeJS MonogoDB Postman Post Request throwing an Error probably the objectID Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM