如何在提交表单进行进一步更新后保留字段中的输入

时间:2012-12-05 04:41:31

标签: php html

我想知道在表单提交后是否可以在表单字段内保留用户输入,以便用户可以更新条目。我有一个html注册表单[有一些JS验证],然后是一个php文件,用于将数据插入到sql&同时在表格视图中显示插入的数据。我还在php文件中包含表单的html代码,以便我可以在提交后看到该表单。但是在表单提交后我无法将数据保留在字段中!这是表格:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript">
            <!--
            function validateNum(evt) {
                        var theEvent = evt;
                        var key = theEvent.keyCode || theEvent.which;
                        key = String.fromCharCode( key );
                        var regex = /[0-9]/;

                        if( !regex.test(key) ) {
                            theEvent.returnValue = false;
                            if(theEvent.preventDefault) theEvent.preventDefault();
                        }
                    }       
            function validate(evt){

                if( document.myForm.ic.value == ""){
                    alert( "IC Number cann't be empty!" );
                    document.myForm.ic.focus() ;
                    return false;}
                else if(isNaN( document.myForm.ic.value ) || document.myForm.ic.value.length != 12){
                    evt.preventDefault();

                    alert( "Please provide your correct IC Number!" );
                    document.myForm.ic.focus() ;
                    return false;}
                if( document.myForm.name.value == "") {

                    alert( "Name cann't be empty!" );
                    document.myForm.name.focus() ;
                    return false;
                } 

                if( document.myForm.contact.value == ""){

                    alert( "Contact number cann't be empty!");
                    document.myForm.contact.focus() ;
                    return false;
                    } else if(isNaN( document.myForm.contact.value )) 
                    {
                    evt.preventDefault();

                    alert( "Please provide your correct Contact Number!" );
                    document.myForm.contact.focus() ;
                    return false;

                }
                 if( document.myForm.address.value == "" ){
                    alert( "Please provide your Address!" );
                    document.myForm.address.focus() ;
                    return false;
                }
            }

            //-->
        </script>
    </head>
    <style type="text/css">
        h2 {
            color: #06C;
        }
        body {
            background-color: #FFC;
        }
    </style>

    <body>
        <form name="myForm" method="post" action="insert.php" onsubmit="return(validate(event));">
            <div align="center"><br>
                    <table width="453" border="0">
                        <tr>
                            <th colspan="4" bgcolor="#99FFFF" scope="col">
                                <h3>Workshop Name: PHP! </h3></th>
                        </tr>
                        <tr bgcolor="#99FF99">
                            <td width="142"> IC Number</td>
                            <td width="15"><div align="center">:</div></td>
                            <td colspan="2"><div align="right">
                                    <input
                                        name="ic" type="text" id="ic" maxlength="12" size="45" onkeypress='validateNum(event)'/>
                                </div></td>
                        </tr>
                        <tr bgcolor="#99FFFF">
                            <td>Full Name</td>
                            <td><div align="center">:</div></td>
                            <td colspan="2"><div align="right">
                                    <input
                                        name="name" type="text" id="name" size="45"/>
                                </div></td>
                        </tr>
                        <tr bgcolor="#99FF99">
                            <td>Contact No.</td>
                            <td><div align="center">:</div></td>
                            <td colspan="2"><div align="right">
                                    <input
                                        name="contact" type="text" id="contact" size="45" onkeypress='validateNum(event)' />
                                </div></td>
                        </tr>
                        <tr bgcolor="#99FFFF">
                            <td>Email</td>
                            <td><div align="center">:</div></td>
                            <td colspan="2"><div align="right">
                                    <input
                                        name="mail" type="text" id="mail" size="45"/>
                                </div></td>
                        </tr>
                        <tr bgcolor="#99FF99">
                            <td height="60">Address</td>
                            <td><div align="center">:</div></td>
                            <td colspan="2">
                                <div align="right">
                                    <textarea name="address" id="address" cols="35" rows="3"></textarea>
                                </div>
                            </td>
                        </tr>
                        <tr bgcolor="#99FFFF">
                            <td colspan="2">&nbsp;</td>
                            <td width="231"><input type="reset" value="Clear" /></td>
                            <td width="47"><div align="right">
                                    <input type="submit" value="Submit" />
                                </div></td>
                        </tr>
                    </table>

                    <br>
                        </div>
                        </form>
                     </body>
                 </html>

