简体   繁体   English

我想将数据发送到 php 文件进行处理并使用 ajax 接收响应而不刷新页面

[英]I want to send data to a php file for processing and receive a response using ajax without page refresh

I want to read and print out tag attribute names from an xml file and print them out on the screen using ajax.The code I wrote in the file_controller.php file works fine in printing the attribute names but the ajax is not fetching the data printed out which is saved in the data array.Please guys I need help in solving this problem.The database files, xml files are all fine.its just the ajax call that is the problem.Thanks in advance我想从 xml 文件中读取和打印标签属性名称,然后使用 ajax 在屏幕上打印出来。 out 保存在数据数组中。请伙计们我需要帮助解决这个问题。数据库文件,xml 文件都很好。它只是 ajax 调用是问题。提前谢谢

Here is my index.php code for receiving the data这是我用于接收数据的 index.php 代码

<?php 
require('connect.php'); 
?>



<html>
<head>
    <title>Bellcom Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-9" style="margin:0 auto; float:none;">
                <span id="message"></span>

                    <div class="form-group">
                        <label>Upload files</label>
                        <input type="file" name="file" id="file">
                    </div>
                    <br>
                    <div class ="form-group">
                        <label>Search for a File Number</label>
                        <input type="text" id="file_name" name="file_name">
                    </div>
                    <br>
                    <div class="form-group">
                        <button type="button" name="submit" id="submit" class="btn btn-info" >Fetch</button>
                    </div>

                <div class="table-responsive" id="display" style="display:none">
                    <table class="table table-bordered">
                        <tr>
                            <td width="10%" align="right"><b>Attribute Name</b></td>
                            <td width="90%"><span id="name"></span></td>
                        </tr>

                         <tr>
                            <td width="10%" align="right"><b>System ID</b></td>
                            <td width="90%"><span id="sysid"></span></td>
                         </tr>
                         <tr>
                            <td width="10%" align="right"><b>Date</b></td>
                            <td width="90%"><span id="date"></span></td>
                         </tr>
                    </table>
                </div>
            </div>

        </div>
    </div>
</body>
</html>

<script>
    $(document).ready(function(){ 
        $('#submit').click(function(){
            var id= $('#file_name').val();
                  if(id != '')
                  {
                    $.ajax({ 
                        url:"file_controller.php",
                        type: "POST",
                        data:{id:id},
                        dataType:"JSON",
                        success:function(data)
                            {
                                 $('#meeting_details').css("display", "block");
                                 $('#name').text(data.name);
                                 $('#sysid').text(data.sysid);
                                 $('#date').text(data.date);
                            },
                        error: function (data) {
                          alert("data is not received");
                        }
                    })
                  }else{
                    alert("Please Select a file");
                    $('#meeting_details').css("display", "none");
                  }
        });
    });
</script>

and here is my php file for processing the data.这是我用于处理数据的 php 文件。

<?php 

require('connect.php'); 

    $f_name = "";


    if (isset($_POST['id'])) {
        $f_name = strip_tags($_POST['id']);//remvove html tags
        $f_name = str_replace(' ', '', $f_name);//remove spaces
        $f_name = strtolower($f_name);//lower case
        $arr = array('XML_','.xml');
        $f_name = implode("$f_name",$arr);

        $database_file = mysqli_query($conn,"SELECT * FROM xml_files WHERE file_name='$f_name'");
        $row = mysqli_fetch_array($database_file);
        $database_file_name=$row['file_name'];



        if (file_exists("files/$database_file_name")) {
            $xml=simplexml_load_file("$database_file_name") or die("Error: Cannot create object");


            foreach ($xml->children() as $node) {
                foreach ($node->children() as $nodechild) {
                    foreach ($nodechild->children() as $nodegrandchild){
                        $arr = $nodegrandchild->attributes();   // returns an array

                        if (!empty($arr["name"])) {
                            $data['name'] = $arr["name"];   //save the attribute name in an array format 
                            print ("name=".$arr["name"]);     // get the value of this attribute
                            print ("<br>");
                            print ("<p><hr>");
                        }
                        if (!empty($arr["sysid"])) {
                            $data['sysid'] = $arr["sysid"];
                            print ("sysid=".$arr["sysid"]);
                            print ("<br>");
                            print ("<p><hr>");
                        }
                         if (!empty($arr["date"])) {
                            $data['date'] = $arr["date"];
                            print ("date=".$arr["date"]);
                            print ("<br>");                 
                            print ("<p><hr>");
                        }
                    }
                }
            }
        }
    echo json_encode($data);
    }       
 ?>

Try to add event.preventDefault() :尝试添加event.preventDefault()

$('#submit').click(function(event){
     event.preventDefault()

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

相关问题 如何在不刷新页面和 URL 路径的情况下使用 ajax 和 php 发送表单数据 - How to send form data using ajax and php without page refresh and URL path 我想将文件发送到api url并获取响应而无需使用ajax重定向到它 - i want to send file to api url and get the response without redirecting to it using ajax 使用 post with express:我想从用户那里接收数据,然后用一个新文件将该数据发送回用户 - Using post with express: I want to receive data from the user and then send that data back to the user with a new file AJAX 转 PHP,无需页面刷新 - AJAX to PHP without page refresh PHP和AJAX-提取到另一页并作为模态发送时不刷新数据 - PHP & AJAX - No refresh data when fetch to another page and send it as a Modal 使用ajax在同一文件中将javascript变量传递给php函数,而无需刷新页面 - Pass javascript variable to php function using ajax in same file without page refresh 如何在一页上发送Ajax请求并在另一页上接收响应 - How to send ajax request on one page and receive response on another page 我如何将数据发送到数据库而不用单击按钮刷新页面 - How I send data to the database without refresh the page with click button 无法使用ajax将数据发送到php文件 - can not Send data to php file using ajax 如何在不刷新页面的情况下发送和接收数据 - How to send and receive data without refreshing the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM