duanditang2916 2017-06-03 03:43
浏览 27
已采纳

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

I have a form there are 5 fields where I want to automatic generate roll number in 5th field after filling up 4 fields(Name,Phone,Course,Batch) without submitting form.but after filling up 4 field no value comes in 5th field(roll).Below is my code

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条回答 默认 最新

  • douningqiu4991 2017-06-03 05:22
    关注
    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
    

    Function getUniqueRandomNo() { $roll=rand(100000,999999); // Check in db, for example in emp_id field $id = mysql_query("select emp_id from emp_master where emp_id = '$roll'"); If($id !=null) { return getUniqueRandomNo(); } else{

    Return $roll; } } echo getUniqueRandomNo(); ?> Note: I have written this recursive function code directly here, please do necessary update. There might be syntax error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dpg78570 2017-06-03 04:29
    关注

    Try This code:

    $(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>

    </div>
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 QT 实现 RSTP 语音对讲功能
  • ¥50 AES魔改之后的安全性关于PRF(相关搜索:密码学)
  • ¥15 用C语言写的一个程序遇到了两个问题第一是偏移正确但读取不到坐标,第二个问题是自己定义的函数实现不了获取指定进程模块。
  • ¥15 在安装Anaconda时总是闪退怎么办?
  • ¥15 对图中电路进行以下几个方面的分析
  • ¥15 对图中电路进行以下几个方面的分析
  • ¥15 对图中电路进行以下几个方面的分析
  • ¥15 对图中电路进行以下几个方面的分析
  • ¥500 抖音主页视频预存加载卡bug
  • ¥15 用汇编语言编写51单片机,定时闹钟程序