簡體   English   中英

從javascript發送字符串到php(在同一文件中)

[英]Send a string from javascript to php (in the same file)

因此,基本上,我得到了一個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>

編輯1

就是這樣,我這次嘗試使用ajax,但這不起作用,似乎我錯過了一些東西。 這是server.php:

<?php

$parameter = $_POST['command']; 

$output = exec("someexecutable.exe $parameter");
$output_array = preg_split("/[\n]+/",  $output); 
print_r($parameter);
?>

這是我的client.php中的ajax調用(在js腳本中):

var command = "find";

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

        var ajax = new XMLHttpRequest;
        ajax.open("POST", "server.php", true);
        ajax.send(command);
        var output_array = ajax.responseText;
        alert(output_array);

由於某種原因,它沒有比ajax.open步驟更進一步。 在IE10的調試器控制台上,出現以下錯誤:SCRIPT438:對象不支持屬性或方法“ open”。

您正在嘗試在ClientSide腳本中運行服務器端腳本,但此操作永遠無法進行。

https://softwareengineering.stackexchange.com/questions/171203/what-are-the-differences-between-server-side-and-client-side-programming

如果您想對text_1和text_2中的數據進行處理,則應該創建一個php文件,該文件可以通過AJAX或簡單的Submit處理發布/獲取請求,以這些元素中的數據為特征,並使其返回或執行任何操作您是否希望它最終完成。

您不能使用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>

這是服務器上的execute.php

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

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

         (...)

?>

好吧...我幾乎進行了更改並測試了許多內容,我發現問題是.send命令的async屬性。 我檢查的responseText的值太快了。 將.open的第三個屬性設置為false使通信同步,因此我可以正確接收信息。 我現在遇到另一個問題,但問題不一樣,因此我將再發表另一篇文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM