无法访问AJAX数据[PHP] [英] Unable to access AJAX data [PHP]
问题描述
主页上的表单( index.php
)提交后,会在新标签中打开随机生成的URL。该随机网址运行的脚本名为 run.php
A form on my homepage (index.php
) opens a randomly generated URL in a new tab, after being submitted. That random url is running a script called run.php
<form action="/run.php" method="POST" target="_blank">
<input type="hidden" id="idgen" name="idgen" value="<?php echo $random ?>">
<input type="text" name="userinput" id="userinput">
<button type="submit" onclick="calcResult();">Go!</button>
</form>
我首页上的JS函数使用 userinput
数据来计算结果。
There are JS functions on my homepage that use the userinput
data to calculate a result.
我正在尝试使用AJAX从我的 index.php $ c发送一个值$ c>到表单生成的随机网址。
I'm trying to use AJAX to send a value from my index.php
to the random url generated by the form.
function calcResult() {
var userinput = document.getElementById('userinput').value;
var randomid = document.getElementById('idgen').value;
// various functions
$.ajax({
type: "POST",
url: 'http://example.com/' + randomid, // same random url as the form sends to
data: {result: "Hello!"}, // using simple result for testing purposes
success: function(response){
console.log(response);
}
});
我添加了<?php echo $ _POST ['result']; ?>
到 run.php
文件(表单打开的随机URL正在运行 run.php
)
I added <?php echo $_POST['result']; ?>
to the run.php
file (the random url that the form opens is running run.php
)
console.log(response)
表示它可以正常工作。它控制台记录整个 example.com/<randomID>
源代码(显示整个 run.php
代码),甚至替换<?php echo $ _POST ['result']; ?>
和 Hello。
console.log(response)
shows it working. It console logs the entire example.com/<randomID>
source code (shows entire run.php
code), and even replaces <?php echo $_POST['result']; ?>
with "Hello".
但是,这仅在控制台中有效。通过该表单打开该网页时,不会显示 Hello。
However, this only works in the console. When that webpage is opened by the form, "Hello" is not shown.
我有一个MySQL数据库,可以从 run.php 文件。我尝试对 $ _ POST ['result'];
做同样的事情,但是没有运气。
I have a MySQL database that I insert values into from the run.php file. I tried to do the same for $_POST['result'];
with no luck.
没有使用上述代码时出现控制台错误,错误日志中也没有致命错误。
There are no console errors when I use the above code, nor are there any fatal errors in the error log.
其他信息
- 表单提交到
run.php
,其中包含header('Location:http ://example.com/'.$_POST ['idgen']);
以便重定向到随机网址。 - 使用htaccess的RewriteRule
RewriteRule ^([0-9a-zA-Z] +)$ /run.php?idgen=$1 [L]
- 试图使用'Flight'框架,但在尝试与它一起使用JS函数时遇到问题。
- The form submits to
run.php
which containsheader('Location: http://example.com/'.$_POST['idgen']);
in order to redirect to the random url. - The random urls exist using a htaccess RewriteRule
RewriteRule ^([0-9a-zA-Z]+)$ /run.php?idgen=$1 [L]
- Attempted to use 'Flight' framework, but was runnning into issues when trying to use JS functions with it.
目标
- 总体目标是将结果(从我首页的JavaScript函数获得)发送到PHP。这样,它们将显示在提交表单后打开的随机URL上-这样我就可以显示它们,并将它们添加到数据库等。
推荐答案
您可以尝试JavaScript的windows.location并传递您的值而不是标头位置。
you can try windows.location of JavaScript and pass your value instead of header location.
这篇关于无法访问AJAX数据[PHP]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!