Hello Community,
I need help with getting javascript function arguments in php eg.

function jsFunction(arg) {
    <?php phpFunction(arg); ?>

<button onclick="jsFunction(this.id);" id="XXXX"/>

But when i press the button it doesn't send "XXXX" to the php function (<?php phpFunction(args); ?>) it sends "args" (as plain text).

Please Help...

Recommended Answers

All 10 Replies

How is php to know what javascript is? You should create ajax call and send json back to the server for processing. then you can do decode_json(args);

nope, you cant do that, php is executed before js and they dont share memory

so to summarize what the two posters above so eloquently stated:

PHP is a server language, which means it's called before a page renders, and the browser is given control over content. So, PHP itself cannot possibly "know" that there is javascript on the page, and that it is trying to do something that warrants communication.

In turn, javascript is a browser (or, client side) langauge, and is given control after the page has been rendered (technically... in reality, it is given control as the DOM loads, or as a line of javascript is read by the browser).

To do what you want to do, you need to have a page designed to accept a GET or POST from a javascript AJAX query.

TL;DR: Make a separate PHP page that will do the processing, make an AJAX call to that page. JSON or not, it works just fine if you expect what you get.

And just to dump my 2 cents in here, I highly recommend using jQuery. It makes all of this so much easier in my opinion.

I don't know much AJAX, JSON or jQuery so could you give me an example of how i can use this in my example.

There is still the question though, what are you ultimately trying to accomplish here? Getting the ID from an element in jQuery is super simple, but what are you doing with this ID exactly? What is the end result you are hoping for?

Here a couple of links with some good examples for using jQuery to make the AJAX calls. The Nettuts link has some good information about passing params to the external file.



I don't have any trouble with getting the id, what i want to do is pass the id (from the javascript side) and pass it to the php function.

so, without you knowing how PHP is used, it's a bit difficult.. but maybe this will help...

2 pages.

Page 1:

<title>AJAX Example</title>
<script type="text/javascript">

function myAJAX()
  var textBox = document.getElementById("myText");
  var myString = textBox.value;

  if (myString.length.replace(" ","") < 1)
      alert ("You need to put something in the box!!!");
      return false;

  //try to create an AJAX object in the browser....
  var AJAXObj;

      AJAXObj = (window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP"));
  catch (e)
      alert ("Your browser does not support AJAX calls. Sorry :-/");

  //set a callback function, for when we get a response from the server (the PHP page).
  if (xmlhttp.readyState==4 && xmlhttp.status==200)

  //try to open a connection: open(METHOD, URL, ASYNC) -- since we are doing a "GET" we are going to add
  //all of our params to the "URL" by adding a "?" and then name=value pairs.

  AJAXObj.open("GET", "myAJAX.php?string=" + myString, true);
  AJAXObj.send(); //sends our request.

<input type="text" id="myText" />
<input type="button" onclick="myAJAX();" />

save the above as example.html
next page is going to be our PHP page:


  echo ("Your string was: " . $_GET['string']);


save as myAJAX.php

Move both to your server. Open example.html. You now have a working AJAX call.

You will see that the JAVASCRIPT and the PHP are totally separate, and the JAVASCRIPT sends information to the PHP which simply waits for data to act upon.

So lets talk about what you want to do. You wanted to send the ID of the item clicked to the PHP.

We set the AJAXObj.open("GET", "myAJAX.php?string=" + myString, true).... the way we got myString was by grabbing the id of the item (so, we know it), and we can just send it.. we can also use a number of methods to figure out the ID based on the click event in javascript, or any other number of things....

I don't think I can be any more clear than this...

Good luck!


Thanks everyone i have found another way to get around it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.