1

So basically, I got a php file where I create a script in the header.

In this script, I take the value of two textbox with document.getElementByID and I concatenate them in a variable. But now, in the same script, I want to send that var to a php section to use it.

I tried the ajax way, but since the php and the javascript is in the same file, it make an error.

Here is what the script section looks like :

IN 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>

EDIT 1

So here it is, I tried to use ajax this time, but this isn't working, seems like i miss something. Here is the server.php:

<?php

$parameter = $_POST['command']; 

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

And here is my ajax call in my client.php (in a js script):

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);

For some reason, it doesn't go farther then the ajax.open step. On the debugger console of IE10, i got this error : SCRIPT438: Object doesn't support property or method 'open' .

3
  • the php file needs to be on a separate page. And then you can get it either through GET or POST. Commented Sep 23, 2015 at 16:09
  • what is your browser? (IE or FF) Your error mean that browser not support XMLHttpRequest. Commented Sep 24, 2015 at 2:31
  • I use IE10 and it seems like it should support it... en.wikipedia.org/wiki/XMLHttpRequest Commented Sep 24, 2015 at 12:18

3 Answers 3

1

You are trying to run a serverside script in your ClientSide script, that's never going to work.

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

If you want to do something with the data from text_1 and text_2, you should create a php file that can handle a post/get request via AJAX or a simple submit, featuring the data from those elements, and make it return or do whatever it is you want it to end up doing.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't use javascript variable (client) from php (server). To do that, you must call 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>

And this is execute.php on server

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

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

         (...)

?>

2 Comments

The problem is, after I get $output, I close the php section to use this value into a javascript instruction. Otherwise, I don't know how to access the php data in the original file. Normally I was just using <?php $myvalue ?> since it was in the same file. Do you know how?
do you try below js code when you got $output: ajax.onreadystatechange=function() { if (ajax.readyState==4 && ajax.status==200) { alert(ajax.responseText); } }
0

Alright... I pretty much changed and tested many things and I found out that the problem was the async property of the .send command. I was checking the value of the respondText too fast. Putting the third property of .open to false made the communication sync, so I receive the infos properly. I got another problem right now, but its not the same thing, so I will do another post.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.