使用Javascript和PHP自动填充文本框

时间:2015-09-23 10:50:11

标签: javascript php

我检查用户是否已登录,如果他们是我从数据库中提取他们的详细信息,那么我想自动将这些数据填入我的表单的一部分。

'

这是我当前尝试用于自动填充的代码,但字段仍显示为空白

while(OCIFetch($stmt)) {
    if(OCIResult($stmt,"PASSWORD")==$Password) {
        $flag=true;

        $First=OCIResult($stmt,"FIRSTNAME");
        $Sur=OCIResult($stmt,"SURNAME");
        $Email=OCIResult($stmt,"EMAIL");
        $Phone=OCIResult($stmt,"PHONE");
        $Address=OCIResult($stmt,"ADDRESS");
        $City=OCIResult($stmt, "CITY");
        $Post=OCIResult($stmt, "POSTCODE");
        //set up session - Declare session variables and assign their corresponding values
        session_start();
        $_SESSION['RegUser'] = OCIResult($stmt,"USERNAME");
        $_SESSION['RegFirst'] = $First;
        $_SESSION['RegSur'] = $Sur;
        $_SESSION['RegEmail'] = $Email;
        $_SESSION['RegPhone'] = $Phone;
        $_SESSION['RegAdd'] = $Address;
        $_SESSION['RegCity'] = $City;
        $_SESSION['RegPost'] = $Post;
    }

2 个答案:

答案 0 :(得分:0)

您不需要javascript,只需将值回显到您的html表单字段

<input id="example" value="<?php echo $Post?>" />

冲洗并重复所有其他表格区域。

答案 1 :(得分:0)

试试这个,

//Autofill the details if the user is logged in
window.onload = function() {
    document.forms['Order']['RegFirst'].value = "<?php echo $_SESSION['RegFirst'];?>";
    document.forms['Order']['RegSur'].value = "<?php echo  $_SESSION['RegSur'];?>";
    document.forms['Order']['RegEmail'].value = "<?php echo $_SESSION['RegPhone'];?>";
    document.forms['Order']['RegPhone'].value = "<?php echo  $_SESSION['RegPhone'];?>";
    document.forms['Order']['RegAdd'].value = "<?php echo $_SESSION['RegAdd'];?>";
    document.forms['Order']['RegCity'].value = "<?php echo$_SESSION['RegCity'];?>";
    document.forms['Order']['RegPost'].value = "<?php echo  $_SESSION['RegPost'];?>";

}