好的朋友我对这个基本的东西很困惑。我已经阅读了很多帖子,它说,我需要添加SELF POST或其他东西,但我不理解。
我有两个文件,index.html和submit.php。 index.html有一个带有提交按钮的表单,通过单击该表单,调用submit.php文件并显示一条消息" 1记录已添加"。我想从submit.php文件重定向到index.html文件。我不知道我做错了什么。是因为一个是html文件和另一个PHP?请帮忙。这是我的代码
index.html文件
<form method="post" action="submit.php">
submit.php文件
<?php
$con = mysql_connect("localhost","myuser","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$sql="INSERT INTO members (sName, sCity, sMobile, sEmail, sSub, sSlogan)
VALUES ('$_POST[name]', '$_POST[city]', '$_POST[mobile]', '$_POST[email]', '$_POST[sub]', '$_POST[slogan]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
</body>
</html>
修改
请查找index.html和submit.php文件的代码。在你的帮助下,两者都很完美。我仍在苦苦挣扎地放置验证码。我在html文件中使用html5输入类型,我不知道在submit.php文件中确切的验证位置。是的,我有多个表单,我按照你的建议制作了validations.php文件。我不理解的是,如果你在validations.php文件中有name字段的函数validate_name($ input),那你为什么要在submit.php中再次验证名字? (if(!empty($ _ POST [&#39; name&#39;]))。我也不明白错误消息的显示位置?如果我尝试添加这些功能,它会在单击提交时给出空白页面并且数据不会进入数据库。
你能否在submit.php文件中建议一个位置,我应该通过编辑我的submit.php文件来添加这些验证?
电子邮件的正则表达式('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'
)
Regexp for phone(^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$
)
这是我的index.html文件
<!DOCTYPE HTML>
<html>
<head>
<title>My Title</title>
</head>
<body>
<form method="post" action="submit.php">
<div class="box">
<div class="cl"><input type="text" name="name" placeholder="Name" /></div>
<div class="cl"><input type="text" name="city" placeholder="City" /></div>
<div class="cl"><input type="text" name="mobile" placeholder="Mobile" /></div>
<div class="cl"><input type="email" name="email" placeholder="Email" /></div>
<div class="cl"><input type="text" name="sub" placeholder="Want 3 m Free Subscription (Yes/No)?"></textarea></div>
<div class="cl"><input type="text" name="slogan" placeholder="Suggest a slogan for 6 m subscription"></textarea></div>
</div>
<div class="srow">
<div class="cl1">
<ul class="action">
<li><input type="submit" value="Submit" /></li>
</ul>
</div>
</div>
</form>
</body>
</html>
这是我从你那里获取的submit.php文件帮助
<?php
include 'config.php'; // store your configuration in a seperate file so
// you only need to update it once when your environment changes
$errors = false;
$output = '';
$nl = '<br>'.PHP_EOL;
$redirect_url = 'index.html';
if (!$con = new mysqli(DBHOST,DBUSER,DBPASS,DBNAME)){
$errors = true;
$output .= "ERROR Can't connect to DB".$nl;
};
if (!$errors){
//should validate/clean $_POST before using in query
$name = $con->escape_string($_POST['name']);
$city = $con->escape_string($_POST['city']);
$email = $con->escape_string($_POST['email']);
$mobile = $con->escape_string($_POST['mobile']);
$sub = $con->escape_string($_POST['sub']);
$slogan = $con->escape_string($_POST['slogan']);
$sql="INSERT INTO members
(sName, sCity, sMobile, sEmail, sSub, sSlogan)
VALUES ('$name', '$city', '$mobile', '$email',
'$sub','$slogan')";
if (!$con->query($sql)){ //forgot a parenthesis here earlier
$output .= 'ERROR: DB said: ('.$con->errno.') '.$con->error.$nl;
$output .= 'Query was:'.$sql.$nl;
$errors = true;
}else{
$output .= "1 record added".$nl;
}
}
if (!$errors){
//if there are no errors redirect to index.html;
header('refresh: 2; URL='.$redirect_url);
$output .= '...Redirecting...'.$nl;
}else{
//show the errors and allow display a link to go back/try again
$output .= '<a href="'.$redirect_url.'">Try again</a>'.$nl;
}
echo $output;
?>
PS:我观察到的一件事是html5(输入类型=&#34;电子邮件&#34;)在您转到下一个字段后立即显示无效的电子邮件警报。如何才能为所有领域做到这一点? (类似于现场失去焦点的验证检查)
由于
答案 0 :(得分:1)
您可以让提交脚本检查失败并重定向到index.html,以便在成功时添加更多内容。
请记住,在使用echo输出任何其他数据之前,您必须设置标题。
header('refresh: 3; URL=index.html');
不要使用mysql,使用mysql i 或PDO。
了解SQL注入。
所以你的sumbit.php可能看起来像:
<?php
include 'config.php'; // store your configuration in a seperate file so
// you only need to update it once when your environment changes
$errors = false;
$output = '';
$nl = '<br>'.PHP_EOL;
$redirect_url = 'index.html';
$con = new mysqli(DBHOST,DBUSER,DBPASS,DBNAME);
if ($con->connect_errno){
$errors = true;
$output .= "ERROR Can't connect to DB".$nl;
};
if (!$errors){
//should validate/clean $_POST before using in query
$name = $con->escape_string($_POST['name']);
$city = $con->escape_string($_POST['city']);
$email = $con->escape_string($_POST['email']);
$mobile = $con->escape_string($_POST['mobile']);
$sub = $con->escape_string($_POST['sub']);
$slogan = $con->escape_string($_POST['slogan']);
$sql="INSERT INTO members
(sName, sCity, sMobile, sEmail, sSub, sSlogan)
VALUES ('$name', '$city', '$mobile', '$email',
'$sub','$slogan')";
if (!$con->query($sql)){ //forgot a parenthesis here earlier
$output .= 'ERROR: DB said: ('.$con->errno.') '.$con->error.$nl;
$output .= 'Query was:'.$sql.$nl;
$errors = true;
}else{
$output .= "1 record added".$nl;
}
}
if (!$errors){
//if there are no errors redirect to index.html;
header('refresh: 2; URL='.$redirect_url);
$output .= '...Redirecting...'.$nl;
}else{
//show the errors and allow display a link to go back/try again
$output .= '<a href="'.$redirect_url.'">Try again</a>'.$nl;
}
echo $output;
config.php将包含
define('DBHOST','localhost');
define('DBUSER','myuser');
define('DBPASS','secretpass');
define('DBNAME','mydb');
编辑/补充:
如果您想进行一些验证,在客户端上执行某些操作会很有帮助,这样当您已经知道某些输入不符合要求时,您的用户就不必提交并被拒绝。
但是你还需要在服务器端进行验证(糟糕的用户可以通过在浏览器中编辑html来规避任何客户端验证)
为了帮助您的用户,您可以使用一些新的html5 input types可选,还可以使用一些额外的javascript:
例如<input type="email" name="email">
您的index.html可以保留为静态页面。它只是呈现输入表单,并可能加载一些javascript资源进行验证。
您的验证应该在submit.php中进行。如果您要在应用程序中添加更多表单,可以考虑将服务器端验证功能放在单独的validations.php中,以便将其包含在submit.php中
它可能包含以下功能:
function validate_name($input){
// fairly naive rule:
// upper and lower case latin characters and space
// at least three character long
// you may want to look at allowing other characters such as é ö etc.
$input = trim($input); //get rid of spaces at either end
if (preg_match('/^[a-zA-Z ]{3,}$/',$input) == 1){
return $input;
}else{
return false;
}
}
在你的submit.php中你可以拥有
...
include_once 'validations.php';
...
if (!empty($_POST['name'])){
if (!$name = $con->escape_string(validate_name($_POST['name'])){
$errors = true;
$output .= 'ERROR: Invalid Name: '.$_POST['name'].$nl;
}
}else{
$errors = true;
$output .= 'ERROR: No name specified'.$nl;
}
if (!empty($_POST['city']){
...
...
要使已输入的数据在发生故障时填充,您可以通过GET参数将数据发送回原始数据。
在submit.php中,接近结尾你可以添加类似......
if (!$errors){
//if there are no errors redirect to index.html;
header('refresh: 2; URL='.$redirect_url);
$output .= '...Redirecting...'.$nl;
}else{
//show the errors and allow display a link to go back/try again
//add parameters to show the data already entered
$redirect_url .= '?'.
http_build_query(
array('name'=>$name,
'city'=>$city,
'mobile'=>$mobile,
...
));
$output .= '<a href="'.$redirect_url.'">Try again</a>'.$nl;
}
echo $output;
并且在index.php中你必须阅读它们并在输入字段中设置它们的值。
<?php
//we'll use urldecode() so that any special characters will be interpreted correctly
if (!empty($_GET['name'])){
$name = urldecode($_GET['name']);
}else{
$name = '';
}
if (!empty($_GET['city'])){
$city = urldecode($_GET['city']);
}else{
$city = '';
}
....
?>
<input type="text" name="name" value="<?php echo $name; ?>"/>
<input type="text" name="city" value="<?php echo $city; ?>"/>
答案 1 :(得分:0)
当您提交表单时,浏览器将跳转表单标记的目标属性中指定的页面,在您的情况下它将< EM> submit.php
处理完POST数据后,您的提交页面应该呈现一些内容,重定向回 index.html ,替代方案:
如果您不想重新加载页面,则应使用AJAX将数据发送到服务器:
首先你应该尝试第一个,这很容易。
答案 2 :(得分:0)
index.html
<form method="post" action="submit.php">
//Html Codes
</form>
<强> submit.php 强>
<?php
$con = mysql_connect("localhost","myuser","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$sql="INSERT INTO members (sName, sCity, sMobile, sEmail, sSub, sSlogan)
VALUES ('$_POST[name]', '$_POST[city]', '$_POST[mobile]', '$_POST[email]', '$_POST[sub]', '$_POST[slogan]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else
{
header("location:index.html?Message=Success")
}
?>