Submit And get response

Discussion in 'JavaScript' started by vOlLvEriNe, Sep 3, 2014.

  1. #1
    Hi all experts,
    Hi Have a form,
    <form action="" method="post">
    <input type="radio" name="sex" value="male">Male
    <input type="radio" name="sex" value="female">Female<br />
    
    <input type="radio" name="age" value="1">10-17
    <input type="radio" name="age" value="2">18-30<br />
    </form>
    HTML:
    Now what i want to do,
    when a user select sex and age both, then javascript submit form automatically and dynamically, and get response from a php file where submit information, please help me
     
    vOlLvEriNe, Sep 3, 2014 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    538
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Hi.
    There're already a lot of similar discussions somewhere inside Digital Point, have you search? I'd do it to something roughly like this:
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function mySubmitHandler(myForm){
      var
       sets = myForm.getElementsByTagName('fieldset'),
       readySetsCount = 0;
      for (var i = 0; i < sets.length; i++){
        var fields = sets[i].getElementsByTagName('input');
        for (var j = 0; j < fields.length; j++){
          if (fields[j].checked){
            readySetsCount++;
            break;
          }
        }
      }
    
      if (readySetsCount < 2){
        alert('form is not ready');
        return false;
      }
      else
        return true;
    }
    </script>
    </head>
    <body>
    
    <form action="" method="post" onsubmit="return mySubmitHandler(this)">
    <fieldset>
      <label>
        <input type="radio" name="sex" value="male">
        <span>Male</span>
      </label>
      <label>
      <input type="radio" name="sex" value="female">
      <span>Female</span>
    </fieldset>
    <fieldset>
      <label>
        <input type="radio" name="age" value="1">
        <span>10-17</span>
      </label>
      <label>
        <input type="radio" name="age" value="2">
        <span>18-30</span>
      </label>
    </fieldset>
    <input type="submit">
    </form>
    </body>
    </html>
    
    HTML:
     
    hdewantara, Sep 3, 2014 IP
  3. vOlLvEriNe

    vOlLvEriNe Member

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    No Dude, I Don't want to add submit button, as I mention, It submit auto when select sex and age will be select, and don't want to refresh the page
     
    vOlLvEriNe, Sep 3, 2014 IP
  4. Prasenjit Kumar

    Prasenjit Kumar Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #4
    Check out this
    you need to add jquery plugin and code like:
    <form action="" method="post" id="jit">
        <input type="radio" class="sex" name="sex" value="male">Male
        <input type="radio" class="sex" name="sex" value="female">Female<br />
       
        <input type="radio" class="age" name="age" value="1">10-17
        <input type="radio" class="age" name="age" value="2">18-30<br />
    </form>
    HTML:
    $( ".sex" ).on( "click", function() {
        if($( ".age:checked" ).length==1){
            $("#jit").submit();
        }
    });
    $( ".age" ).on( "click", function() {
      if($( ".sex:checked" ).length==1){
            $("#jit").submit();
        }
    });
    Code (JavaScript):
     
    Prasenjit Kumar, Sep 4, 2014 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Uhm, no. If he doesn't want to refresh the page, he needs to use some sort of AJAX-call - and display the result. Something like this:
    
    $("input[name=sex],input[name=age]").change(function() {
      if ($("input[name=sex]").val() != '' && $("input[name=age]").val() != '') {
         $.post('processform.php', serialize($('#formID')), function(data) { 
            alert(data); // returnvalue from processform.php
         });
      }
    }
    
    Code (markup):
    Something like that - not tested, might need some tweaking
     
    PoPSiCLe, Sep 5, 2014 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Is there some reason you really seem to want to piss all over accessibility and usability with scripttardery to avoid having a simple submit button? See, this is EXACTLY the type of "Gee ain't it neat" bullshit I usually am ranting and raving about what total nonsense it is.

    JS for nothing and your scripts for free, that ain't working, that's not how you do it...

    Of course it's then hardly a shock the drooling mouth-breathing jQuery re-re's and their idiotic bloated halfwit code bloat come pouring out of the woodwork... :(
     
    deathshadow, Sep 6, 2014 IP