0
$name = trim(&$_POST['name']);

why trim this way would not work? I was following a tutorial, it put & sign before $_POST['name'] which I was not sure what is that for.. I got an error when it is $name = $_POST['name'];

this is the following lines of code

if($name && $email && $password && $cpassword){
        mysql_connect("127.0.0.1","root","") or die("Fail to connect to database");
        mysql_select_db("reglog");

        mysql_query("INSERT INTO users(username,email,password) VALUES('$name','$email','$cpassword')");
        echo 1;
}else{
    echo "not allowed"; 
}

by using $name = trim(&$_POST['name']);

I dont get the 1 value return to my ajax data

here is my js

$("#send").click(function(){
    var all = $("form").serialize();
    if(validateName() & validateEmail() & validatePass1() & validatePass2() == true){
        $.ajax({
            type: "post",
            url: "insert.php",
            data: all,
            success: function(data){
                if(data==1){
                    alert("success, you have registered");
                }else{
                    alert('Validate your form with correct informations to proceed!');
                }
            }
        });
    }
});
4

5 回答 5

0

尝试使用 empty() 检查变量集与否

$name = empty($_POST['name'])? '一些价值': trim(&$_POST['name']);

于 2013-09-13T02:43:53.333 回答
0

&PHP 中,用于通过引用传递给函数的参数,以便传递的值在函数外部发生变化,例如:

$whatever = 0;
function referWhat(&$arg){
  $arg = 10;
}
referWhat($whatever);

现在$whatever === 10

于 2013-09-13T02:43:21.853 回答
0

当您使用 时 $("form").serialize(),您会以如下格式创建字符串:

a=1&b=2&c=3&d=4&e=5&name=some_name

因此,要name进入 PHP 端,您需要执行以下操作:

$data = array();
parse_str($_POST, $data)
//do
print_r($data);// to see how you get the `POST` data

或使用$('form').serializeArray(),如

var all = $("form").serializeArray();

在 PHP 中

$name = $_POST['name'];
于 2013-09-13T02:55:29.243 回答
0

当你使用 $name = trim($_POST['name']); 时你得到了什么错误

语法很完美。

于 2013-09-13T02:39:32.633 回答
0

尝试并始终isset()首先使用以避免错误是一个好主意:

$name = isset($_POST['name']) ? trim($_POST['name']) : 'Some other value';

就我个人而言,我认为当你遇到错误时有点蹩脚,特别是如果$_POST['name'] 设置了。但是无所谓。;)

于 2013-09-13T02:39:43.287 回答