是否可以使用 JavaScript var i= [[“j.php?i=”+input]];发送到 PHP


is it possible to use javascript var i= [["j.php?i="+input]]; to send to php?

我是新手,只是在做我刚刚做的练习: var i= [["j.php?i=1"]];,它将值发送到php并打印,但是当我想要用户的一些输入时,它什么也不做。例如:

文件 J.js

var input = "hello";
var send= [["j.php?i="+input]];

在 j.php

<?php
$e=$_GET['i'];
echo $e;
?>

任何想法或我完全错了?我试图有一些变化。我真的对window.location.href做到了.但我只是想知道如何以这种方式做 var send= [[]]。谢谢

编辑:你的问题不是很清楚,也许试试这个:

var input = "hello";
var url   = "/j.php?i=" + input;
var send  = [[url]];

但是,我不清楚你想用你的send变量做什么......

源语言

您必须使用标记(GET方法(,表单(POST方法(或Ajax请求(任何Http动词(将数据发送到服务器:

<!-- Here you give a unique id to your link -->
<a id="link" href="">your link</a>
<script>
    var i = "Hello";
    // wait for page to be loaded
    document.addEventListener("DOMContentLoaded", function(event) { 
      // change the href attribute of the link with the id equal to 'link'
      // with whatever data contained in the i variable
      // this is done after the page is loaded
      document.getElementById("link").href = "/j.php?i=" + i;
    });
</script>

然后,当您单击链接时,它会将名为 i 的变量发送到您的 php 页面,其中包含Hello的值。

您也可以通过以下方式执行此操作:

<a id="link2">your link</a>
<script>
    var i = "HELLO";
    // wait for page to be loaded
    document.addEventListener("DOMContentLoaded", function(event) { 
        // here we are telling javascript to change the url 
        // in the browser when the user clicks the link
        document.getElementById('link2').onclick = function() {
            window.location = '/j.php?i=' + i
        }
    });
</script>