Ahmed Alvi
Ahmed Alvi

Reputation: 841

How can I pass a PHP string into a Javascript function call?

I simple want to wan to pass php string into java script function here is the code. I know there is problem in sending string to javascript function but how can i solve it????If i pass integer value then it works fine it shows problem in passing string

echo "<td><a id='".$row['Patient_Id']."' onclick=changename(".$row['Patient_Id'].",".$row['age'].",".$row['Notes'].") >".$row["Patient_Name"]."</a></td></tr>";

Here is the java script funtion

function changename(vlue,age,id)
{
 alert(id);
 var MyDiv1 = document.getElementById(vlue);
 document.getElementById('age').innerHTML=age;          
 var MyDiv2 = document.getElementById('pname');
 MyDiv2.innerHTML = MyDiv1.innerHTML;   //d     
 var MyDiv3 = document.getElementById('hidden');
 MyDiv3.value  =vlue;               
}

Upvotes: 0

Views: 697

Answers (2)

George
George

Reputation: 36794

Your parameters are string values, so they should be enclosed in quotes:

echo "<td><a id='".$row['Patient_Id']."' onclick=changename( '".$row['Patient_Id']."' , '".$row['age']."' , '".$row['Notes']."' ) >".$row["Patient_Name"]."</a></td></tr>";
//                                                           ^----------------------^ etc

As it stands, JavaScript perceives your strings as identifiers. If you had checked your console you'd have seen corresponding errors (assuming these identifiers aren't defined).

Upvotes: 2

Pedro Estrada
Pedro Estrada

Reputation: 56

your onclick doesn't have quotations

echo "<td><a id='".$row['Patient_Id']."' onclick='changename(".$row['Patient_Id'].",".$row['age'].",".$row['Notes'].")' >".$row["Patient_Name"]."</a></td></tr>";
                                                 ^ //here                                                             ^ // and here

Upvotes: 0

Related Questions