gpt4 book ai didi

javascript - 复制相同的数据请求查询,但使用 JavaScript Ajax post

转载 作者:行者123 更新时间:2023-11-29 21:12:30 24 4
gpt4 key购买 nike

复制相同的数据请求查询,但使用 JavaScript Ajax post,以便在按下按钮时不会刷新页面,仅请求另一个页面并显示在页面的某个部分中。我需要一些帮助将其更改为 ajax 帖子

我的代码

*<?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>*

最佳答案

如果您想在表单提交上使用 AJAX 请求,您应该:

  1. 导入 jQuery 库:例如<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
  2. 创建一个新的 JavaScrpit 文件,您将在其中捕获表单提交事件、发送 jQuery 请求并分析响应,例如:

    $(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() }
    })
    })
    });

您可以使用 AJAX 对象的其他功能,例如成功或错误回调函数:jQuery.ajax() 。不要忘记将 ID 添加到您的表单中。

关于javascript - 复制相同的数据请求查询,但使用 JavaScript Ajax post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36246445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com