简体   繁体   English

使用一个按钮为php和javascript提交多个表单

[英]submitting multiple forms with one button for both php and javascript

Is it possible to submit two forms with one button? 是否可以用一个按钮提交两个表格? One form's values are generated from JavaScript and the other form from PHP. 一个表单的值是从JavaScript生成的,另一个表单是从PHP生成的。

I made an example to give an idea of what I'm attempting. 我举了一个例子来说明我在尝试什么。

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

<?php
Class testing{

    function text_test(){
        ?>
        <form method="post" name="postform">
        <input type="textbox" name="text"/>
        <input type="submit" onclick ="submit_form()" name="submit"/>
        </form>
        <?php
    }

    function display(){
        echo $_POST['text'];
        echo $_POST['js_input'];
     }
 }
$test = new testing();
$test->text_test();

if(isset($_POST['js_form'])){
    $test->display();
}
?>

<script type="text/javascript">
    var jsvariable = "js_test";
</script>

<form method="post" name="js_form">
<input type="hidden" name="js_input"    value="document.getElementByName(jsvariable).value" />
</form>

<script type="text/javascript">
function submit_form(){
    $('postform').each(function () {
            $(this).ajaxForm();  //Initialize as ajaxForm
            var options = {
               context: this
            }                 
            $(this).ajaxSubmit(options); //Submit form with options above
    });
    $('js_form').each(function () {
        $(this).ajaxForm();  //Initialize as ajaxForm
        var options = {
           context: this
        }                 
        $(this).ajaxSubmit(options); //Submit form with options above
     });
 }
 </script>

I don't believe its possible to do two consecutive submit() calls, because invoking submit() means that you are making an HTTP request (GET, POST, etc..) and that the server will forward your page somewhere else. 我不相信它可以做两个连续的submit()调用,因为调用submit()意味着你正在发出HTTP请求(GET,POST等),并且服务器会将你的页面转发到其他地方。 This means that the second submit() will never be reached. 这意味着永远不会达到第二个submit()。

It might be better to send a blocking asynchronous POST/GET call and after that call a submit() or even a second POST/GET. 发送阻塞异步POST / GET调用并在调用之后调用submit()甚至第二个POST / GET可能会更好。

Edit1 EDIT1

Please see this thread on how to make asynchrnonous GET requests in php . 请参阅此主题如何在php中进行异步GET请求 The next is quoted from this source 下一个是从这个来源引用的

file_get_contents will do what you want file_get_contents会做你想要的

$output = file_get_contents('http://www.example.com/');
echo $output;

Edit: One way to fire off a GET request and return immediately. 编辑:一种触发GET请求并立即返回的方法。

Quoted from http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html 引自http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html

function curl_post_async($url, $params)
{
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }
    $post_string = implode('&', $post_params);

    $parts=parse_url($url);

    $fp = fsockopen($parts['host'],
        isset($parts['port'])?$parts['port']:80,
        $errno, $errstr, 30);

    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}

You can submit them separately via Ajax. 您可以通过Ajax单独提交它们。 Here's an example using jQuery and the Form Plugin . 这是一个使用jQuery和Form Plugin的例子。 Should get you started. 应该让你开始。

     $('form').each(function () {
            $(this).ajaxForm();  //Initialize as ajaxForm
            var options = {
               success: succesCallback,
               error: errorCallback,
               context: this
            }                 
            $(this).ajaxSubmit(options); //Submit form with options above
    });

looking at your code you are trying to submit a form and submit a secondary form afterwards... 查看您的代码,您尝试提交表单并在之后提交辅助表单...

if(isset($_POST['submit'])) {
    ?>
    <script type="text/javascript">
        document.js_form.submit();
    </script>
    <?php

if your 2nd form data is NOT dependant on the first form submitted you can simple APPEND form elements to the php form - see below 如果您的第二个表单数据不依赖于提交的第一个表单,您可以将简单的APPEND表单元素添加到php表单 - 请参阅下文

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

    var jsvariable = "js_test";

    $(document).ready(function(){
      //append form elements here...
      $('#form').append('<input type="hidden" name="js_input" value="'+jsvariable+'" />');
    });

</script>


<?php
Class testing{

    function text_test(){
        ?>
        <form method="post" name="postform" id="form">
        <input type="textbox" name="text"/>
        <input type="submit" name="submit"/>
        </form>
        <?php
    }

    function display(){
        echo $_POST['text'] . '<br>';
        echo $_POST['js_input'];
    }
}

$test = new testing();
$test->text_test();

if(isset($_POST['submit'])) {
    $test->display();
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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