这是insert.php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript">
            <!--
            function validateNum(evt) {
                        var theEvent = evt;
                        var key = theEvent.keyCode || theEvent.which;
                        key = String.fromCharCode( key );
                        var regex = /[0-9]/;

                        if( !regex.test(key) ) {
                            theEvent.returnValue = false;
                            if(theEvent.preventDefault) theEvent.preventDefault();
                        }
                    }       
            function validate(evt){

                if( document.myForm.ic.value == ""){
                    alert( "IC Number cann't be empty!" );
                    document.myForm.ic.focus() ;
                    return false;}
                else if(isNaN( document.myForm.ic.value ) || document.myForm.ic.value.length != 12){
                    evt.preventDefault();

                    alert( "Please provide your correct IC Number!" );
                    document.myForm.ic.focus() ;
                    return false;}
                if( document.myForm.name.value == "") {

                    alert( "Name cann't be empty!" );
                    document.myForm.name.focus() ;
                    return false;
                } 

                if( document.myForm.contact.value == ""){

                    alert( "Contact number cann't be empty!");
                    document.myForm.contact.focus() ;
                    return false;
                    } else if(isNaN( document.myForm.contact.value )) 
                    {
                    evt.preventDefault();

                    alert( "Please provide your correct Contact Number!" );
                    document.myForm.contact.focus() ;
                    return false;

                }
                 if( document.myForm.address.value == "" ){
                    alert( "Please provide your Address!" );
                    document.myForm.address.focus() ;
                    return false;
                }
            }

            //-->
        </script>
    </head>
    <style type="text/css">
        h2 {
            color: #06C;
        }
        body {
            background-color: #FFC;
        }
    </style>

    <body>
        <form name="myForm" method="post" action="update.php" onsubmit="return(validate(event));">
            <div align="center"><br>
                    <table width="453" border="0">
                        <tr>
                            <th colspan="4" bgcolor="#99FFFF" scope="col">
                                <h3>Workshop Name: PHP! </h3></th>
                        </tr>
                        <tr bgcolor="#99FF99">
                            <td width="142"> IC Number</td>
                            <td width="15"><div align="center">:</div></td>
                            <td colspan="2"><div align="right">
                                    <input
                                        name="ic" type="text" id="ic" maxlength="12" size="45" onkeypress='validateNum(event)'/>
                                </div></td>
                        </tr>
                        <tr bgcolor="#99FFFF">
                            <td>Full Name</td>
                            <td><div align="center">:</div></td>
                            <td colspan="2"><div align="right">
                                    <input
                                        name="name" type="text" id="name" size="45"/>
                                </div></td>
                        </tr>
                        <tr bgcolor="#99FF99">
                            <td>Contact No.</td>
                            <td><div align="center">:</div></td>
                            <td colspan="2"><div align="right">
                                    <input
                                        name="contact" type="text" id="contact" size="45" onkeypress='validateNum(event)' />
                                </div></td>
                        </tr>
                        <tr bgcolor="#99FFFF">
                            <td>Email</td>
                            <td><div align="center">:</div></td>
                            <td colspan="2"><div align="right">
                                    <input
                                        name="mail" type="text" id="mail" size="45"/>
                                </div></td>
                        </tr>
                        <tr bgcolor="#99FF99">
                            <td height="60">Address</td>
                            <td><div align="center">:</div></td>
                            <td colspan="2">
                                <div align="right">
                                    <textarea name="address" id="address" cols="35" rows="3"></textarea>
                                </div>
                            </td>
                        </tr>
                        <tr bgcolor="#99FFFF">
                            <td colspan="2">&nbsp;</td>
                            <td width="231"><input type="reset" value="Clear" /></td>
                            <td width="47"><div align="right">
                                    <input type="submit" value="Update" />
                                </div></td>
                        </tr>
                    </table>

                    <br>
                        </div>
                        </form>


                    <br>
                        </div>
                        </form>

                        <div align="center">
                          <?php
                        if (!mysql_connect('localhost', 'root', '')) {

                            echo "Connected";
                        }


                        mysql_select_db("workshop");


