遇到问题从PDO的结果发送给阿贾克斯发送给、结果、问题、阿贾克斯

由网友(Lovingd(噬心人))分享简介:我想要做的是,如果用户成功注册了PDO将提供一个信息,并将其发送到AJAX和AJAX会消息,如果用户注册或没有。它工作正常,我把这个情况在我的PDO后,现在它不会插入不多和Ajax告诉错误注册用户!所有的时间。脚本:<脚本类型=文/ JavaScript的>$(文件)。就绪(函数(){$('#提交)。点击...

我想要做的是,如果用户成功注册了PDO将提供一个信息,并将其发送到AJAX和AJAX会消息,如果用户注册或没有。它工作正常,我把这个情况在我的PDO后,现在它不会插入不多和Ajax告诉错误注册用户!所有的时间。

脚本:

 <脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){

   $('#提交)。点击(函数(五){
       即preventDefault();

        VAR数据= {};
        。data.name = $('#姓名)VAL();
        data.age = $('#时代)VAL()。
        data.gender = $('#性别)VAL()。
        data.address = $('#地址)VAL()。
        。data.image = $('#imgInp)VAL();


        $阿贾克斯({
            键入:POST,
            网址:user.php的,
            数据:数据,
            缓存:假的,
            成功:函数(响应){
            如果(数(响应)== 1)
                {
                警报(用户注册成功);
                }
                      其他
                {
                警报(错误注册用户!);
                }
            }
        });
            返回false;
    });

});
< / SCRIPT>
 

user.php的:

 < PHP
$主机=localhost的;
$用户=根;
$传球=;
$ DB =测试;

$ DBC =新PDO(mysql的:主机=。$主机,数据库名=$分贝,$用户,$通行证);
$ dbc->的setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);

$ NAME = @ $ _ POST ['名称'];
$年龄= @ $ _ POST ['年龄'];
$地址= @ $ _ POST ['地址'];
$性别= @ $ _ POST ['性别'];
$ imageName = @ $ _ FILES ['形象'] ['名称'];


 $ Q =INSERT INTO学生(姓名,年龄,地址,性别,imageName)VALUES(:姓名,年龄,:地址:性别:图像);

    $查询= $ dbc-> prepare($ Q);
    $查询 - > bindParam(':名称',$名);
    $查询 - > bindParam(':年龄',$年龄);
    $查询 - > bindParam(':地址',$地址);
    $查询 - > bindParam(':性别',$性别);
    $查询 - > bindParam(':形象',$ imageName);


    $结果= $查询 - >执行();
    $结果?回声1; :回声2; ;
?>
 

解决方案

看来你有错误的:

  $结果?回声1; :回声2; ;
 
C罗连续八年欧冠四强被终结,会影响他跟梅西的地位之争吗

你演示

尝试这样的:

 回声$结果? 1:2;
 

工作演示

您可以看到这里的教程。

I want to do is if the user successfully registered the pdo will provide an information and send it to ajax and the ajax will message if the user is registered or not. It was working properly after i put this condition in my pdo and now it wont insert no more and ajax tells "error registering user!" all the time.

script:

<script type="text/javascript">
$(document).ready(function() {

   $('#submit').click(function (e) {
       e.preventDefault();

        var data = {};
        data.name = $('#name').val();
        data.age = $('#age').val();
        data.gender = $('#gender').val();
        data.address = $('#address').val();
        data.image = $('#imgInp').val();


        $.ajax({
            type: "POST",
            url: "user.php",
            data: data,
            cache: false,
            success: function (response) {
            if (Number(response) == 1)
                {
                alert("User successfully registered");
                }
                      else
                {
                alert("Error registering user!");
                }
            }
        });
            return false;
    });

});
</script>

user.php:

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";

$dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$name = @$_POST['name'];
$age = @$_POST['age'];
$address = @$_POST['address'];
$gender = @$_POST['gender'];
$imageName = @$_FILES['image']['name'];


 $q = "INSERT INTO students(name, age, address, gender, imageName ) VALUES(:name, :age, :address, :gender, :image)";

    $query = $dbc->prepare($q);
    $query->bindParam(':name', $name);
    $query->bindParam(':age', $age);
    $query->bindParam(':address', $address);
    $query->bindParam(':gender', $gender);
    $query->bindParam(':image', $imageName);


    $results = $query->execute();
    $results ? echo "1"; : echo "2"; ;
?>

解决方案

It seems that you have error in :

$results ? echo "1"; : echo "2"; ;

yours demo

try like this :

echo  $results ? "1" : "2";

working demo

you can see here a tutorial.

阅读全文

相关推荐

最新文章