PHP,用于注册表单的AJAX
我正在尝试建立我的第一个PHP网站,我真的很想用正确的方式。我正在处理位于以下位置的表单: 我已建立以下表格:PHP,用于注册表单的AJAX,php,mysql,ajax,Php,Mysql,Ajax,我正在尝试建立我的第一个PHP网站,我真的很想用正确的方式。我正在处理位于以下位置的表单: 我已建立以下表格: <p><form action="/signup/register.php" method="post"> <label for="first_name">First Name</label> <input type="text
<p><form action="/signup/register.php" method="post">
<label for="first_name">First Name</label>
<input type="text" name="first_name" />
<label for="last_name">Last Name</label>
<input type="text" name="last_name" />
<label for="company">Company</label>
<input type="text" name="company" />
<label for="job_title">Job Title</label>
<input type="text" name="job_title" />
<label for="phone">Phone</label>
<input type="text" name="phone" />
<label for="email">Email</label>
<input type="text" name="email" />
<label for="username">Choose a Username</label>
<input type="text" name="username" />
<label for="password">Choose a Password</label>
<input type="text" name="password" />
<label for="confirm_password">Confirm Your Password</label>
<input type="text" name="confirm_password" />
<input type="submit" value="Get Started" />
</form>
名字
姓
单位
职位名称
电话
电子邮件
选择一个用户名
选择一个密码
确认您的密码
这是我的PHP页面register.PHP:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$con = mysql_connect("localhost","*******","******"); //Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bwgblog", $con); //Replace with your MySQL DB Name
$first_name=mysql_real_escape_string($_POST['first_name']);
$last_name=mysql_real_escape_string($_POST['last_name']);
$company=mysql_real_escape_string($_POST['company']);
$job_title=mysql_real_escape_string($_POST['job_title']);
$phone=mysql_real_escape_string($_POST['phone']);
$email=mysql_real_escape_string($_POST['email']);
$username=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$confirm_password=mysql_real_escape_string($_POST['confirm_password']);
$sql="INSERT INTO members (first_name,last_name,company,job_title,phone,email,username,password,confirm_password) VALUES ('$first_name','$last_name','$company','$job_title','$phone','$email','$username','$password','$confirm_password')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";
mysql_close($con);
?>
AJAX不要求后端有任何特殊的功能,因此最简单的解决方案可能是使用一个“usercheck.php”文件来查询数据库中传递的用户名,然后返回某种形式的true/false。您可能希望使用JSON进行回复(如果您有PHP5,这很容易-请参阅JSON_encode)
关于AJAX前端,如果您使用现有的框架(我使用了Mochikit和prototype,两者似乎都很好),您会发现这是最简单的,其中有几个框架。这应该允许您轻松加载服务器的响应
如果AJAX使用GET而不是POST(这更简单),那么您可以通过使用适当的查询字符串查看页面来测试响应。在任何情况下,使用将允许您实时查看通话
2) 无需使用密码检查AJAX—这可以简单地使用普通JavaScript完成:只需比较两个输入的.value属性。1)AJAX不要求后端有任何特殊功能—因此最简单的解决方案可能是使用“usercheck.php”文件查询数据库中传递的用户名,然后返回某种形式的true/false。您可能希望使用JSON进行回复(如果您有PHP5,这很容易-请参阅JSON_encode)
关于AJAX前端,如果您使用现有的框架(我使用了Mochikit和prototype,两者似乎都很好),您会发现这是最简单的,其中有几个框架。这应该允许您轻松加载服务器的响应
如果AJAX使用GET而不是POST(这更简单),那么您可以通过使用适当的查询字符串查看页面来测试响应。在任何情况下,使用将允许您实时查看通话
2) 无需使用AJAX进行密码检查-这只需使用普通JavaScript即可完成:只需比较两个输入的.value属性。同意PeterJCLaw对除JavaScript框架选择之外的所有帐户的意见。以下是使用jQuery的方法:
// give the form an ID to use a better selector: ie: $('#myform')
// intercept form submit
$('form').submit(function(){
// check if passwords match; you might want to do more thorough validation
if($('input[name=password]').val()==$('input[name=confirm_password]').val()){
// make ajax post request and store the response in "response" variable
$.post('/signup/register.php', $(this).serialize(), function(response){
// process response here (assume JSON object has boolean property "ok"
if(response.ok==true){
// sweet, it worked!
alert('OK!');
}else{
// handle error
alert('Ooops');
}
}, 'json');
// stop the form from being submitted
return false;
}else{
// for the sake of simplicity
alert('Passwords don't match!);
}
});
同意PeterJCLaw关于除javascript框架选择之外的所有账户的意见。以下是使用jQuery的方法:
// give the form an ID to use a better selector: ie: $('#myform')
// intercept form submit
$('form').submit(function(){
// check if passwords match; you might want to do more thorough validation
if($('input[name=password]').val()==$('input[name=confirm_password]').val()){
// make ajax post request and store the response in "response" variable
$.post('/signup/register.php', $(this).serialize(), function(response){
// process response here (assume JSON object has boolean property "ok"
if(response.ok==true){
// sweet, it worked!
alert('OK!');
}else{
// handle error
alert('Ooops');
}
}, 'json');
// stop the form from being submitted
return false;
}else{
// for the sake of simplicity
alert('Passwords don't match!);
}
});
看看Jquery的扩展
这将简化所有这一切。检查远程值也很简单
您可以将Jquery上传到您的服务器,也可以由google代码托管它们。使用谷歌版本大大增加了客户也已经下载并可以使用其缓存副本的可能性。看看Jquery的扩展
$fields = array('first_name','last_name','company','job_title','phone','email','username','password','confirm_password');
$dbfields = array(); $dbdata = array(); $dbfieldq = array(); $types = ''; //Setting Variable
foreach ($fields as $field){ //For Each Field
if (!isset($_POST[$field]){ header('Location: signup.php'); die('Please Fill in all fields, they are required'); } //Missing Field Error -- Doublecheck on serverside
array_push($dbdata, strip_tags($_POST[$field])); //Add Data - MySQLi Prepared Statements don't need to be escaped
array_push($dbfields,$field); //Add a field
array_push($dbfieldq,'?'); //Add a ?
$types += 's'; //Add a field type (string for all of these)
}
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db'); //Connect
if ($mysqli->connect_error) { //If there is a connect Error
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$names = explode($dbfields); //Explode the Field Names
$questions = explode($dbfieldq); //Explode the ?
$stmt = $mysqli->prepare("INSERT INTO DBName ($names) VALUES ($questions)");
$params = $this->paramValues;
array_unshift($dbdata, implode($this->paramTypes);
call_user_func_array( array( $stmt, 'bind_param' ), $params);
$stmt->bind_param($types, $code, $language, $official, $percent);
$stmt->execute();
$mysqli->close();
这将简化所有这一切。检查远程值也很简单
您可以将Jquery上传到您的服务器,也可以由google代码托管它们。使用谷歌版本大大增加了客户已经下载并可以使用其缓存副本的可能性
$fields = array('first_name','last_name','company','job_title','phone','email','username','password','confirm_password');
$dbfields = array(); $dbdata = array(); $dbfieldq = array(); $types = ''; //Setting Variable
foreach ($fields as $field){ //For Each Field
if (!isset($_POST[$field]){ header('Location: signup.php'); die('Please Fill in all fields, they are required'); } //Missing Field Error -- Doublecheck on serverside
array_push($dbdata, strip_tags($_POST[$field])); //Add Data - MySQLi Prepared Statements don't need to be escaped
array_push($dbfields,$field); //Add a field
array_push($dbfieldq,'?'); //Add a ?
$types += 's'; //Add a field type (string for all of these)
}
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db'); //Connect
if ($mysqli->connect_error) { //If there is a connect Error
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$names = explode($dbfields); //Explode the Field Names
$questions = explode($dbfieldq); //Explode the ?
$stmt = $mysqli->prepare("INSERT INTO DBName ($names) VALUES ($questions)");
$params = $this->paramValues;
array_unshift($dbdata, implode($this->paramTypes);
call_user_func_array( array( $stmt, 'bind_param' ), $params);
$stmt->bind_param($types, $code, $language, $official, $percent);
$stmt->execute();
$mysqli->close();
一个更好的方法来做php。。。使用准备好的语句和循环来准备变量
一个更好的方法来做php。。。使用准备好的语句和循环来准备变量。你知道我应该使用哪种JavaScript吗?谢谢你的上述建议。你可以看出我是新来的,所以谢谢你慢慢来。另外,当你提到Mochikit、Prototype和Firebug时,我是否会下载它们并将它们上传到我的服务器上?Firebug是一个firefox插件。Prototype是一个JS框架,是的,你可以下载它并把它放在你的服务器上。你知道我应该使用什么JavaScript吗?谢谢你的上述建议。你可以看出我是新来的,所以谢谢你慢慢来。另外,当你提到Mochikit、Prototype和Firebug时,我是否会下载它们并将它们上传到我的服务器上?Firebug是一个firefox插件。Prototype是一个JS框架,是的,您可以下载它并将其放在服务器上。当然可以,但是为这个简单的功能添加整个插件的开销真的有必要吗?您可能需要使用alert()以外的其他方法来警告,并且您应该在最后的警报中检查字符串的转义。我没有使用jQuery,所以我不确定-你的匿名函数似乎没有返回任何东西-表单不会被提交吗?当然,但是为这个简单的功能添加整个插件的开销真的有必要吗?你可能想使用alert()以外的东西来警告,您应该检查最后一个警报中字符串的转义。我没有使用jQuery,所以我不确定-您的匿名函数似乎没有返回任何内容-表单不会被提交吗?