2015-06-05 52 views
1

我有一個html頁面,它從form1中獲取數據,並將其存儲在使用php的mysql數據庫中(點擊提交)。現在,我使用另一個窗體form2來自動填充form1中的輸入字段,如果它是數據庫中已存儲的記錄。從php中獲取數據並在同一個HTML頁面填充另一個表格的表格

所以,我給了form2的兩個字段,並寫了一個「match.php」,試圖在mysql數據庫中搜索記錄。當我點擊form2上的提交時,這會運行。我還包含在「match.php」js腳本中,以更改表單1的值屬性。

但問題是當我在form2的sumit上顯示空白頁時。 如果找到匹配項,我想要填充form1中的文本框!

match.php

$conn = mysqli_connect($servername, $username, $password, $dbname); 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 
$a=$_POST['empname']; 
$b=$_POST['vid']; 
$sql = "SELECT * FROM table1 where name='$a' AND visitorid='vid'"; 
$result = mysqli_query($conn, $sql); 

while($row = mysqli_fetch_assoc($result)) 
$b=$row['number']; 

echo " 
<script> 
var form1 = document.forms[0]; 
var form2 = document.forms[2]; 

var number= form1.elements['n11']; 

number.value=".$b.""; 
echo " </script>"; 

Form1中

<form enctype="multipart/form-data" action="con3.php" method="post"> 

    <table> 
     <tr rowspan="500" colspan="500"> 
     <td> 
     <input type='file' name="userfile" onchange="readURL(this);"/> 
      <img id="blah" src="#" alt="your image" /> 
     </td> 
     </tr> 

     <tr colspan="2" rowspan="2"> 
     <td>VISITOR'S ID</td> 
     <td><input type="textbox" id="t1" value="automatically generated" name="n1" onfocus="this.blur()"/></td> 

     <tr colspan="2" rowspan="2"> 
     <td>NAME OF THE EMPLOYEE</td><span style="color:red;">*</span> 
     <td><input type="textbox" id="t4"  name="n4" onblur="f4()"/><span style="color:red;">*</span></td> 
     </tr> 

     <tr colspan="2" rowspan="2"> 
     <td>ID PROOF</td> 
     <td> 
     <select      name="n20" onblur="f38()" > 
     <option>id</option> 
     <option>1210312117</option> 
     </select> 
     </tr> 

     <tr colspan="2" rowspan="2"> 
     <td>CONTACT NUMBER ON EMERGENCY</td><span style="color:red;">*</span> 
     <td><input type="textbox" id="t14" name="n11" onblur="f14()" required/><span style="color:red;">*</span></td> 
     </tr> 

     <tr colspan="2" rowspan="2"> 
     <td>INTIME</td> 
     <td><input type="textbox" id="t17" name="n16" onblur="f17()"/></td> 
     <td><input type="button" value="PICKTIM" id="t18" onclick="document.getElementById('t17').value = f88()"/></td> 
     </tr> 

     <tr colspan="2" rowspan="2"> 
     <td>OUTTIME</td> 
     <td><input type="textbox" id="t100" name="n17" onblur="f100()"/></td> 
     </tr> 

     <td><input type="submit" value="SAVE RECORD" id="t25" onclick="f25()"/></td> 
     </tr> 
</table> 

窗口2

<form action="match.php" method="post"> 
<h3> Old employee?</h3> 

    Name of the emplyee: <input type="text" name="empname" id="id1" /></br> 
    Visitor id: <input type ="text" name="vid" id="id2"/> <br/> 
    <input type="submit" value="submit"/> 
    </form> 
+1

你能提供你正在使用的代碼嗎? –

+0

告訴我們你做了什麼 – Sherlock

+0

請張貼您的代碼,表單,match.php和任何其他相關代碼。還提供任何錯誤消息,包括error_log中最近的行。 – Tarquin

回答

0

存儲您的SQL數據結果(你fetch-> assoc命令())在$ _SESSION對象像這樣:

//Before the DOCTYPE and every other line 
<?php session_start(); ?> 

//In your php code : 
$row = mysqli_fetch_assoc($result->empname) 
//I don't use this method of PHP for databases, I use PDO, but the result is the same and so I don't know if this is above is really correct 
//but you seem to know what to do 
$_SESSION["empname"] = $empname; 

//And in your form2 
Name of the employee : <?php echo $_SESSION["empname"]; ?> 

要在提交按鈕後顯示對象,請使用session_start()在頁面上啓動會話;你可以通過echo $ _SESSION [「object_name」]顯示它們;

相關問題