Javascript 可以在同一个PHP文件中传递数据吗?

Javascript 可以在同一个PHP文件中传递数据吗?,javascript,php,Javascript,Php,我有一个按钮(有人创建了它,我需要编辑),它保存到employee.php中的$elisting。该按钮将转到名为edit()的javascript函数$elisting代码如下: $elisting =""; $queryf = "select employee.id as myid,jobtitle,info,locid,deptid,gender,dob,emid,employee$cid.name,username,dept,datejoin,location.name as

我有一个按钮(有人创建了它,我需要编辑),它保存到employee.php中的
$elisting
。该按钮将转到名为
edit()
的javascript函数<代码>$elisting代码如下:

$elisting ="";

    $queryf = "select employee.id as myid,jobtitle,info,locid,deptid,gender,dob,emid,employee$cid.name,username,dept,datejoin,location.name as loc from  employee

        left join location on location$cid.id = employee.locid 

        inner join department on department.id = employee.deptid order by username

    ";

    $resultf = pg_query($queryf); 

    $numrows = pg_numrows($resultf);

    while ($rowf = pg_fetch_array($resultf)) {

        $ct=$ct+1;

        $myid = $rowf[myid];

        $uname = $rowf[username];

        $loc1 = $rowf[loc];

        $dept = $rowf[dept];

        $date = $rowf[datejoin];

        $name = $rowf[name];

        $emid = $rowf[emid];

        $dob = $rowf[dob];

        $gender = $rowf[gender];

        $job = $rowf[jobtitle];

        $info = $rowf[info];

        $datejoin = $rowf[datejoin];

        $locid = $rowf[locid];

        $deptid = $rowf[deptid];

        $newbal = $rowf[newbal];

        $templatelist = "";

        $tempcn = 0;

        $querytemp = "select tid,ltype from leaveatemplate$cid where username='$uname' order by tid desc";

        $resulttemp = pg_query($querytemp); 

        while ($rowtemp = pg_fetch_array($resulttemp)) {

            $tempcn = $tempcn +1;

            $tid = $rowtemp[tid];

            $ltype = $rowtemp[ltype];

            if($tempcn=="1")

                $templatelist = "$ltype|$tid";

            else

                $templatelist = $templatelist.","."$ltype|$tid";

        }

$elisting=$elisting."<tr><td align=\"center\">$uname</td><td align=\"center\">$loc1</td><td align=\"center\">$dept</td>
        <td align=\"center\">$date</td>
        <td align=\"center\"><a><button class=\"btn btn-mini\"data-toggle=\"modal\"href=\"#long\"
        onClick=\"javascript:edit('$name','$uname','$emid','$dob','$gender','$job','$info','$datejoin','$locid','$deptid','$myid','$templatelist');\"><i class=\"icon-pencil\"></i></a>
$llisting ="";

    $ct=0;

    //leave code
    $ccompassionate = "3";
    $cemergency = "4";
    $cmaternity = "5";
    $cmedical = "6";
    $cannual = "2";

    //leave value
    $compassionate = 2;
    $emergency = 5;
    $maternity = 60;
    $medical = 20;

    $lapplicant = $_POST['lapplicant'];

    $querye = "select leavetype.id, leavetype.ltype, leaves.leave from leavetype,leaves where username='".$_SESSION["username"]."'";

        $resulte = pg_query($querye); 

        while ($rowe = pg_fetch_array($resulte)) {

            $ct=$ct+1;

            $lid = $rowe[id];

            $ltype = $rowe[ltype];

            $leave = $rowe[leave];  

        if ($lid == $ccompassionate) {
            $compassionate_query = pg_query("select ltotal from leave where lapplicant='".$lapplicant."' and ltype='".$ccompassionate."'");
            if (count($compassionate_query) > 0) {
                foreach ($compassionate_query as $data) {
                    $total_compassionate = $total_compassionate + $data['total'];
                    $value = $compassionate - $total_compassionate;
                }

            } else {
                $value = $compassionate;
            }
$llisting=$llisting."<tr><td align=\"center\">$ct</td><td>$ltype</td><td><input type='text' value='$lapplicant'></td><td align=\"center\">";

        $llisting=$llisting."<select name=\"temp$lid\" id=\"temp$lid\" class=\"span12\"><option value=\"0\"> - </option> $tlisting</select>";

        $llisting=$llisting."</td></tr>\n";
由于我未能尝试在
edit()
中获取
b
,因此我需要找到在
$elisting
中获取$uname的方法,并将其发送到同一个PHP文件(employee.PHP)中的
$llisting
,代码如下:

$elisting ="";

    $queryf = "select employee.id as myid,jobtitle,info,locid,deptid,gender,dob,emid,employee$cid.name,username,dept,datejoin,location.name as loc from  employee

        left join location on location$cid.id = employee.locid 

        inner join department on department.id = employee.deptid order by username

    ";

    $resultf = pg_query($queryf); 

    $numrows = pg_numrows($resultf);

    while ($rowf = pg_fetch_array($resultf)) {

        $ct=$ct+1;

        $myid = $rowf[myid];

        $uname = $rowf[username];

        $loc1 = $rowf[loc];

        $dept = $rowf[dept];

        $date = $rowf[datejoin];

        $name = $rowf[name];

        $emid = $rowf[emid];

        $dob = $rowf[dob];

        $gender = $rowf[gender];

        $job = $rowf[jobtitle];

        $info = $rowf[info];

        $datejoin = $rowf[datejoin];

        $locid = $rowf[locid];

        $deptid = $rowf[deptid];

        $newbal = $rowf[newbal];

        $templatelist = "";

        $tempcn = 0;

        $querytemp = "select tid,ltype from leaveatemplate$cid where username='$uname' order by tid desc";

        $resulttemp = pg_query($querytemp); 

        while ($rowtemp = pg_fetch_array($resulttemp)) {

            $tempcn = $tempcn +1;

            $tid = $rowtemp[tid];

            $ltype = $rowtemp[ltype];

            if($tempcn=="1")

                $templatelist = "$ltype|$tid";

            else

                $templatelist = $templatelist.","."$ltype|$tid";

        }

$elisting=$elisting."<tr><td align=\"center\">$uname</td><td align=\"center\">$loc1</td><td align=\"center\">$dept</td>
        <td align=\"center\">$date</td>
        <td align=\"center\"><a><button class=\"btn btn-mini\"data-toggle=\"modal\"href=\"#long\"
        onClick=\"javascript:edit('$name','$uname','$emid','$dob','$gender','$job','$info','$datejoin','$locid','$deptid','$myid','$templatelist');\"><i class=\"icon-pencil\"></i></a>
$llisting ="";

    $ct=0;

    //leave code
    $ccompassionate = "3";
    $cemergency = "4";
    $cmaternity = "5";
    $cmedical = "6";
    $cannual = "2";

    //leave value
    $compassionate = 2;
    $emergency = 5;
    $maternity = 60;
    $medical = 20;

    $lapplicant = $_POST['lapplicant'];

    $querye = "select leavetype.id, leavetype.ltype, leaves.leave from leavetype,leaves where username='".$_SESSION["username"]."'";

        $resulte = pg_query($querye); 

        while ($rowe = pg_fetch_array($resulte)) {

            $ct=$ct+1;

            $lid = $rowe[id];

            $ltype = $rowe[ltype];

            $leave = $rowe[leave];  

        if ($lid == $ccompassionate) {
            $compassionate_query = pg_query("select ltotal from leave where lapplicant='".$lapplicant."' and ltype='".$ccompassionate."'");
            if (count($compassionate_query) > 0) {
                foreach ($compassionate_query as $data) {
                    $total_compassionate = $total_compassionate + $data['total'];
                    $value = $compassionate - $total_compassionate;
                }

            } else {
                $value = $compassionate;
            }
$llisting=$llisting."<tr><td align=\"center\">$ct</td><td>$ltype</td><td><input type='text' value='$lapplicant'></td><td align=\"center\">";

        $llisting=$llisting."<select name=\"temp$lid\" id=\"temp$lid\" class=\"span12\"><option value=\"0\"> - </option> $tlisting</select>";

        $llisting=$llisting."</td></tr>\n";
调用html页面的代码:

if($msg!="") $alert= "<div class=\"alert\"><i class=\"icon-info-sign\"></i> $msg</div><p>&nbsp;</p>";

    $source2 = file_get_contents('http://'.$_SESSION["url"].'/cgi-bin/vo/'.$_SESSION["sessid"].'.interface.designer.vo?file=eleave/employee.htm');

    $html2 = str_replace("[templateurl]",$templateurl,$source2);

    $html2 = str_replace("[alert]",$alert ,$html2);

    $html2 = str_replace("[elisting]",$elisting ,$html2);

    $html2 = str_replace("[ulisting]",$ulisting ,$html2);

    $html2 = str_replace("[locationlisting]",$locationlisting ,$html2);

    $html2 = str_replace("[deptlisting]",$deptlisting ,$html2);

    $html2 = str_replace("[llisting]",$llisting ,$html2);

    $html2 = str_replace("[wfid]","sid=$sid" ,$html2);

    echo $html2;
如果($msg!=”)$alert=“$msg

”; $source2=file\u get\u contents('http://'.$\u SESSION[“url”]。/cgi-bin/vo/'.$\u SESSION[“sessiond”]。.interface.designer.vo?file=eleave/employee.htm'); $html2=str_replace(“[templateurl]”,$templateurl,$source2); $html2=str_replace(“[alert]”,$alert,$html2); $html2=str_replace(“[elisting]”,$elisting,$html2); $html2=str_replace(“[ulisting]”,$ulisting,$html2); $html2=str_replace(“[locationlisting]”,$locationlisting,$html2); $html2=str_replace(“[deptlisting]”,$deptlisting,$html2); $html2=str_replace(“[llisting]”,$llisting,$html2); $html2=str_replace(“[wfid]”,“[sid=$sid]”,$html2); echo$html2;
请先试试基本的,我对这个没问题

在JS中:

<script>
    function TestTriggered(name){
        alert("the function triggered!");
        alert("My name is: " + name);
    }
</script>

函数TestTriggered(名称){
警报(“功能已触发!”);
警报(“我的名字是:“+名字”);
}
关于HTML部分

<input type="button" name="myButton" onClick="javascript:TestTriggered('John');" value="Please click"/>


请先尝试一下,看看它是如何工作的。

在你的Javascript中,只使用
函数编辑(a,b,c,d,e,f,g,h,i,j,k,l){alert(b);}
你得到了什么?仍然显示相同的页面,先生。。没什么变化。。我弄糊涂了。在PHP上使用print_r()进行调试,并使用console.log()查看您正在做什么。JS中的alert只是在同一页面上弹出一个框。如果您使用的是Chrome,请按Shift+Ctrl+J打开控制台。打开控制台,单击以触发edit()函数,然后查看是否有任何结果。控制台没有显示任何内容,先生。这不是答案。是的,我知道,因为代码很长,它不适合注释部分。这是为了展示事情是如何运作的,然后我们可以相应地将其扩展到用户的问题。我尝试放置您的代码,它显示空白页和
NetworkError:500内部服务器错误-http://192.168.1.5/eleave/employee.php?sid=SID421464131stwptwbply497.
然后我尝试编辑我的代码:
,它显示了来自
edit()
的相同页面,文本字段中没有数据。出现错误:
ReferenceError:testtriggered未定义。
它是否与
href=\“\35; long\”
相关?long是div的id,包含$elisting中的表单和数据。您必须首先定义函数
testtriggered
。删除
href
,因为它用于导航到其他页面我在html页面中有sir。。当我删除
href='#long'
时,控制台中没有出现任何错误<代码>引用错误:未定义testtriggered