jquery 中的新功能:如何在 jquery 中单独提交表单?

发布于 2024-11-24 18:23:51 字数 1758 浏览 1 评论 0原文

这是 jQuery 中的标准表单提交器:

<script>
$(document).ready(function(){  
    $("form#submit").submit(function() {  
        var fname = $('#fname').attr('value');  
        var lname = $('#lname').attr('value');  
        $.ajax({  
            type: "POST",  
            url: "report.php",  
            data: "fname="+ fname +"& lname="+ lname,  
            success: function(){  
                $('form#submit').hide(function(){ 
                    $('div.success').show();
                });    
            }
        });  

        return false;  
    });
}); 
</script>

以下是表单:

<form id="submit" name="submit">
<input id="fname" name="fname" size="20" type="hidden" value="aword">  <font class='black'>report:</font>
<input id="lname" name="lname" size="40" type="text">  
<button id="send">send</button>
</form>

<form id="submittwo" name="submittwo">
<input id="fname" name="fname" size="20" type="hidden" value="anoterword">  <font class='black'>report:</font>
<input id="lname" name="lname" size="40" type="text">  
<button id="send">send</button>
</form>

report.php:

<?php
$fname = htmlspecialchars(trim($_POST['fname']));  
$lname = htmlspecialchars(trim($_POST['lname']));  
$myFile = "reports.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "\n".$fname."\n";
fwrite($fh, $stringData);
$stringData = $lname."\n";
fwrite($fh, $stringData);
fwrite($fh, $theData);
fclose($fh);
?>

将会有更多使用 PHP 循环的表单。我想做的是将它们分开提交。知道如何或可以告诉我在哪里可以找到答案吗?

This a standard form submitter in jQuery:

<script>
$(document).ready(function(){  
    $("form#submit").submit(function() {  
        var fname = $('#fname').attr('value');  
        var lname = $('#lname').attr('value');  
        $.ajax({  
            type: "POST",  
            url: "report.php",  
            data: "fname="+ fname +"& lname="+ lname,  
            success: function(){  
                $('form#submit').hide(function(){ 
                    $('div.success').show();
                });    
            }
        });  

        return false;  
    });
}); 
</script>

and here are the forms:

<form id="submit" name="submit">
<input id="fname" name="fname" size="20" type="hidden" value="aword">  <font class='black'>report:</font>
<input id="lname" name="lname" size="40" type="text">  
<button id="send">send</button>
</form>

<form id="submittwo" name="submittwo">
<input id="fname" name="fname" size="20" type="hidden" value="anoterword">  <font class='black'>report:</font>
<input id="lname" name="lname" size="40" type="text">  
<button id="send">send</button>
</form>

report.php:

<?php
$fname = htmlspecialchars(trim($_POST['fname']));  
$lname = htmlspecialchars(trim($_POST['lname']));  
$myFile = "reports.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "\n".$fname."\n";
fwrite($fh, $stringData);
$stringData = $lname."\n";
fwrite($fh, $stringData);
fwrite($fh, $theData);
fclose($fh);
?>

There will be more forms looped with PHP. What I want to do is to submit them separately. Any idea how or can you tell me where I can find an answer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不即不离 2024-12-01 18:23:51

如果我已经很好地理解了这个问题,这可以帮助

$('form').submit(function(e){
    e.preventDefault(); // prevent the default behaviour of the form submit

    var fname = $("#fname",this).val(); // get the fname of the submitted form
    var lname = $("#lname",this).val(); // get the lname of the submitted form
    $.ajax({  
            type: "POST",  
            url: "report.php",  
            data: "fname="+ fname +"& lname="+ lname,  
            success: function(){  
                $('form#submit').hide(function(){
                    $('div.success').show();
                });    
            }
        });  
    });

这里是一个小提琴 http://jsfiddle.net/fG8T3/

iff i have understood the question well this can help

$('form').submit(function(e){
    e.preventDefault(); // prevent the default behaviour of the form submit

    var fname = $("#fname",this).val(); // get the fname of the submitted form
    var lname = $("#lname",this).val(); // get the lname of the submitted form
    $.ajax({  
            type: "POST",  
            url: "report.php",  
            data: "fname="+ fname +"& lname="+ lname,  
            success: function(){  
                $('form#submit').hide(function(){
                    $('div.success').show();
                });    
            }
        });  
    });

here is a fiddle http://jsfiddle.net/fG8T3/

美羊羊 2024-12-01 18:23:51

您可以只使用一种表单,但更改字段名称

<form id="submit" name="submit">
   <input id="fname" name="fname[]" size="20" type="hidden" value="aword">  
   <input id="lname" name="lname[]" size="40" type="text">

   <input id="fname" name="fname[]" size="20" type="hidden" value="anoterword"> 
   <input id="lname" name="lname[]" size="40" type="text">
   <input type="submit" value="send" />
</form> 

现在您可以将 GET 数据作为数组进行迭代:

foreach ($_GET['fname'] as $index => $value)
{
   echo $_GET['fname'][$index].' '.$_GET['lname'][$index];
}

注意:ID 应该是唯一的。

You could just use one form, but change the names of your fields

<form id="submit" name="submit">
   <input id="fname" name="fname[]" size="20" type="hidden" value="aword">  
   <input id="lname" name="lname[]" size="40" type="text">

   <input id="fname" name="fname[]" size="20" type="hidden" value="anoterword"> 
   <input id="lname" name="lname[]" size="40" type="text">
   <input type="submit" value="send" />
</form> 

Now you can iterate through the GET data as an array:

foreach ($_GET['fname'] as $index => $value)
{
   echo $_GET['fname'][$index].' '.$_GET['lname'][$index];
}

Note: ID's should be unique.

独守阴晴ぅ圆缺 2024-12-01 18:23:51

目前尚不清楚您在哪里遇到问题,但如果您尝试在不离开当前页面的情况下提交它们,您可能正在寻找 jQuery 表单插件。它使提交表单和显示结果变得更加容易,因此您无需像在发布的代码中那样手动执行此操作。

It's not very clear where you're having the problem but if you're trying to submit them without leaving the current page you may be looking for the jQuery Form plugin. It makes it a bit easier to submit forms and display the results so you won't be doing it manually as you are in the code you posted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文