简体   繁体   中英

jquery auto form submission issue

i have a simple form where one one field is there which is random javascript file whose name ranges from 1 to 500000 so i have used rand function to populate its field without any issue

i want form to be automatically submitted after loading but i dont want pages to be refreshed and even i dont want field value to be changed after form is submitted. my code is very clear but dont know why it is not working

index.html

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>


 <title>testing</title>
 </head>

<body>

 <script type="text/javascript" >
 $(function() {
$("form1#form1").submit(function() {
 var searchBox = $("#searchBox").val();
 var dataString = 'searchBox='+ searchBox ;
if(searchBox=='')
 {
  $('.success').fadeOut(200).hide();
  $('.error').fadeOut(200).show();
 }
 else
{
 $.ajax({
 type: "POST",
 url: "test34.php",
 data: dataString,
  success: function(){
  $('.success').fadeIn(200).show();
 $('.error').fadeOut(200).hide();
 }
 });
 }
 return false;
 });
 });


 </script>

    <form method="post" name="form1" id="form1" enctype="multipart/form-data">
<input type="text" class="status" name="searchBox" id="searchBox"        value="<?= "".rand(1,2889889).".js"; ?>">


    <input class="button" type="submit" value="Submit" /></form>

    </body>
    </html>

test34.php

    <?php
    include('connect.php'); 
    $title23 =  $_POST['searchBox'];
mysql_query("insert into test (title) values('$title23')");

    echo $title23;

 ?>

but it is not submitting the form automatically please advise

After you've bound your submit method to the form, try triggering the submit method by doing this.

        return false;
    });

    $("form1#form1").trigger('submit'); // Here
});

您的选择器应该看起来像这样的$("form#form1")而不是$("form1#form1")

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