FIX- Php表单提交警报重定向到其他页面

时间:2014-11-01 08:26:56

标签: php html

我已经为表单编写了一个代码,在提交时会在浏览器中打开demo.php标记。我希望表单在同一页面上生成(谢谢)警报,而不重定向到demo.php。是否可以嵌入一个jquery notificaiton插件?如果是,那么如何?

<?php
error_reporting(0);

define('DB_NAME', 'form');
define('DB_USER', '******');
define('DB_PASSWORD', '*******');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
     die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

echo "<script type='text/javascript'>alert('It worked!')</script>";
exit();


$value = $_POST['name'];
$value2 = $_POST['email'];
$value3 = $_POST['bio'];

$sql = "INSERT INTO form (name, email, bio) VALUES ('$value', '$value2','$value3')";

if (!mysql_query($sql))  
{
       die('Error: ' . mysql_error());
}


mysql_close();

?>

<form method="post" action="demo.php" id="form"  >			   
				
			<center><legend><h1> Enter Your Details </h1></legend>

            <div class="formelement">
                <label for="name">Name: </label>
                <input type="text" name="name" id="name" required/>
            </div>
 
            <div class="formelement">
                <label for="email">Email-ID: </label>
                <input type="email" name="email" id="email" required/>
            </div>
			
			<div class="formelement">
			    <label for="bio">Your Message: </label>
				<textarea rows="1" cols="15" name="bio" id="bio"></textarea>
			</div>
			
			<div class="formelement">
                <input type="submit" value="SUBMIT" class="submit"/>
            </div>


</form>

1 个答案:

答案 0 :(得分:0)

尝试更改

<form method="post" action="demo.php" id="form"  >

<form method="post" action="" id="form"  >

它会将表单发布到同一个文件,因此不要重定向到其他页面

并在里面打印警报 -

    $db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
echo "<script type='text/javascript'>alert('It worked!')</script>";
$value = $_POST['name'];
$value2 = $_POST['email'];
$value3 = $_POST['bio'];

$sql = "INSERT INTO form (name, email, bio) VALUES ('$value', '$value2','$value3')";

$result=mysql_query($sql) or die('Error: ' . mysql_error());

if ($result)  
{
echo "<script type='text/javascript'>alert('success!')</script>";
}
mysql_close();

您可以使用jquery ajax表单提交.so您可以使用jquery

发出警报

see here