简体   繁体   中英

Form which retrieves data from mysql using php and fills another form in the same HTML page

I have an html page which takes data from one form, say, form1 and stores it in mysql database using php(on clicking submit). Now i am using another form, form2, to automatically fill in the input fields in form1, if it is an already stored record in database.

so, I give two fields in form2 and wrote a "match.php" that tries to search for the record in mysql database. This is run when i click submit on form2. I have also included in "match.php" js script to change the value attributes of form 1.

But the problem is when i clock on sumit of form2 , blank page is shown. I want the textboxes in form1 to be filled if match is found!

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>

form2

<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>

Store your SQL data results (that you fetch->assoc()) in $_SESSION objects like this :

//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"]; ?>

To display your objects after the submit button, start the session on the page with session_start(); and you can display them by echo $_SESSION["object_name"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM