Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.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发送到php(在同一文件中)_Javascript_Php_Ajax - Fatal编程技术网

将字符串从javascript发送到php(在同一文件中)

将字符串从javascript发送到php(在同一文件中),javascript,php,ajax,Javascript,Php,Ajax,所以基本上,我得到了一个php文件,在其中我在头文件中创建了一个脚本 在这个脚本中,我使用document.getElementByID获取两个文本框的值,并将它们连接到一个变量中。但是现在,在同一个脚本中,我想将var发送到一个php部分来使用它 我尝试了ajax方式,但由于php和javascript位于同一个文件中,因此会出错 下面是脚本部分的外观: 在FILE.PHP中 <script type="text/javascript"> rowNum = 0; f

所以基本上,我得到了一个php文件,在其中我在头文件中创建了一个脚本

在这个脚本中,我使用
document.getElementByID
获取两个文本框的值,并将它们连接到一个变量中。但是现在,在同一个脚本中,我想将
var
发送到一个php部分来使用它

我尝试了ajax方式,但由于php和javascript位于同一个文件中,因此会出错

下面是脚本部分的外观:

在FILE.PHP中

<script type="text/javascript">
    rowNum = 0;
    function some_function()
    {    
        var command = "somebasiccommand";

        if(document.getElementById("text_1").value != "" && document.getElementById("text_2").value != "")
        {
            command += " " + document.getElementById("text_1").value + " " + document.getElementById("text_2").value;
        }               

        <?php

            $parameter = command; <----- obviously not working, but that's basically what im looking for

            $output = exec("someExecutable.exe $parameter");

                                (...)
        ?>
    }
</script>

出于某种原因,它没有比ajax.open步骤走得更远。在IE10的调试器控制台上,我遇到了以下错误:SCRIPT438:Object不支持属性或方法“open”。

您不能从php(服务器)使用javascript变量(客户端)。为此,必须调用ajax

<script type="text/javascript">
    rowNum = 0;
    function some_function()
    {    
         var command = "somebasiccommand";
         if(document.getElementById("text_1").value != "" && document.getElementById("text_2").value != "")
         {
            command += " " + document.getElementById("text_1").value + " " + document.getElementById("text_2").value;
         }               
        //AJAX call to a php file on server
       //below is example
       var ajax = window.XMLHttpRequest;
       ajax.open("POST", "yourhost.com/execute.php", true);
       ajax.send(command);   
    }
</script>

rowNum=0;
函数some_函数()
{    
var command=“somebasiccommand”;
if(document.getElementById(“text_1”).value!=“”&document.getElementById(“text_2”).value!=“”)
{
command+=''+document.getElementById(“text_1”).value+''+document.getElementById(“text_2”).value;
}               
//对服务器上php文件的AJAX调用
//下面是一个例子
var ajax=window.XMLHttpRequest;
open(“POST”,“yourhost.com/execute.php”,true);
send(命令);
}
这是服务器上的execute.php

<?php
        $parameter = $_POST['command']; 

        $output = exec("someExecutable.exe $parameter");

         (...)

?>

您试图在客户端脚本中运行服务器端脚本, 那是行不通的


如果您想处理来自text_1和text_2的数据,您应该创建一个php文件,该文件可以通过AJAX或简单的提交处理post/get请求,以这些元素的数据为特征,并使其返回或执行您希望它最终执行的任何操作。

好的。。。我做了很多更改并测试了很多东西,发现问题出在.send命令的async属性上。我检查respondText的值太快了。将.open的第三个属性设置为false使通信同步,因此我正确地接收信息。我现在遇到了另一个问题,但不是同一个问题,所以我将发表另一篇文章。

php文件需要放在单独的页面上。然后你可以通过get或POST获得它。你的浏览器是什么?(IE或FF)您的错误意味着浏览器不支持XMLHttpRequest。我使用IE10,它似乎应该支持它。。。问题是,在我获得$output之后,我关闭php部分,将该值用于javascript指令。否则,我不知道如何访问原始文件中的php数据。通常我只是在使用,因为它在同一个文件中。你知道怎么做吗?当你得到$output:
ajax.onreadystatechange=function(){if(ajax.readyState==4&&ajax.status==200){alert(ajax.responseText);}}
<script type="text/javascript">
    rowNum = 0;
    function some_function()
    {    
         var command = "somebasiccommand";
         if(document.getElementById("text_1").value != "" && document.getElementById("text_2").value != "")
         {
            command += " " + document.getElementById("text_1").value + " " + document.getElementById("text_2").value;
         }               
        //AJAX call to a php file on server
       //below is example
       var ajax = window.XMLHttpRequest;
       ajax.open("POST", "yourhost.com/execute.php", true);
       ajax.send(command);   
    }
</script>
<?php
        $parameter = $_POST['command']; 

        $output = exec("someExecutable.exe $parameter");

         (...)

?>