Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将脚本变量从HTML传递到PHP_Javascript_Php_Html - Fatal编程技术网

Javascript 将脚本变量从HTML传递到PHP

Javascript 将脚本变量从HTML传递到PHP,javascript,php,html,Javascript,Php,Html,我试图找到一个解决方案,但没有什么对我有效,或太复杂 我试图将一个变量从HTML中的脚本发送到php文件 这里是我的HTML脚本 <script type="text/javascript"> function DetectAndServe() { urlp=[];u=location.search.replace("?","").split("&").forEach(function(d){e=d.split("=");urlp[e[0]]=e[1]

我试图找到一个解决方案,但没有什么对我有效,或太复杂

我试图将一个变量从HTML中的脚本发送到php文件

这里是我的HTML脚本

<script type="text/javascript">
    function DetectAndServe() {
        urlp=[];u=location.search.replace("?","").split("&").forEach(function(d){e=d.split("=");urlp[e[0]]=e[1];})
        var redirectlink = "https://www.mywebsite.com/redirect_app?utm_medium=" + urlp.utm_medium + "&utm_source=" + urlp.utm_source + "&utm_campaign=" + urlp.utm_campaign;
    }   
</script>

函数DetectAndServe(){
urlp=[];u=location.search.replace(“?”,”).split(“&”).forEach(函数(d){e=d.split(“=”);urlp[e[0]]=e[1];})
变量重定向链接=”https://www.mywebsite.com/redirect_app?utm_medium=“+urlp.utm_medium+”&utm_source=“+urlp.utm_source+”&utm_活动=“+urlp.utm_活动”;
}   

在我的PHP文件中使用redirectlink变量最简单的方法是什么?实际上,您需要创建一个表单,将函数结果发送到PHP文件中

使用JavaScript的表单示例:

<form method="post" action="#">
    <input type="text" id="redirectlink" name="redirectlink">
    <button type="submit">Send</button>
</form>
<script>
function DetectAndServe() {
    urlp=[];u=location.search.replace("?","").split("&").forEach(function(d){e=d.split("=");urlp[e[0]]=e[1];})
    var redirectlink = "https://www.mywebsite.com/redirect_app?utm_medium=" + urlp.utm_medium + "&utm_source=" + urlp.utm_source + "&utm_campaign=" + urlp.utm_campaign;
    return redirectlink;
}  
document.getElementById('redirectlink').value = DetectAndServe();
</script>

发送
函数DetectAndServe(){
urlp=[];u=location.search.replace(“?”,”).split(“&”).forEach(函数(d){e=d.split(“=”);urlp[e[0]]=e[1];})
变量重定向链接=”https://www.mywebsite.com/redirect_app?utm_medium=“+urlp.utm_medium+”&utm_source=“+urlp.utm_source+”&utm_活动=“+urlp.utm_活动”;
返回重定向链接;
}  
document.getElementById('redirectlink')。value=DetectAndServe();
在您的PHP中,您可以检索:

<?php
if(isset($_POST['redirectlink'])){
    $link = $_POST['redirectlink'];
    echo $link;
}

实际上,您需要创建一个表单,将函数结果发送到PHP文件中

使用JavaScript的表单示例:

<form method="post" action="#">
    <input type="text" id="redirectlink" name="redirectlink">
    <button type="submit">Send</button>
</form>
<script>
function DetectAndServe() {
    urlp=[];u=location.search.replace("?","").split("&").forEach(function(d){e=d.split("=");urlp[e[0]]=e[1];})
    var redirectlink = "https://www.mywebsite.com/redirect_app?utm_medium=" + urlp.utm_medium + "&utm_source=" + urlp.utm_source + "&utm_campaign=" + urlp.utm_campaign;
    return redirectlink;
}  
document.getElementById('redirectlink').value = DetectAndServe();
</script>

发送
函数DetectAndServe(){
urlp=[];u=location.search.replace(“?”,”).split(“&”).forEach(函数(d){e=d.split(“=”);urlp[e[0]]=e[1];})
变量重定向链接=”https://www.mywebsite.com/redirect_app?utm_medium=“+urlp.utm_medium+”&utm_source=“+urlp.utm_source+”&utm_活动=“+urlp.utm_活动”;
返回重定向链接;
}  
document.getElementById('redirectlink')。value=DetectAndServe();
在您的PHP中,您可以检索:

<?php
if(isset($_POST['redirectlink'])){
    $link = $_POST['redirectlink'];
    echo $link;
}
html文件中的

<script type="text/javascript">
window.location.href = 'https://www.mywebsite.com/myfile.php?variable1=value1&variable2=value2';
</script>
在html文件中

<script type="text/javascript">
window.location.href = 'https://www.mywebsite.com/myfile.php?variable1=value1&variable2=value2';
</script>


AJAX是这里唯一的选择。好的,谢谢,你能更具体一点,在我的情况下如何使用它吗,我是初学者,请对使用AJAX做一些研究,然后,当你有一个特定的问题时回到这里——这不是一个驱动式代码生成服务。你也可以制作一个表单,将你的URL发布到php文件中。你是否需要调用该链接才能捕获
urlp.utm_medium
urlp.utm_source
urlp.utm_活动
as
$\u GET['utm_medium']
$\u GET['utm\u source']
$\u GET['utm\u campaign']
分别?AJAX是这里唯一的选择。好的,谢谢,你能更具体一点,如何使用它在我的情况下,我是初学者,请对使用AJAX做一些研究,然后,当你有一个特定的问题时回到这里——这不是一个驱动式代码生成服务。你也可以制作一个表单,将你的URL发布到php文件中。你是否需要调用该链接才能捕获
urlp.utm_medium
urlp.utm_source
urlp.utm_活动
as
$\u GET['utm_medium']
$\u GET['utm\u source']
$\u GET['utm\u campaign']
分别?
DetectAndServe
没有返回任何内容。是的,刚刚修复了它谢谢你,它工作正常,我现在还有一个问题,我无法用document.frm.reset()重置表单;我有一个错误“document.frm.reset不是一个函数”,我可能需要发布一个其他问题,您需要像这样使用它
document.getElementById(“idform”).reset()
DetectAndServe
没有返回任何内容。是的,刚刚修复了它谢谢你,它工作得很好,我只是有一个其他问题,现在我无法用document.frm.reset()重置表单;我有一个错误“document.frm.reset不是一个函数”,我可能需要发布一个其他问题,您需要像这样使用它
document.getElementById(“idform”).reset()