doufan6033 2016-03-27 10:43
浏览 45
已采纳

复制相同的数据请求查询,但使用JavaScript Ajax帖子

Replicate the same data request query, but use a JavaScript Ajax post so that it doesn’t refresh the page on button press, just requests another page and display in a section on the page. i need some help changing this to a ajax post

my code

*<?php
if(isset($_POST['submit']))
{
$success = $_POST['success'];
$dates = $_POST['dates'];
$datee = $_POST['datee'];
/*** mysql hostname ***/
$hostname = 'localhost';
$dbname = '*******';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = '*******';
try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    $tablename = 'login_attempts';
    $sql = 'SHOW COLUMNS FROM `'.$tablename.'`';
    $fields = array();
    $csv = array();

    $stmt = $dbh->query($sql);      
    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        array_push($fields, $row['Field']);
    }

    array_push($csv, $fields);
    $success = mysql_real_escape_string($success);  
    $sql = "SELECT * FROM $tablename WHERE success = '".$success."' AND attempted >='".$dates."' AND attempted <='".$datee."'";
    $stmt = $dbh->query($sql);
    $stmt->execute();

    $csv = array();

    while($row = $stmt->fetch(PDO::FETCH_NUM))
    {
        array_push($csv, $row);
    }

    $fp = fopen('file.csv', 'w');

    foreach ($csv as $row) {
        fputcsv($fp, $row);
    }

    fclose($fp);
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=export.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
    readfile('file.csv');
    $dbh = null;
} catch(PDOException $e) {
    echo $e->getMessage();
}
     exit();}
?>
<html>
    <head>
        <title>csv with criteria</title>
    </head>
    <body>

        <form action="csv2.php" method="post"  enctype="multipart/form-data">
        Select data range
        <br>
        <input type="date" name="dates" id="dates"> Starting date
        <br>
        <input type="date" name="datee" id="datee"> Ending date
        <br>
        Select what data you'd like
        <br>
        <input type="radio" name="success" value="1" checked> Yes<br>
        <input type="radio" name="success" value="0"> No<br>
        <input type="submit" value="show" name="submit">
        <br>
    </form>
    </body>
</html>*
  • 写回答

2条回答 默认 最新

  • 普通网友 2016-03-27 10:53
    关注

    If you want to use AJAX request on form submit you should:

    1. Import the jQuery library: e.g. <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    2. Create a new JavaScrpit file where you will catch the form submit event, send jQuery request and analize the response, e.g.:

      $(document).ready(function(){
      $("#myForm").submit(function(e){
         e.preventDefault(); //stop sending the form
      
         //here you should validate your form
      
         //submit it via AJAX:
         $.ajax({
             url: "csv2.php",
             data: { dates: $("#dates").val(), datee: $("#datee").val(), $('input[name='success']:checked', '#myForm').val() }
         })
      })
      });
      

    You can use another functions of AJAX object such as success or error callback functions: jQuery.ajax(). Don't forget to add ID to your form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出