将while中的变量传递给popupdiv

时间:2014-10-07 05:37:59

标签: javascript php html variables input

我有表显示SQL表的值。每行都有一个下拉列表,允许您选择要执行的操作。当您选择操作并单击' go',它弹出一个表单,我希望能够将$ id变量从表传递到弹出div表单,然后再传递到下一个pop页面。 ID在下拉列表中正确显示,但无论何时显示div,它都会显示不正确的ID(它显示表的第一个ID,而不是所选下拉列表的ID)。

继承人的PHP

    while($row = mysql_fetch_array($proposal_result)) {
            $date=substr($row["date"], 0, 50);
            $formatted_date=date('d/m/Y', strtotime($date));
            $id=substr($row["idproposal"], 0, 50);
            $businessname=substr($row["businessname"], 0, 50);
            $status=substr($row["status"], 0, 50);
            $staff=substr($row["staff"], 0, 50);
            $file_access='<bucket-location>';
            $file_name='proposal_'.$id.'_'.$businessname.'.pdf';
            $file=$file_access.$file_name;
            print "<tr><td>".$formatted_date."</<td>
            <td>".$id."</td>
            <td width='25px'><a href='".$file."'target='_blank'><img src='".$images."/attachment.png' alt='file'></a></td>
            <td>".$businessname."</td><td>".$staff."</td>
            <td>".$status."</td>
            <td>
                    <div>
                        <select id='zb-admin-dropdown' name='zb-admin-dropdown' class='dropdowns'  required>
                            <option value='0'>Select and action...*</option>
                            <option value='1'>Change Status for ID#".$id."</option>
                            <option value='2'>Delete Proposal</option>
                        </select>
                    <input type='submit' id='report-submit' value='Go' onclick='displayDiv()'></div>

                    </td></tr>";
        }

的Javascript

function displayDiv() {
    e=document.getElementById("zb-admin-dropdown");
    strUser=e.options[e.selectedIndex].value;
    if (strUser=='1') {
        document.getElementById('abc').style.display = "block";     
    }
    if (strUser=='2') {
        document.getElementById('def').style.display = "block";
    }
}

popupdiv

print "<div id='abc'>
                <div id='popup'>
                    <form name='changestatus' action='' method='post'>
                        <!--<img id='close' src='images/3.png'> CLOSE ICON-->
                        <h2>Change Status for ".$id."</h2>
                        <hr>
                        <textarea name='deletecomments' placeholder='Comments...'></textarea><br />
                        <a href='/delete?id=".$id."&businessname=".$businessname."'><input type='button' id='report-submit' value='Delete Proposal'></a><a href='/zerobooks-admin-dashboard'><input type='button' id='report-submit' value='Cancel'></a>
                    </form>
                </div>
            </div>";
print "<div id='def'>
                <div id='popup'>
                    <form name='deletefeedback' action='' method='post'>
                        <!--<img id='close' src='images/3.png'> CLOSE ICON-->
                        <h2>Reason for deleting proposal <br />".$id."</h2>
                        <hr>
                        <input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>Added by mistake<br />
                        <input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>No longer required<br />
                        <input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>Incorrect Information Provided<br />
                        <input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>Reason 4<br />
                        <textarea name='deletecomments' placeholder='Comments...'></textarea><br />
                        <a href='/delete?id=".$id."&businessname=".$businessname."'><input type='button' id='report-submit' value='Delete Proposal'></a><a href='/zerobooks-admin-dashboard'><input type='button' id='report-submit' value='Cancel'></a>
                    </form>
                </div>
            </div>";

我的目标是将正确的ID传递给popupdiv。任何帮助,将不胜感激。您可以忽略popupdiv表单的内容。

1 个答案:

答案 0 :(得分:2)

在html中,单击按钮时调用该函数。在函数调用中传递变量。所以:

<input type='submit' id='report-submit' value='Go' onclick='displayDiv(' + $id + ')'></div>

然后让javascript函数接受输入:

function displayDiv(divId) { ...

并在javascript中使用它。但问题是,php是服务器端,而javascript是客户端。因此,要获取服务器端php的信息,您需要发出http请求。

我无法看到所有代码的位置,但您可以通过最少的重构尝试一些解决方法。首先,在显示div元素之后,您可以使用javascript更新href标记:

 `document.getElementById(/*a_element's_id*/).href=''+divId`  

否则,我不是php专家,但我想也许如果php脚本在同一范围内,那么两个php脚本都可以访问全局变量或全局静态变量吗?

就个人而言,我会全部使用javscript! :) 祝你好运