使用php

时间:2016-02-27 12:26:53

标签: javascript php jquery ajax

我试图从数据库中获取用户名是否可用的结果。但它没有给出任何结果我没有得到ajax响应这是html代码

<form id="user_form">
        <input placeholder="username here" type="text" name="ajax-data" id="ajax-data">
        <input type="submit" name="btnSubmit" id="btnSubmit" Value="Submit">
    </form>
    <span class="php_responce_here"></span>

这是我用过的ajax代码

     $(document).ready(function()
         {
    $("form#user_form").click(function()
    {
        var textboxvalue = $('input[name=ajax-data]').val();

        $.ajax(
        {
            type: "POST",
            url: 'second.php',
            data: {ajax-data: textboxvalue},
            success: function(result)
            {
                $(".php_responce_here").html(result);
            }
        });
    });
});​
                </script>

php的最终代码,其中我使用了验证和查询来查找用户名是否在数据库中可用,问题是它没有给出任何结果

         <?php
error_reporting(0); 
    require "config.php";// configuration file holds the database info

    $user_name = $_POST['ajax-data']; // textbox in the html


    if($user_name)
    {
        $usernamecheck=  mysql_query("SELECT count(*) FROM users WHERE username='$user_name'");
        $check=  mysql_fetch_row($usernamecheck);
        if($check[0]==0)
        {
            if($user_name!=""){
                if(strlen($user_name)>25){
                    echo "You have reached the maximum limit";
                }
                else{
                    echo "User name is valid";
                 }
            }
            else
            {
                echo "username is empty";   
            }
        }
        else{
           echo "Username Already Taken";
       }
    }

?>

4 个答案:

答案 0 :(得分:0)

应提交事件而不是点击:

JButton

正如@Cyril BOGNOU指出的那样;

$("form#user_form").submit(function(e) {
    e.preventDefault();

    var textboxvalue = $('input[name=ajax-data]').val();

    $.ajax(
    {
        type: "POST",
        url: 'second.php',
        data: { "ajax-data": textboxvalue },
        success: function(result) {
            $(".php_responce_here").html(result);
        }
    });
});

答案 1 :(得分:0)

如果要返回JSON(例如

),您还应该添加要与参数一起返回的数据类型
  

dataType:'JSON',

是的我觉得你应该写得更好

  

数据:{“ajax-data”:textboxvalue}

所以更新应该是

$(document).ready(function()
{
    $("form#user_form").click(function()
    {
    var textboxvalue = $('input[name=ajax-data]').val();

    $.ajax(
            {
                type: "POST",
                url: 'second.php',
                dataType: 'JSON',
                data: {"ajax-data": textboxvalue},
                success: function(result)
                {
                    $(".php_responce_here").html(result.message);
                }
        });
    });
});

并从PHP脚本返回json字符串

<?php

error_reporting(0);
require "config.php"; // configuration file holds the database info

$user_name = $_POST['ajax-data']; // textbox in the html


if ($user_name) {
    $usernamecheck = mysql_query("SELECT count(*) FROM users WHERE username='$user_name'");
    $check = mysql_fetch_row($usernamecheck);
    if ($check[0] == 0) {
        if ($user_name != "") {
            if (strlen($user_name) > 25) {
                $message = "You have reached the maximum limit";
            } else {
                $message = "User name is valid";
            }
        } else {
            $message = "username is empty";
        }
    } else {
        $message = "Username Already Taken";
    }
    echo json_encode(["message" => $message]);
}

?>

答案 2 :(得分:0)

空页?什么都没打印出来?

<?php
  error_reporting(-1);
  ini_set('display_errors', 1);

  require "config.php";// configuration file holds the database info  

  if(isset($username = $_POST['ajax-data'])){
    if($l = strlen($username) <= 25 && $l > 2){
      $sql = "SELECT * FROM users WHERE username='$username'"; // wide open for SQL injections. use mysqli or PDO instead.
      if($rsl = mysql_query($sql) != false){ // ALWAYS verify if your query's ran successfully.
        if(mysql_num_rows($rsl) != 0){
          echo 'Username already exists';
        } else {
          echo 'Username is available';
        }
      } else {
        echo 'Query failed: ' . mysql_error();
      }
    } else {
      echo $l > 25 ? 'Reached limit' : 'Needs to be longer';
    }
  } else {
    echo "post['ajax-data'] not set<\br>";
    print_r($_POST);
  }

?>

然后有你的Javascript代码,我有问题。但是你有一个提交按钮,但你想检查它是否有效?

$(document).ready(function(){
  $("#user_form").submit(function(event){
    event.preventDefault();

    $.ajax({
      url: "second.php",
      type: "post",
      data: $(this).serialize(),
      success: function(result){
        $(".php_responce_here").html(result);
      }
    });
  });
});​ 

答案 3 :(得分:0)

注意:不推荐使用mysql。您应该使用mysqliPDO

您的代码中存在一些错误。检查以下代码。它应该工作。

<script>
    $(document).ready(function () {
        $("form").submit(function (event) {
            var textboxvalue = $("#ajax-data").val();
            $.ajax({
                data: {ajaxdata: textboxvalue},
                type: "POST",
                url: 'second.php',
                success: function (result)
                {
                    $(".php_responce_here").html(result);
                }
            });
            return false;
        });
    });
</script>

您无法使用ajax-data创建变量-

PHP

$usernamecheck = mysql_query("SELECT * FROM users WHERE username='$user_name'");
$check = mysql_num_rows($usernamecheck);

您应该使用mysql_num_rows代替mysql_fetch_row。它会自动计算行数。

检查工作example