1

My javascript file:

var ms = 3000;
$.get("curl.php", { test: ms } );

My curl.php:

$ms = $_GET("test");
echo $ms;

Firefox firebug says:

Fatal error: Function name must be a string in C:\Users\Jansu\Documents\workspace\php\curl.php on line 2

what could be the problem?

Even better would be when the javascript and php code is in the same file, so I don't have to post/get anything. Just somehow pass javascript to php.

1
  • even if you put it all into one file, you still need to do get/post. yes it's the same file, but php part runs on server, and javascript part runs inside the browser. Commented May 4, 2011 at 11:16

3 Answers 3

10

You want [], not () (docs):

$ms = $_GET["test"];

Re your edit:

even better would be when the javascript and php code is in the same file, so i do not have to post/get anything. just somehow pass javascript to php

Even if they're in the same file, they're executed in completely different places. The JavaScript is executed on the client, the PHP is executed on the server. To get data from the client to the server, it has to be sent there. Ajax (the way you did it) is the primary way you do that without causing a full page refresh, so you're on the right track.

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

4 Comments

when i use ajax like this, will all the script inside curl.php ran also?
@Jaanus: Yes. Any time the browser requests the curl.php resource, the PHP code within it gets run. You can use logic within your PHP code to only run the relevant bits, or (probably better) have a separate PHP file for processing the ajax request, since it's best to keep distinct things separated.
is there a way i can contact you out of stackoverflow. im doing kinda secret project . and i have some questions :)
@Jaanus: Sure, click through to my profile and the contact info is there. I'm also (cough) an independent contractor. I have quite a lot of work on right now, but...
0

You don't mention if you are sanitising your data separately, but the filter_input function is more secure than calling $_GET directly.

Also POST is generally considered better than GET for AJAX calls as it doesn't have the same string length limit, slightly more secure too.

$ms = filter_input(INPUT_POST, 'test', FILTER_SANITIZE_STRING);

Comments

0
<script type="text/javascript">
 var ms = 9000;
function test()
{
    $.ajax({ url: "a.php",
         data: {"test":ms},
         type: 'get',
         success: function(output) {
                      $('#testing').html(output);
                  }
    });
}
test();
</script>
<?php
$ms = $_GET["test"];
echo "$ms";
?>
<div id="testing">
</div>

You can echo the output in any HTML element. Here i'm using div.

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.