gpt4 book ai didi

php - jquery ajax变量到php

转载 作者:搜寻专家 更新时间:2023-10-31 20:44:12 25 4
gpt4 key购买 nike

自从我决定使用基于 jquery 库的不错的三 View 以来,我被困了 2 周。我的问题是我试图将选定文件夹的值作为 php 的变量。

我能够获取值并通过 Jquery 中的 Alertbox 显示它,但该值未发布到 PHP 变量。 echo 什么也没显示。

下面的文字只是为了阐明为什么我需要将此解决方案与 php 等一起使用。

然后这就解决了我将继续努力获得我稍后将发送到 bash 脚本的完整路径,这是已经完成的。它为我的 SQL 数据库创建了一个文本文件,其中包含 flac 或 mp3 专辑中的元标记,该文件已经完成并正在运行。此应用程序是本地的,用于维护数据库。

关于我的问题和代码(记住什么 jquery 和 ajax 对我来说是全新的)

文件名:file_browser.phpJquery部分

<script>
$(document).ready(function()
{
$("button").click(function()
{
var dir = ($(".active").text())
$.ajax({
type: "POST",
url:"file_browser.php",
data: dir,
success: function(data){
alert(dir); dir }
});
});
});
</script>

PHP代码

<?php
echo "<form>
<button>Append</button>
<div id'japp'>";
if (isset($_POST['dir'])) {
$dir = $_POST['dir']; }
echo $dir;
echo "</div>
</form>";
?>

我会继续努力,现在我至少有机会得到一些帮助,以避免再花一周的时间:)。

最佳答案

来自 $.ajax. 上的 jquery 网站

By default, Ajax requests are sent using the GET HTTP method. If the POST method is required, the method can be specified by setting a value for the type option. This option affects how the contents of the data option are sent to the server. POST data will always be transmitted to the server using UTF-8 charset, per the W3C XMLHTTPRequest standard.

The data option can contain either a query string of the form key1=value1&key2=value2, or an object of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string using jQuery.param() before it is sent. This processing can be circumvented by setting processData to false. The processing might be undesirable if you wish to send an XML object to the server; in this case, change the contentType option from application/x-www-form-urlencoded to a more appropriate MIME type.

我假设 $(".active").text() 的值只是您想要传递的值,没有任何 key 与之配对。如上所示,将您的字符串格式化为键/值对,您应该会很棒。

关于php - jquery ajax变量到php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15279886/

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