gpt4 book ai didi

php - 将 js 文件的输出传递到另一个 php 文件

转载 作者:行者123 更新时间:2023-12-02 19:32:49 26 4
gpt4 key购买 nike

我有一个 javascript 文件的输出,格式为

js 文件看起来像

$(document).ready(function()
{
$.ajax({
type: "POST",
url: "test.php",
data: { name: "term", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});

我想将这些值输出到一个单独的 Excel 文件中。所以我使用了 php 并将脚本写入另一个文件

<?php
$name = $_POST["name"];
$location = $_POST["location"];
<?php
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('Type', 'Entity Name', 'Link'));
fputcsv($output, results);
?>

并将 javascript 文件包含在 php 文件的 header 部分中。bt 我没有得到 js 文件生成的“结果”输出。如何将js文件的结果传递到php文件?

最佳答案

将数据从 js 传递到 php 的唯一方法是通过 POST 或 GET 请求。

这是一个关于它的老派教程:http://www.tizag.com/phpT/postget.php

"new"且简单的方法是通过 ajax 和 jquery: http://api.jquery.com/jQuery.ajax/

JS:

$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});

PHP:

$name = $_POST["name"];
$location = $_POST["location"];

在“msg”中,您会收到从 php 脚本返回的内容。

关于php - 将 js 文件的输出传递到另一个 php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11307445/

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