Search code examples
javascriptphphtmlajaxonline-game

call js function in php with multiple parameter


hello everyone i try to write a php game and my problem is i want to call call js function in my php code amd i wrote

echo "<tr><td><p class='tttex'>".$str1."</p></td><td><button id='nashbtn".$var."' onclick='nash(".$var.",".$str1.",".$idnash.",1);'><img src='img/tick.png' align='center' width='25' height='25'></button></td><td><button id='nashbtn2".$var."' onclick='nash(".$var.",".$str1.",".$idnash.",0);'><img src='img/X.png' align='center' width='25' height='25'></button></td><tr>";`

and my js code is

    function nash(num,str22,stna,id)
{
    document.getElementById("nashbtn"+num).disabled=true;
    document.getElementById("nashbtn"+num).style.visibility = "hidden";
    document.getElementById("nashbtn2"+num).disabled=true;
    document.getElementById("nashbtn2"+num).style.visibility = "hidden";
    var xmlhttp2;
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp2=new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp2.open("POST","ajaxk.php",true);
    xmlhttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp2.send("nashg="+str22+"&tna="+stna+"&idnash="+id);
}

when is not work plase help me

`


Solution

  • Write your echo statement as below.

    echo "<tr><td><p class='tttex'>$str1</p></td>".
    
    "<td><button id='nashbtn$var' onclick=\"nash('$var','$str1','$idnash',1)\"><img src='img/tick.png' align='center' width='25' height='25'></button></td>".
    
    "<td><button id='nashbtn2$var' onclick=\"nash('$var','$str1','$idnash',1)\"><img src='img/X.png' align='center' width='25' height='25'></button></td><tr>";
    

    Hope it will help you :)