// Get values from form
                        $ic = mysql_real_escape_string($_POST['ic']);
                        $name = mysql_real_escape_string($_POST['name']);
                        $contact = mysql_real_escape_string($_POST['contact']);
                        $mail = mysql_real_escape_string($_POST['mail']);
                        $address = mysql_real_escape_string($_POST['address']);

                        if (staff_detail_exist($ic) == "available") {

                            insert_staff_detail($ic, $name, $contact, $mail, $address, $paytype);
                            echo "<p style='text-align:center; color:green;'>" . "Workshop application successful! You will be notified shortly via E-mail after confirmation! Thank You!";
                        } else if (staff_detail_exist($ic) == "exist") {

                            echo "<p style='text-align:center; color:red;'>" . "Record already exists! Please enter another Staff ID. Thank You!" . "</p>";
                        }

                        function insert_staff_detail($ic, $name, $contact, $mail, $address, $paytype) {

                            $sql = "INSERT INTO apply (staffid, staffname, staffno, staffemail, staffaddress, paytype) VALUES ('$ic', '$name', '$contact', '$mail', '$address','$paytype')";
                            mysql_query($sql);
                        }

                        function staff_detail_exist($ic) {

                            $result = null;
                            $sql = "SELECT * FROM apply WHERE staffid = '$ic'";
                            $data = mysql_query($sql);

                            if (mysql_num_rows($data) == 0) {

                                $result = "available";
                            } else {

                                $result = "exist";
                            }

                            return $result;
                        }

                $staffid = $_POST['ic'];

                $con = mysql_connect("localhost", "root", "");
                if (!$con) {
                    die('Could not connect: ' . mysql_error());
                }

                mysql_select_db("workshop", $con);
                $result = mysql_query("SELECT * FROM apply where staffid = '$ic'");

                echo "<table width=400 border=1 cellpadding=0 align=center>";

                while ($row = mysql_fetch_array($result)) {
                    echo "<tr>";
                    echo "<th>Staff/IC Number: </th><td>" . "<center>" . $row['staffid'] . "</center>" . "</td>";
                    echo "</tr>";
                    echo "<th>Name: </th><td>" . "<center>" . $row['staffname'] . "</center>" . "</td>";
                    echo "</tr>";
                    echo "<th>Email: </th><td>" . "<center>" . $row['staffemail'] . "</center>" . "</td>";
                    echo "</tr>";
                    echo "<th>Contact No.: </th><td>" . "<center>" . $row['staffno'] . "</center>" . "</td>";
                    echo "</tr>";
                    echo "<th>Address: </th><td>" . "<center>" . $row['staffaddress'] . "</center>" . "</td>";
                    echo "</tr>";
                }
                echo "</table>";

                mysql_close($con);
 ?>
                    </body>
                </html>

我试图在php文件的表单字段中添加value="<? echo "$row['staffid']"?>",但没有运气!我只是基本的PHP。那么,有什么帮助吗?谢谢!

3 个答案:

答案 0 :(得分:1)

Take the form posted values just above your html code like this
<?php
    if (isset($_POST["submit"]) && $_POST["submit"]=='Submit') {
        $name=$_POST["name"];
    }
?>

And echo it in your html form.
 <input name="name" type="text" id="name" size="45" value="<? echo $name?>"/>

答案 1 :(得分:1)

我已经使用过几次这个功能;非常方便

function getPost($field){
   return (isset($_POST[$field]) && $_POST[$field] != "" ? $_POST[$field] : "");
}

用法

<input type="text" name="contact" value="<?php echo getPost("contact"); ?>" />

这是针对用户提交信息并且由于某种原因再次发送回表单的情况 - 例如,他们的条目可能没有通过PHP验证。

答案 2 :(得分:1)

感谢所有,它终于工作了:)我在输入标签中使用了value="<?php echo isset($_POST['myField']) ? $_POST['myField'] : 'myField_db' ?>"。所以,它是:<input type="text" name="myField" value="<?php echo isset($_POST['myField']) ? $_POST['myField'] : 'myField_db' ?>" />其中myField是输入名称&amp; myField_db是数据库中的列名。