在javascript中获取两个值输入

时间:2017-04-03 12:12:13

标签: javascript php jquery

我创造了一个"实时"检查器检查用户提供的输入以检查数据库上是否已有可用(例如用户名,电话号码)

输入框:

:before



javascript:

:after



最后是php文件:

<input type = "text" name = "user_username" placeholder="Username" onkeyup="chkstudidnumber(this.value)" autocomplete="off" required/><br/>
<div id = "msg"></div>



现在,我想在我的名字和姓氏输入框

上执行此操作
<script>
  function chkstudidnumber(val)
  {
      $.ajax ({
        type:"POST",
        url:"check_username.php",
        data:'username='+val,
        success: function(data){
          $("#msg").html(data);
        }
      });
  }
</script>

如何应用相同的javascript(或创建新的javascript)将两个输入合并为<?php include("dev_connection.php"); $g_username = htmlentities($_POST['username']); $stud_username = mysqli_escape_string($con,$g_username); $sql = "SELECT * FROM users WHERE username = '$stud_username' LIMIT 1 "; $result = mysqli_query($con,$sql); $count = mysqli_num_rows($result); if($count==1) { echo "<div id = 'msg_error'>"; echo "<img src = './img/img_error.png' style='height:20px;width:20px;vertical-align:middle;'> "; echo " &nbsp;&nbsp;that username is already registered<br/>"; echo " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if you forgot your password, please contact us"; echo "</div>"; } else { echo "<div id = 'msg'>"; echo "<img src = './img/img_check2.png' style='height:10px;width:10px;'> "; echo "username available"; echo "</div>"; } ?>

3 个答案:

答案 0 :(得分:1)

更改您的功能,以便它可以接受多于一个参数,如:

function chkstudidnumber(val1, val2)
{
    $.ajax ({
        type:"POST",
        url:"check_username.php",
        data: {
            'fname' : val1,
            'lname' : val2
        },
        success: function(data){
          $("#msg").html(data);
        }
    });
}

答案 1 :(得分:1)

在ajax $('form').serialize()函数中简单使用data来获取php页面上的所有表单输入,以便您可以拥有所有输入,例如用户名,名字,姓氏和所有。

试试这个:

<script>
  function chkstudidnumber(val)
  {
      $.ajax ({
        type:"POST",
        url:"check_username.php",
        data:$('form').serialize(),
        success: function(data){
          $("#msg").html(data);
        }
      });
  }
</script>

如果您在页面上有多个表单,那么您还可以向表单提供classid,并将其与serialize函数一起使用,仅发布该表单数据。

答案 2 :(得分:1)

<script>
  function chkstudidnumber()
  {
   var firstName = $("input[name=user_firstname]").val();
   var lastName = $("input[name=user_lastname]").val();
          $.ajax ({
        type:"POST",
        url:"check_username.php",
        data:'username='+firstName+'lastname='+lastName,
        success: function(data){
          $("#msg").html(data);
        }
      });
  }
</script>

<input type = "text" name = "user_firstname" placeholder="Firstname"autocomplete="off" required/>
<input type = "text" name = "user_lastname" placeholder="Lastname"autocomplete="off" required onkeyup="chkstudidnumber()" />