简体   繁体   中英

how to submit the form without refreshing the page

this is my html code when i click on submit button it displays alert and then when i open the retrieveform.php it does not show me the value please solve this mystery

   <html>
   <head>
   <script type="text/javascript" src="jquery.js"></script>
   <script>
    $(function () {

    $('form').on('submit', function (e) {

      e.preventDefault();

      $.ajax({
        type: 'post',
        url: 'retrieveform.php',
        data: $('form').serialize(),
        success: function () {
          alert('helllo');
        }
      });

    });

  });
 </script>
 </head>
 <body>
 <form>
 <input type="text"name="t1">
 <input type="submit"name="s1"value="submit">
 </form>
 </body>
 </html>

and my retrieveform.php is

 <?php
 if($_POST['t1'])
 {
 echo $_POST['t1'];
 }
 else
 {
 echo "hekllojjio";
 }
 ?>

Is it possible that the element "t1" is present since it's part of your form, but empty or just empty characters .. perhaps check if this is the case?

<?php
    if($_POST['t1'] && !empty($_POST['t1'])) {
        echo "You submitted t1 of: ".$_POST['t1'];
    } else {
        echo "t1 is empty";
    }
?>

Also of note is you may want to use something like firebug to view the ajax page loaded and its results. The echo statement on the ajax loaded page will not display on the parent page unless you set the contents of the response into the page:

<html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $(function () {
                $('.form').on('submit', function (e) {
                    tValue = $(this).find(input[type=text]:first).val();
                    e.preventDefault();
                    $.ajax({
                        type: 'post',
                        url: 'retrieveform.php',
                        data: $('form').serialize(),
                        success: function (response) {
                            alert('helllo');
                            $('#placeholder').html(response);
                            $('#placeholder2').html(tValue);
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <?PHP for ($i = 1; $i <= 10; $i++) { ?>
        <form class="form" name="form_<?PHP echo $i; ?>" id="form_<?PHP echo $i;?>">
            <input type="text" id="t<?PHP echo $i; ?>" name="t<?PHP echo $i; ?>">
            <input type="submit" name="s<?PHP echo $i; ?>" value="submit">
        </form>
        <?PHP } ?>
        <div id="placeholder"></div>
        <div id="placeholder2"></div>
    </body>
</html>

with this your ajax loaded page's echo will be put on the page in the "placeholder" div.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM