-3

Possible Duplicate:
How to pass a PHP variable to Javascript?

I need to somehow make this function to accept parameter.

index.html

function dataOut(name2) {
    var names = name2.value;
    document.getElementById("output2").innerHTML = names;
    //even document.getElementById("output2").innerHTML= "blablabla"; 
    //just to show that it works
}

And the function will be called from :

dbase.php

...
echo "<td><a href='javascript:dataOut(this.value)'>" . $row['FirstName'] . "</a></td>";
...

I dont have problem on displaying the the table. The link does show with their respective "$row['FirstName']" which are string from database. I tried to use non-parameter function:

function dataOut(){}
echo "<td><a href='javascript:dataOut()'>" . $row['FirstName'] . "</a></td>";

and these work fine.

If i try to pass parameter, the function will not do anything; not even if i just want to print random string as in the commented section of the function. If i hover my mouse on the link created within that table i see:

javascript:dataOut(firstname)
//where first name is the value of the $row['FirstName']

Alternatives that i tried:

echo "<td><a href='javascript:dataOut(this.value)'>" . $row['FirstName'] . "</a></td>";
echo "<td><a href='javascript:dataOut($row['FirstName'])'>" . $row['FirstName'] . "</a></td>";
echo "<td><a href='javascript:dataOut('" . $row['FirstName'] .'")'>" . $row['FirstName'] . "</a></td>"; //this give me undefined variable error
echo "<td><a href='javascript:dataOut(". $row['FirstName']. ")'>" . $row['FirstName'] . "</a></td>"; // this no error

Any help would be appreciated. More over i tried also <a onclick=dataOut ....> which doesnt work either.

6
  • 5
    Holy cow, there's gotta be a billion questions on SO about how to do this. Well, maybe not that many, but it's been asked and answered. Commented Jun 9, 2012 at 0:10
  • 5
    @JaredFarrish: *bajillion. Now you're accurate. Commented Jun 9, 2012 at 0:12
  • yes, i am aware that there have been over teragazzilion of similar questions, and this website did recommended similar posts before i even posted this. the alternative codes are the answer that i got from most of the previous ones that people ask. Well, those doesnt solve my problem, and this is my very first question ever which i have tried to research for 2 weeks. So, i would appreciate less critique on my newbiness in this case. Instead, guide me to the right link that i havent touch Commented Jun 9, 2012 at 0:29
  • 1
    It's possible when you make use of a framework: agiletoolkit.org/intro/4 Commented Jun 9, 2012 at 0:38
  • 1
    No offense intended, but maybe that was rude on my part. If you stick around long enough around these parts, you'll see that some questions get asked, and asked, and asked, and asked (again? yeah, again), ad nauseum. This is one of them, and there's only so many ways to skin this cat. In the grand scheme of things, this is up for closing (IMO) as it's really too localized. So, no offense. You're just hearing us regulars groaning and moaning cuz we're bored. Commented Jun 9, 2012 at 0:50

1 Answer 1

2

You shouldn't try to create JavaScript strings by concatenating quotes and plain strings. Use json_encode() instead which is guaranteed to output a valid JavaScript expression:

echo "<td><a href='javascript:dataOut(".json_encode($row['FirstName']).")'>" . $row['FirstName'] . "</a></td>";

On a side-note, using javascript: urls is generally a bad idea. Better use onclick="dataOut(...); return false;" instead and use a useful href value or # if no proper URL for people without JavaScript exists. Of course it would be even better to register the event properly instead of using an inline event, and storing the data in a data- attribute but this would be even more off-topic here.

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

3 Comments

Might prove helpful: stackoverflow.com/search?q=json_encode%28%29 And yeah, I was going to say that's some funky ol school "Java Script" going on there.
haha .. sorry for the funkyness .. MY very first project. In general, i have to get data from database, display them in table, make those table row more interactive with event listener (in this case i use href instead), and each interaction suppose to call a javascript function and pass the value of that specific row as parameter. I am just testing to pass 1 parameter (i have multiple param to pass in actuality).
@thiefmaster thank you so much for pointing out the JSON. that is a field i have never touched before. You have just saved me another weeks of struggle ... thanks !!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.