簡體   English   中英

使用一個按鈕為php和javascript提交多個表單

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

是否可以用一個按鈕提交兩個表格? 一個表單的值是從JavaScript生成的,另一個表單是從PHP生成的。

我舉了一個例子來說明我在嘗試什么。

<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>

我不相信它可以做兩個連續的submit()調用,因為調用submit()意味着你正在發出HTTP請求(GET,POST等),並且服務器會將你的頁面轉發到其他地方。 這意味着永遠不會達到第二個submit()。

發送阻塞異步POST / GET調用並在調用之后調用submit()甚至第二個POST / GET可能會更好。

EDIT1

請參閱此主題如何在php中進行異步GET請求 下一個是從這個來源引用的

file_get_contents會做你想要的

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

編輯:一種觸發GET請求並立即返回的方法。

引自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);
}

您可以通過Ajax單獨提交它們。 這是一個使用jQuery和Form Plugin的例子。 應該讓你開始。

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

查看您的代碼,您嘗試提交表單並在之后提交輔助表單...

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

如果您的第二個表單數據不依賴於提交的第一個表單,您可以將簡單的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