在ajax php中没有提交表单的唯一ID

时间:2017-06-03 03:43:32

标签: javascript php jquery ajax

我有一个表格有5个字段,我想在填写4个字段(名称,电话,课程,批处理)后在第5个字段中自动生成卷号而不提交表单。但是在填写4个字段后没有值进来第5个字段(roll).Below是我的代码

的index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <head>
<title>Untitled Document</title>
<script>
    function my_validate_func() {
        if ($('#name').val() != "" && $('#phone').val() != "" &&
            $('#course').val() != "" && $('#center').val() != "") {
            $.ajax({
                type: "POST",
                url: 'submit.php',
                success: function(response) {
                    $('#roll').val(response.roll);
                }
            });
        }
    }
</script>
</head>

<body>
    <form method="post" action="">
        <input type="text" name="name" id="name" onchange="my_validate_func()">
        <input type="text" name="phone" id="phone" onchange="my_validate_func()">
        <input type="text" name="course" id="course" onchange="my_validate_func()">
        <input type="text" name="center" id="center" onchange="my_validate_func()">
        <input type="text" name="roll" id="roll" value="<?php $roll; ?>">
    </form>
</body>

</html>

**submit.php code is below**

<?php
$roll=rand(100000,999999);
echo $roll;
?>

2 个答案:

答案 0 :(得分:0)

尝试此代码:

$(document).ready(function(){
	//You can use keyup or change or etc.
  $('input.fields').change(function() {
    var check_empty = $('input.fields').filter(function(item) {
        return $.trim($(this).val()).length === 0;
    }).length === 0;
    
    if(check_empty === true){
    	//uncomment in rell test
    	/*$.ajax({
        type: "POST",
        url: 'submit.php',
        success: function(response) {
          $('#roll').val(response.roll);
        }
      });*/
      
      //for test
      $('#roll').val(Math.random());
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action="">
  <input type="text" name="name" id="name" class="fields" >
  <input type="text" name="phone" id="phone" class="fields">
  <input type="text" name="course" id="course" class="fields">
  <input type="text" name="center" id="center" class="fields">
  <input type="text" name="roll" id="roll" disabled>
</form>

答案 1 :(得分:0)

Corrected Code,

<!DOCTYPE> 
<head>
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type='text/javascript'>
$( document ).ready(function() {});
    function my_validate_func() {
        if ($('#name').val() != "" && $('#phone').val() != "" &&
            $('#course').val() != "" && $('#center').val() != "") {
            $.ajax({
                type: "POST",
                url: 'submit.php',
                success: function(response) {
                    $('#roll').val(response);
                }
            });
        }
    }
</script>
</head>

<body>
    <form method="post" action="">
        <input type="text" name="name" id="name" onchange="my_validate_func()">
        <input type="text" name="phone" id="phone" onchange="my_validate_func()">
        <input type="text" name="course" id="course" onchange="my_validate_func()">
        <input type="text" name="center" id="center" onchange="my_validate_func()">
        <input type="text" name="roll" id="roll" value="">
    </form>
</body>

</html>

**submit.php code is below**

<?php
$roll=rand(100000,999999);
echo $roll;
?>


** Updated submit.php code is below**

<?php

函数getUniqueRandomNo() {     $辊=兰特(100000,999999);     //签入db,例如在emp_id字段中 $ id = mysql_query(&#34;从emp_master中选择emp_id,其中emp_id =&#39; $ roll&#39;&#34;); 如果($ id!= null) { return getUniqueRandomNo(); } 否则{

返回$ roll; } }     echo getUniqueRandomNo();     ?&GT; 注意:我在这里直接写了这个递归函数代码,请做必要的更新。可能存在语法错误。