Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2013
    Thanked 0 Times in 0 Posts

    how to pass an variable thats an array to a function

    I am having trouble passing an array to a java script function. here is my code.
    Its weird I created another variable called test and set it equal to the array($test = "$row[0]"; )
    it worked, but does not show the word. What it does is just shows one of the letters if I do test[1] in ....american('$test[1]')... why wont it show the whole word when its just $test, and why nothing shows with $row[0]

    //javascript for the buttons gets info from the database by means of php.
    function american(title)
    $test = "$row[0]";
    		echo "<button onclick=american('$row[0]')>$row[0]</button><br />";

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,662 Times in 2,631 Posts
    $test doesn't have any value in the code you have posted here. Assuming $row[0] is a string within an array of $row, then using $test[0] would be the first letter of the value in $row[0].
    PHP Code:
    $row = array('This is a string');
    $test $row[0];
    $test[0]; //T 
    That's simply because a string is nothing more than a char array, so you can access a string letter by letter by accessing into it.

    As for the JS, the only thing I can see being a problem PHP wise is if your string contains a ' within it which would damage the request for the american function call. Otherwise, assuming that $row is a valid array, everything looks fine. You can check your output HTML source to see what's in the <button> value and onclick attribute.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  3. Users who have thanked Fou-Lu for this post:

    cpamieta (05-14-2013)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts