-2

I am storing some functions in an object, and these functions need to be accessed in PHP via the onclick attribute of the dynamic button. Normally, this would be very simple if the functions were not stored in an object, but unfortunately, this is how these functions need to be created:

var arrayLength = <?php echo $arrayLength; ?>;
var click = {};

for (var num=1;num<=arrayLength;num++) {
  var newClick = "click_" + num;
  click[newClick] = function() { 
    // some contents when this button is clicked 
  };
}

To call them in javascript, I'd do something like:

click['click_' + someID]();

However, since I'm working with PHP, I am not so sure how I will be able to call this in the onclick attribute. It is something I really need to do, so if you know a solution or can think of a workaround, let me know.

Rob W
  • 9,134
  • 1
  • 30
  • 50
Jess
  • 7
  • 1
    AJAX, JSON and HTTP are your friends. – moonwave99 Jul 02 '13 at 20:48
  • Why do you need to call these from PHP? Javascript is made for loading scripts post-server-load, e.g., you *can't* call Javascript until the page is fully loaded. Why not just put the call in a jQuery `.click(function(){})` on `$(document).ready(function(){});`? Am I misunderstanding you? – Liftoff Jul 02 '13 at 20:49
  • 2
    PHP runs on the server, Javascript runs on the client, and only via AJAX shall they twain meet. – Marc B Jul 02 '13 at 20:49
  • You've echoed the length of the array into the JS variable. I'm not able to understand from the code you present, why you could not simply do the same. Do you need to access JS from PHP, or merely create the JS from PHP? – enhzflep Jul 02 '13 at 20:50
  • You just can't run a js function from php. What are you really trying to achieve? – bfavaretto Jul 02 '13 at 20:51
  • The JS needs to be accessed from PHP. Basically, in my PHP code, I am retrieving data entered by the user and stored in mysql, and each entry of this data is displaying a textbox beneath it with a button, each button which will have its own function in this object that is to be accessed in the onclick html button attribute. – Jess Jul 02 '13 at 20:57
  • You can pass data back to PHP using Ajax, but you can't pass or run a function. See @MarcB's comment above, you need to undertand PHP runs on the server, and JS runs on the client. See http://stackoverflow.com/a/13840431/825789 – bfavaretto Jul 02 '13 at 21:19
  • I understand. Thanks for your help. Is there a workaround or some alternative solution? – Jess Jul 02 '13 at 21:27

2 Answers2

2

First: What is so special about your function that you have to write it's body in JS and call it in PHP?

Second: Why not just write that function in PHP and pass data from JS via AJAX?

Third: make double sure you know difference between http, html, php, javascript and AJAX and understand what these are, not just some wibly-wobly stuff that make web pages work. Then you will know your answer yourself.

kajacx
  • 12,361
  • 5
  • 43
  • 70
  • 1
    If I didn't write the function in JS, how would I tell the button what to do when it is clicked?? I don't think that is possible without the help of JS... My script makes it so that upon a button click, another textbox will load dynamically on the page to allow for additional user input. In the past, I was able to get that to work because I was only handling one button. However, now I am trying to handle multiple buttons, each serving a different function, and each of these functions has to do with dynamically adding an additional textbox in a different location on the same page... – Jess Jul 02 '13 at 22:09
  • OK, I'm super bored and have nothing to do anyway, I'll try to make an exaple how to do it with lots of buttons. (using JQuery if its ok) – kajacx Jul 03 '13 at 10:59
  • Done: http://www.pslib.cz/karel.hrkal/php/ajax_example This shows how ajax works though, and has probably few in common with your problem, you would have to change a LOT to make it do what you want. – kajacx Jul 03 '13 at 12:31
1

Your objects can go from PHP to JS, but not the other way around. In order to alleviate this issue, you can use json_encode($object) and pass it to the javascript to be processed. From there, you can then access the properties and values from PHP.

Rob W
  • 9,134
  • 1
  • 30
  • 50
  • The object is already created and stored in javascript though, and it is the PHP that needs access to it. I know that is not possible, now. However, how will the json_encode($object) solve that problem? – Jess Jul 02 '13 at 20:58
  • You can send data to PHP from JS via XmlHttpRequest / or an AJAX request. – Rob W Jul 02 '13 at 21:03
  • Thanks for your help. Hmm.. Even though this may help me get the data of the functions contained within the object, would it allow me to --call-- these functions? – Jess Jul 02 '13 at 21:06