-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.

Community
  • 1
  • 1
henry
  • 13
  • 1
  • 2
  • 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. – Jared Farrish Jun 09 '12 at 00:10
  • 5
    @JaredFarrish: *bajillion. Now you're accurate. – Ry- Jun 09 '12 at 00: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 – henry Jun 09 '12 at 00:29
  • @Derek i read that post, that post doesnt have any parameter passed. In that case, i have no problem whatsoever; already tested. Mine have a parameter to pass, as soon as i added that, all crumble ! – henry Jun 09 '12 at 00:38
  • 1
    It's possible when you make use of a framework: http://agiletoolkit.org/intro/4 – romaninsh Jun 09 '12 at 00: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. – Jared Farrish Jun 09 '12 at 00:50

1 Answers1

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.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • Might prove helpful: http://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. – Jared Farrish Jun 09 '12 at 00:14
  • 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). – henry Jun 09 '12 at 00:32
  • @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 !!!! – henry Jun 09 '12 at 00:41