简体   繁体   English

我的 php 文件显示过滤后的响应,但 responseText 在运行 web 页面时显示所有数据库值

[英]My php file shows filtered response but responseText show all the database values when run through web page

When I run my search_form.php attached in action with form tag, it runs correctly and gives filtered database response in json form.当我运行带有表单标签的search_form.php时,它运行正确并以 json 形式提供过滤后的数据库响应。 Like this (which I want in my web page but I'm unable to get this):像这样(我想要在我的 web 页面中,但我无法得到这个):

[   
    {
        "id": "6",
        "section": "Science",
        "que": "What is DTG stands for?",
        "ans1": "It is dynamically Tuned Gyroscope designed to orient any of the two axis in space.",
        "ans2": "Dynamically Tuned Gyroscope. Answer to the point."   
    } 
]

My form tag and div tag is as follows in my mainWebPage.php file:我的mainWebPage.php文件中的表单标记和 div 标记如下:

<form id="search_bar" action="search_form.php" method="post" onsubmit="return showSearchResults()">  <!-- /* MARK-1 */ -->
    Search: <input type="text" name="term" /><br> 
    <input type="submit" value="Submit" class="btn btn-outline-success " style="width: 100%; margin-top: 5px;" /> 
</form> 
<div class="container">
    <div id="search_output" style="width: 80%;">Nothing Searched yet</div>
</div>

My script is as follows in my mainWebPage.php file:我的mainWebPage.php文件中的脚本如下:

function showSearchResults() {
    console.log('This is search result.');
    var obj = $('#search_bar').serializeJSON();
    var json_string = JSON.stringify(obj);
    alert("Json string: "+json_string);
    var url = "search_form.php";
    var xhttp1 = new XMLHttpRequest();
    console.log(xhttp1.responseText+" after onload of search");
    xhttp1.open("POST", url, true);
    xhttp1.setRequestHeader("content-type", "application/json");
    xhttp1.send(json_string);
    var search_output = document.getElementById('search_output');
    console.log(xhttp1.responseText+" here is ajaxhttp1 search_form");
    search_output.innerHTML = "Loading search results....";
    xhttp1.onreadystatechange = function () {
        search_output.innerHTML = "";
        console.log(xhttp1.responseText+" here is ajaxhttp2 search_form");
        if (xhttp1.readyState == 4 && xhttp1.status == 200) {
            console.log(xhttp1);
            var jcontent1 = JSON.parse(xhttp1.responseText);
            for (var i = 0; i < jcontent1.length; i++) {
                console.log(i+"---S");
                if(jcontent1[i].ans2 != ""){
                    search_output.innerHTML +=''+ '<ul style="background:#bbd5d5" onmouseover="this.style.background=\'#ffeee5\'"; onmouseout="this.style.background=\'#bbd5d5\';">Que. <a id="clickable_que'+i+'" onclick="showThisQue('+i+')">' +jcontent1[i].que + '<br> Ans. ' + jcontent1[i].ans1 +'<br> Ans. ' + jcontent1[i].ans2+'</a><span style="width: 400px;float:right;">'+jcontent1[i].section+'</span><br></ul>';
                }else{
                    search_output.innerHTML += ''+ '<ul style="background:#bbd5d5" onmouseover="this.style.background=\'#ffeee5\'"; onmouseout="this.style.background=\'#bbd5d5\';">Que. <a id="clickable_que'+i+'" onclick="showThisQue('+i+')">' +jcontent1[i].que + '<br> Ans. ' + jcontent1[i].ans1+' </a><span style="width: 400px;float:right;">'+jcontent1[i].section+'</span><br></ul>';
                }
            }
            console.log(jcontent1+" response parsed text search_form");
        }
    }
            
    //return false; /* MARK-2 */
}

But when I changed MARK-1 & Mark-2 to this:但是当我将 MARK-1 & Mark-2 更改为:

<form id="search_bar" action="search_form.php" method="post" onsubmit="return false">  <!-- /* MARK-1 CHANGED (made return output false here) */ -->
return false; /* MARK-2 CHANGED (made uncommented) */

And ran the mainWebPage.php and do the search, it gives my whole database result in my webpage like this:并运行mainWebPage.php并进行搜索,它在我的网页中给出了我的整个数据库结果,如下所示:

Que. What is star sensor accuracy?
Ans. Its bore-sight accuracy is 40-arcsec and cross section accuracy is 10-arcsec. Science Concept

Que. What is dynamic observer mode?
Ans. ES+DTG mode of the spacecraft is called as dynamic observer mode. It is chosen when primary sensors failed to work. Like, Star sensor.
Ans. Earth sensor and another sensor which gives yaw information combining these two sensors called the DOM.Science Concept

Que. This is a test q?
Ans. This test answer science

Que. What is DTG stands for?
Ans. It is dynamically Tuned Gyroscope designed to orient any of the two axis in space.
Ans. Dynamically Tuned Gyroscope. Answer to the point.Science

Que. What is the role of SLE team on JPL passes?
Ans. Science Concept

Que. What is RCP configuration?
Ans. science concept

Que. What is NPA?
Ans.
Ans. NPA is nothing but Non-Performing Assets. Its a loss for any financial institution.Banking

Que. What is Loop Stress in antenna?
Ans. Science

which I dont want.我不想要。 I wanted the searched response which I got earlier in my search_form.php file.我想要之前在我的search_form.php文件中得到的搜索响应。 My search_form.php file content is this:我的search_form.php文件内容是这样的:

<?php
$conn = mysqli_connect("localhost","root","","qnaforum") or die ("Error ".mysqli_error($conn));
$term = $_POST['term'];  
    
$sql = "SELECT * FROM qna WHERE que LIKE '%".$term."%'";
$r_query = mysqli_query($conn, $sql);
$myArray = array();
if(mysqli_num_rows($r_query) > 0){
    while ($row = mysqli_fetch_assoc($r_query)){ 
        $myArray[] = $row;
    } 
} else{
    $myArray['que'] = "No records matching your query were found.";
}
mysqli_close($conn);
header('Content-Type: application/json');
$json = json_encode($myArray);
echo $json;
?>

Please help and tell me what am I doing wrong?请帮助并告诉我我做错了什么?

You're trying to send the search value in the AJAX as JSON, but your PHP is not set up to read JSON.您正在尝试将 AJAX 中的搜索值作为 JSON 发送,但您的 PHP 未设置为读取 Z0A4ECD11C1D23A28Z0ECD1184ABBB8。 (And it makes no sense to use JSON anyway, just to send a single value). (无论如何使用 JSON 是没有意义的,只是为了发送一个值)。

Just send the form data in form-url-encoded format, as it would be if you submitted the form without AJAX.只需以 form-url-encoded 格式发送表单数据,就像您提交没有 AJAX 的表单一样。 jQuery can help you with this - eg jQuery 可以帮助您 - 例如

xhttp1.send($('#search_bar').serialize());

You also need to remove this line:您还需要删除此行:

xhttp1.setRequestHeader("content-type", "application/json");

PS As an aside, if you're already using jQuery, its $.ajax function is a lot easier to use than the ancient XMLlHttpRequest . PS 顺便说一句,如果您已经在使用 jQuery,它的$.ajax function 比古老的XMLlHttpRequest更容易使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 responseText返回我的php文件的顶部 - responseText is returning the top of my php file 为什么我在访问 Web 应用程序的页面时能够看到所有数据库的密码? - Why am I able to see all of the database's passwords when visiting a page of my web application? 在首页上显示过滤的内容,而不是显示所有内容 - Show the filtered content on first page instead of show all contents 通过javascript从我的网站读取特定事件(或所有响应)的HTTP responseText - To read the HTTP responseText of a particular event (or for all the responses) from my website through javascript 在没有PHP的HTML页面上显示MySQL数据库中的值 - Show values from MySQL Database on HTML page without PHP 当我尝试将值作为通过Ajax传递到php文件时,代码中的错误是什么? - What is the error in my code when i trying to pass values as Get through Ajax to a php file 如何在不刷新 web 页面的情况下运行 php 文件? - How to run a php file without refreshing the web page? 网页未显示新文件内容 - web page not shows new file content 我如何在我的页面上显示我的php响应而不用ajax刷新? - How do i get to show my php response on my page without refreshing with ajax? PHP文件不返回.responseText中的文本 - PHP file does not return text in .responseText
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM