0

I already saw this answer in the forum passing variables using html "anchor" to JavaScript

<a href="#" onclick="size( 'x' ); return false;">HTML Link</a>

but what I want to do is pass the php variable using html tag to the JavaScript, and right now I cannot run it or make the windows pop out using JS.

Below are my codes for PHP and JS:

reference: html to javascript

php file

while($row = mysqli_fetch_array($varRuleId))
{
echo"<tr>";
echo"<td class=tbl_size id=tbl_size_text   colspan='13'>"."<a href='#' onclick='dRaLoader( '$row[resultId]','$srId'); return false;'>". $row['resultId'] ."</a>"."</td>";
echo"<tr>"; 
}

js file

function dRaLoader()
{
    var myWindow = window.open("","myWindow","width=800,height=600");
    myWindow.document.write("<p>This is 'myWindow'</p>");
    myWindow.opener.document.write("<p>This is the source window!</p>");
}

thanks in advance.

Community
  • 1
  • 1
user3287181
  • 61
  • 1
  • 9
  • your dRaLoader-function doesnt take any arguments lol – john Smith Feb 13 '14 at 13:01
  • you should have parameters in your js function – MamaWalter Feb 13 '14 at 13:02
  • Check your console. Guaranteed you have errors in there ;) – RobinvdA Feb 13 '14 at 13:05
  • yeah right, but i cannot even pop out the window, when you click the link it does nothing, when it should pop out something right? can you check that one? thanks – user3287181 Feb 13 '14 at 13:05
  • replace this `onclick='dRaLoader( '$row[resultId]','$srId'); return false;'` with this `onclick='dRaLoader(); return false;'` – RobinvdA Feb 13 '14 at 13:06
  • @RobinvdA dude it solved the problem it is already popping out a new windowsl thanks from my heart XD. for the second part: am i passing the php variable the right way? thanks – user3287181 Feb 13 '14 at 13:13
  • `onclick='dRaLoader(\"$row[resultId]\",\"$srId\"); return false;'` Make sure your function accepts 2 variables `function dRaLoader($var2, $var1)` – RobinvdA Feb 13 '14 at 13:19
  • Just to clarify, you can do this `echo "$var";`. You can't do this `echo '$var';`. if you want to echo a variable using single quotes, you have to append it to the string `echo '' . $var . '';` – RobinvdA Feb 13 '14 at 13:20
  • @RobinvdA thanks again dude! really helpful, for the last part this is my code. . . var myWindow = window.open("display.php?$srId$resultId","myWindow","width=1204,height=768"); im already calling another file that will display the result thanks. – user3287181 Feb 13 '14 at 13:29
  • im already passing the variable to a display page but im not having any luck guys here is my code from JS passing to a .php file for the display thanks var myWindow = window.open("dRA.php?resultId='+.$resultId.+'&srId='+.$srId.+'","myWindow","width=1204,height=768"); – user3287181 Feb 13 '14 at 13:57

2 Answers2

0

Try this.

echo '<td class="tbl_size id=tbl_size_text"  colspan="13"><a href="#" onclick="dRaLoader(\''.$row['resultId'].'\',\''.$srId.'\');">'. $row['resultId'] .'</a></td>';


function dRaLoader($resultId, $srcId)
{
    //do something with $resultId and $srcId here
    var myWindow = window.open("","myWindow","width=800,height=600");
    myWindow.document.write("<p>This is 'myWindow'</p>");
    myWindow.opener.document.write("<p>This is the source window!</p>");
}
Jay Bhatt
  • 5,601
  • 5
  • 40
  • 62
0

you have to concat the php variables with your string.

while($row = mysqli_fetch_array($varRuleId))
{
echo"<tr>";
echo"<td class=tbl_size id=tbl_size_text   colspan='13'><a href='#' onclick='dRaLoader( \"".$row[resultId]."\",\"".$srId."\"); return false;'>". $row['resultId'] ."</a></td>";
echo"<tr>"; 
}
MamaWalter
  • 2,073
  • 1
  • 18
  • 27
  • And you should use double quotes `onclick='dRaLoader( \"".$row[resultId]."\",\"".$srId."\"); return false;'` – RobinvdA Feb 13 '14 at 13:12