html表单,数据库提交,刷新或返回时的空数据

时间:2014-09-09 13:41:56

标签: javascript php html

我有一个从提交数据到db,并且在输入db之后感谢消息来了,一切都在同一页面。问题是当刷新或返回相同的数据或空数据输入到db。请帮忙。

<?php
$myServer = "localhost";
$myUser = "root";
$myPass = "";
$myDB = "sample"; 

$name=$_POST['fname'];
$mobile=$_POST['mobile'];
$email=$_POST['email'];
//connection to the database
$dbhandle = mysql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer"); 
  //select a database to work with
$selected = mysql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB"); 
//echo "$myDB";
//Insert data into db 
 mysql_query("INSERT INTO reg(name,mobile,email) values ('$name','$mobile','$email')");
 /* echo "$name";
echo "$mobile";
echo "$email";  */
//header("location:thank.html");


?>


<!doctype html>
<html>
    <head>
        <title>Basic form with validation</title>
        <link rel="stylesheet" href="style/style.css" type="text/css" media="all"/>
    </head>
    <body>
    <div style="width:300px; background:#19a3d1;margin:0 auto;height:200px;">
    <?php
        if(empty($_POST))
        {
        ?>

        <form name="myform" id="frm1" action="index.php" method="post" style="margin:0 auto;"onsubmit="return(validate());">
            <h1 style="text-align:center">Form</h1>
            <div  style="display: table; margin:0 auto;">
                <div style="display: table-row;">
                    <div style="display: table-cell;padding:5px;">
                        <label>Name</label>
                    </div>
                    <div style="display: table-cell;">
                        <input type="text" name="fname" id="fname" placeholder="Name" />
                    </div>
                </div>
                <div style="display: table-row;">
                    <div style="display: table-cell;padding:5px;">
                        <label>Mobile</label>
                    </div>
                    <div>
                        <input type="text" name="mobile" id="mobile" placeholder="Mobile" />
                    </div>
                </div>
                <div style="display: table-row;">
                    <div style="display: table-cell;padding:5px;">
                        <label>Email</label>
                    </div>
                    <div>
                        <input type="text" name="email" id="email" placeholder="Email" />
                    </div>
                </div>

                <div style="display: table-row;">
                    <div style="display: table-cell;padding:5px;" ></div>
                    <div style="display: table-cell;padding:5px;"><input type="submit" value="Submit"></div>
                </div>
            </div>
        </form>

        <?php
        }
        else{
        ?>

        <div id="thank" >
            <span style="color:#ff0000;padding:30px;text-align:center;display:block;">Thank you!!</span>
        </div>
        <?php
        }
        ?>
        </div>


        <script src="js/main.js"></script>
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    </body>
</html>`

1 个答案:

答案 0 :(得分:0)

提供提交按钮的名称。

喜欢

<input type="submit" value="Submit" name="btnSubmit">

编写如下的PHP代码:

<?php
......
......

if(isset($_POST['btnSubmit']))
{
  Your code....
}    
?>

您可以使用ajax提交表单数据。

http://www.formget.com/submit-form-using-ajax-php-and-jquery/