douyan2470 2012-02-29 16:56
浏览 40
已采纳

是否可以在提交表单并刷新页面之前使用PHP验证?

I have created a user registration form using PHP, a password must be entered and entered again to check they both match. I have my conditional checks which are run when the form is submitted, if they fail, the form is reloaded empty for the user to fill in. I wanted to do a real time check against the password, and repeatPassword fields, so if they were both entered and did not match a warning displayed instantly, not after the whole form was filled in and submitted.

I have tried doing this using only PHP, I was using JavaScript before but I want to do it all in PHP as I think it will be better and more reliable.

Here is the code I have for my form checking condition:

$firstname=$_POST['fName'];
$surname=$_POST['sName'];
$email=$_POST['emailad'];
$password= md5($_POST['password']);
$repeatpassword= md5($_POST['repeatPassword']);
$secretquestion=$_POST['sQuestion'];
$secretanswer=$_POST['sAnswer'];
$address1=$_POST['address1'];
$address2=$_POST['address2'];
$address3=$_POST['address3'];
$address4=$_POST['address4'];
$city=$_POST['city'];
$postcode=$_POST['postcode'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];

if(!empty($password)
&& !empty($repeatpassword)
&& !empty($firstname)
&& !empty($surname)
&& !empty($email)
&& ($repeatpassword == $password)
&& !empty($secretquestion)
&& !empty($secretanswer)
&& !empty($address1)
&& !empty($city)
&& !empty($postcode)
&& !empty($phone))
 {


$reg =true;
$sql = "INSERT INTO cryptuser (firstname,surname,email,password, secretq, secreta,
                               address1,address2,address3,address4,city,postcode,phone,mobile)
        VALUES     ('$firstname','$surname','$email','$password','$secretquestion','$secretquestion',
            '$address1','$address2','$address3','$address4','$city','$postcode','$phone','$mobile')"; 

I had code written which I deleted because it was definitely wrong, but I was trying to perform a PHP function, if the password and repeatPassword fields are not empty, check both values are the same, if they are do nothing, else display warning text.

I felt this would work, but I am still trying to get used to php being a server side language.

Any advice would be appreciated.

  • 写回答

3条回答 默认 最新

  • douling0053 2012-02-29 17:04
    关注

    First off, it won't be "better and more reliable" in PHP than in Javascript. Both languages are Turing-complete and there's little you can do in one realm you can't do in the other. You're only limited by your ability as a coder.

    That said, if you want to do the validation in PHP before the form is submitted, you're going to have to get the data to the server somehow. Of course, that process is much like submitting a form...

    One means would be to have JS which scrapes the information from the form and makes an AJAX request to the server. If the server says "OK", then the JS sets the client location to the new page (since the server already received all the contents as part of the validation process, you shouldn't need to POST them again).

    Moving on, your validation code as you've written it above is a very bad idea. See this comic for a fast-and-decent explanation why ( http://bobby-tables.com appears to be down) - imagine what the SQL query would look like if you had that as $_POST['fName'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误