dougu7546 2014-09-15 08:18
浏览 57
已采纳

如果span id包含包含特定文本的php数据

First time posting but this script has got me completely stuck...

Ill explain a few things first

I need to create a click function on a register button that when clicked it checks the span id e.g $("#username_results) for php return data that has specific text e.g " Username in use.". Then do a function else do something else.

So here is the HTML part i have cropped out the non relevant parts

          <td width="168"><div align="right">Username :</div></td>
          <td width="144"><input name="username" type="text" id="username" form="registration"></td>
          <td width="162" height="16"><div align="left"><span class="error_text" id="username_results"></span></div></td>

and here is the JQuery

    $("#username").keyup(function(username) {

    $(this).val($(this).val().replace(/\s/g, ''));

    var username = $(this).val();

    if(username.length < 4){

        $("#username_results").css({"color":"red"});    
        $("#username_results").html('<img src="images/not_available.png" />' + " Invalid username.")
        }

    if(username.length >= 4) {

        $("#username_results").html('<img src="images/loader.gif" />');
        $.post('scripts/ajax_registration.php', {'username':username}, function(username_data) {
        $("#username_results").html(username_data);
        });
    }
});

and here is the PHP

include 'sql_connection.php';

if(isset($_POST["username"]))

{

if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !='xmlhttprequest') {
    die();
}

$username =  strtolower(trim($_POST["username"])); 

$username = filter_var($username, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);

$results = mysqli_query($sql_connection,"SELECT id FROM members WHERE name='$username'");

$username_exist = mysqli_num_rows($results);

if($username_exist) {
    echo ('<script>$("#username_results").css({"color":"red"})</script>');
    echo ('<img src="images/not_available.png" />');
    echo (" Username in use.");
}else{
    echo ('<script>$("#username_results").css({"color":"green"})</script>');
    echo ('<img src="images/available.png" />');
    echo (" Username available.");
}

mysqli_close($sql_connection);

}

so what i was trying was

$("#register").click(function(e) {

    var username_results = $("#username_results").val();

    if (username_results.indexOf("Username") > 0) {

    alert("YAY ITS WORKING")    

    }

});

and this was not returning any alerts unless i took the > 0 out then it would show the alert at all times even when nothing was displayed in that span.

Any help would be appreciated. Thank you

  • 写回答

1条回答 默认 最新

  • dpdp42233 2014-09-15 08:31
    关注

    You need to use

    $("#username_results").text()
    

    <span> doesn't have value that's why it's returning nothing to you. I've made JSBin for you: http://jsbin.com/renivuroziqi/2/edit

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题