0

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;
?>
ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110
Doni
  • 11
  • 2
  • 8
  • Why does the $roll value get displayed in the input before it is declared? Do you want users to be able to manipulate it? Would you rather use ``? – mickmackusa Jun 03 '17 at 03:46
  • i do it in hidden field, but still not generate – Doni Jun 03 '17 at 03:47
  • 2
    I fear this is going down a duplicate question path. Be sure to echo your `$roll` into your html and continue searching SO, starting with: https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php Please consider removing your question and only post after you have spent sufficient time trying to self solve. – mickmackusa Jun 03 '17 at 04:05
  • remove `` and change `$('#roll').val(response.roll);` to `$('#roll').val(response);`, if the roll in php isn't saved anywhere you could generate random number in javascript as well – Kazz Jun 03 '17 at 04:27

2 Answers2

0

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>
Ali Hesari
  • 1,821
  • 5
  • 25
  • 51
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

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.

Sonal Khunt
  • 1,876
  • 12
  • 20
  • if i generate unique id through rand function like that ,is there any possibility to generate duplicate value? – Doni Jun 03 '17 at 13:27
  • Yes if database is big and function already created a big number of series there is chance of duplicacy. To remove this possibility append timestamp value. Timestamp value can never be repeatative. Have you got to resolve issue by my code? If yes Mark is positive so other can also get benefit if encounter with same problem. Thanks – Sonal Khunt Jun 03 '17 at 15:51
  • suppose i want to insert rand function value in mysql database,then how can i check that this random generated value is already in our database or not ? – Doni Jun 04 '17 at 05:34
  • Thanks Doni. If you ready to use timestamp then no need to check as it will never be duplicated. Else you need to write recursive function in place of that 2 line. Function​ will keep checking in db till you get unique no. Something like I have added in my answer now. – Sonal Khunt Jun 04 '17 at 08:35
  • can you tell one things? suppose i want to insert rand function value in mysql database, how can i check through timestamp. actually i use simple query like INSERT INTO student (`roll`) values ($_POST['rand']); here how can i use timestamp for checking duplicating value exist or not. – Doni Jun 05 '17 at 04:42
  • can you answer this question? actually radio button does not work ,please check this link. if possible to answer. https://stackoverflow.com/questions/44357769/radio-button-validate-in-html5/44357912#44357912 – Doni Jun 05 '17 at 04:49