用感谢信息替换表格

时间:2015-10-14 18:57:31

标签: php

我知道你在评论的是什么,“这是一个重复的答案” - 这不是完全正确的,我从高到低搜索到了如何做但我似乎无法找到任何东西。

问题是,如何用感谢信息替换表单?这是不同的,因为我的表单提交到一个php页面,其中包含sql连接并将表单DATA添加到MYSQL中的数据库。提交数据时,它会添加到数据库中。但它仍停留在php页面上。

继承我的代码:

<div class="index-contant">
		<div class="margin">
			<h2>GET A FREE QUOTE</h2>
			<div class="row" style="margin-left:auto !important; margin-right:auto !important; display:block !important;">
				<form name="contactForm" action="php/sql_insert.php" method="GET">
					<div class="col-sm-4 col-md-4">
						<input type="text" class="form-control" name="name" id="name" placeholder="*Name" id="name"required>
							<br>
						<input type="email" class="form-control" name="email" id="email" placeholder="*Email" id="email"required>
							<br>
						<input type="text" class="form-control" name="subject" id="subject" placeholder="*Subject" id="subject" required>
					</div>
					<div class="col-sm-8 col-md-8">
						<textarea class="form-control textarea-resize" rows="7" id="message" name="message" placeholder="*Message" required></textarea>
					</div>
						<p style="padding:0 !important; margin:0 !important"> &nbsp </p>
					<div style="margin-left:15px; margin-right:15px;">
						<input type="submit" class="button form-submit" value="SEND">
					</div>
				</form>
			</div>
		</div>
	</div>

<?php

include('sql_connect.php');

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];


$sql="INSERT INTO $tbl_name(name, email, subject, message)VALUES('$name', '$email', '$subject', '$message')";
$result=mysql_query($sql);
?> 

<?php 
// close connection 
mysql_close();
?>

我希望能够将数据发送到MySQL并保持在同一页面上。然后用感谢信息替换表单。我尝试过使用GET和POST,它们都没有让我在同一页面上。

1 个答案:

答案 0 :(得分:0)

index.php (刚刚举了一个index.php的例子。在这里更改你的名字)

<div class="index-contant">
    <div class="margin">
        <?
        if(!$_GET['SuccessMessage']=="Success")
        <?}?>
        <form>
            .
            .
            //Your Code
            .
            .
        </form>
        <?}
        else
        {?>
        <div class="alert alert-success" role="alert">
            Success
        </div>
        <?}?>
    </div>
</div>  

<强> sql_insert.php

<?php

include('sql_connect.php');

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
..
//Your Code
.
.
mysql_close();

header("location:index.php?SuccessMessage=Success"); 
//Put this in your last line to redirect it to previous page.
//I've used index.php, you write your page name
?>