Form submit with AJAX passing form data to PHP without page refresh

后端 未结 10 1471
盖世英雄少女心
盖世英雄少女心 2020-11-22 06:07

Can anyone tell me why this bit of code isn\'t working?


  
    

        
相关标签:
10条回答
  • 2020-11-22 06:22

    JS Code

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/   libs/jquery/1.3.0/jquery.min.js">
    </script>
    
    <script type="text/javascript" >
      $(function() {
      $(".submit").click(function() {
      var time = $("#time").val();
      var date = $("#date").val();
      var dataString = 'time='+ time + '&date=' + date;
    
    if(time=='' || date=='')
    {
      $('.success').fadeOut(200).hide();
      $('.error').fadeOut(200).show();
    }
    else
    {
      $.ajax({
        type: "POST",
        url: "post.php",
        data: dataString,
        success: function(){
         $('.success').fadeIn(200).show();
         $('.error').fadeOut(200).hide();
        }
      });
    }
    return false;
    });
    });
    </script>
    

    HTML Form

       <form>
          <input id="time" value="00:00:00.00"><br>
          <input id="date" value="0000-00-00"><br>
          <input name="submit" type="button" value="Submit">
        </form>
    <span class="error" style="display:none"> Please Enter Valid Data</span>
    <span class="success" style="display:none"> Form Submitted Success</span>
    </div>
    

    PHP Code

    <?php
    if($_POST)
    {
    $date=$_POST['date'];
    $time=$_POST['time'];
    mysql_query("SQL insert statement.......");
    }else { }
    
    ?>
    

    Taken From Here

    0 讨论(0)
  • 2020-11-22 06:22
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript" >
      $(function () {
        $(".submit").click(function (event) {
          var time = $("#time").val();
          var date = $("#date").val();
          var dataString = 'time=' + time + '&date=' + date;
          console.log(dataString);
          if (time == '' || date == '')
          {
            $('.success').fadeOut(200).hide();
            $('.error').fadeOut(200).show();
          } else
          {
            $.ajax({
              type: "POST",
              url: "post.php",
              data: dataString,
              success: function (data) {
                $('.success').fadeIn(200).show();
                $('.error').fadeOut(200).hide();
                $("#data").html(data);
              }
            });
          }
          event.preventDefault();
        });
      });
    </script>
    <form action="post.php" method="POST">
      <input id="time" value=""><br>
      <input id="date" value=""><br>
      <input name="submit" type="button" value="Submit" class="submit">
    </form>
    <div id="data"></div>
    <span class="error" style="display:none"> Please Enter Valid Data</span>
    <span class="success" style="display:none"> Form Submitted Success</span>
    
    <?php
    
    print_r($_POST);
    if ($_POST['date']) {
      $date = $_POST['date'];
      $time = $_POST['time'];
      echo '<h1>' . $date . '---' . $time . '</h1>';
    }
    else {
    
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 06:23
    <html>
      <head>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script>
          $(function () {
            $('form').bind('click', function (event) {
            // using this page stop being refreshing 
            event.preventDefault();
    
              $.ajax({
                type: 'POST',
                url: 'post.php',
                data: $('form').serialize(),
                success: function () {
                  alert('form was submitted');
                }
              });
            });
          });
        </script>
      </head>
      <body>
        <form>
          <input name="time" value="00:00:00.00"><br>
          <input name="date" value="0000-00-00"><br>
          <input name="submit" type="submit" value="Submit">
        </form>
      </body>
    </html>
    

    PHP

    <?php
    
    if(isset($_POST["date"]) || isset($_POST["time"])) {
    $time="";
    $date="";
    if(isset($_POST['time'])){$time=$_POST['time']}
    if(isset($_POST['date'])){$date=$_POST['date']}
    
    echo $time."<br>";
    echo $date;
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 06:30

    The form is submitting after the ajax request.

    <html>
      <head>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script>
          $(function () {
    
            $('form').on('submit', function (e) {
    
              e.preventDefault();
    
              $.ajax({
                type: 'post',
                url: 'post.php',
                data: $('form').serialize(),
                success: function () {
                  alert('form was submitted');
                }
              });
    
            });
    
          });
        </script>
      </head>
      <body>
        <form>
          <input name="time" value="00:00:00.00"><br>
          <input name="date" value="0000-00-00"><br>
          <input name="submit" type="submit" value="Submit">
        </form>
      </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 06:30

    In event handling, pass the object of event to the function and then add statement i.e. event.preventDefault();

    This will pass data to webpage without refreshing it.

    0 讨论(0)
  • 2020-11-22 06:30
    <div class="container">
           <div class="row">
            <div class="col-md-3 col-sm-6 col-xs-12"></div>enter code here
              <div class="col-md-6 col-sm-6 col-xs-12">
              <div class="msg"></div>
                <form method="post" class="frm" id="form1" onsubmit="">
                   <div class="form-group">
                   <input type="text" class="form-control" name="fname" id="fname" placeholder="enter your first neme" required>
               <!--><span class="sp"><?php// echo $f_err;?></span><!-->
              </div>
            <div class="form-group">
                   <input type="text" class="form-control" name="lname" id="lname" placeholder="enter your last neme" required>
                  <!--><span class="sp"><?php// echo $l_err;?></span><!-->
                 </div>
                  <div class="form-group">
                   <input type="text" class="form-control" name="email" id="email" placeholder="enter your email Address" required>
                  <!--><span class="sp"><?php// echo $e_err;?></span><!-->
                 </div>
                 <div class="form-group">
                   <input type="number" class="form-control" name="mno" id="mno" placeholder="enter your mobile number" required>
                  <!--><span class="sp"><?php //echo $m_err;?></span><!-->
                 </div>
                  <div class="form-group">
                   <input type="password" class="form-control" name="pass" id="pass" pattern="(?=.*[a-z])(?=.*[A-Z]).{4,8}" placeholder="enter your Password" required>
                  <!--><span class="sp"><?php //echo $p_err;?></span><!-->
                 </div>
                  <div class="radio">
                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" value="male" name="gender" id="gender" checked>male<br>
                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" value="female" name="gender" id="gender">female<br>
                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" value="other" name="gender" id="gender">other<br>
                                             <!--><span class="sp"> <?php //echo $r_err;?></span><!-->
                 </div>
                    <div class="checkbox">
                      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="check" id="check" checked>I Agree Turms&Condition<br>
                       <!--><span class="sp"> <?php //echo $c_err;?></span><!-->
                     </div>
                     <input type="submit"  class="btn btn-warning" name="submit" id="submit">
                 </form>enter code here
    
              </div>
                   <div class="col-md-3 col-sm-6 col-xs-12"></div>
             </div> 
    
        </div>
    
    0 讨论(0)
提交回复
热议问题