0

jquery dialogと混同しただけですphp form submit

私には2つのフォームがpage1.phpあり、page2.php

page1.php<a>リンクがあります

<a id="addNew"> Add New </a>
<div id="myDiv"> 
       <!-- load 'page2.php' file using this div --> 
</div>

このリンクをクリックするとpage2.php、jqueryダイアログにフォーム値を含むファイルがロードされます。

これは私のスクリプトです

<script>
   $("#addNew").click(function() {
       $("#myDiv").load('page2.php').dialog({modal:true}); 
   });
</script>

これらのスクリプトは、page2.phpファイルをロードして正常に動作します。

今、私の質問は、

フォームを送信すると、その値はto にpage2.phpなります。でも一緒にいたい。re-directedpage2.phppage1.php

どうすればこれを達成できますか? 前もって感謝します

4

2 に答える 2

1

フォームを提供しなかった方法を見て、推測することしかできません。

<form id="some_form" action="page2.php" method="post">
    <input type="text" name="user_name"/>
    //a collection of form elements
<form>

次に、ajaxを使用し、フォーム送信イベントをキャプチャしながら、デフォルトのアクションを防ぎます

$(document).on('submit', '#some_form', function(e){
    e.preventDefault(); //stop form from redirecting, also stops sending of data
    var $this = $(this); // cache form object for performance
    $.ajax({
        url: $this.prop('action'),
        type: $this.prop('method'),
        data: {
          handle_form: 'ajax',
          form_data : $this.serializeArray()//array of objects
        },
        success:function(data){
            //data is what is returned from the server if successful
        }
    });
    return false; //just another example of stopping form submit, don't need both
});

次に、page2.php ファイルで、フィールドの存在を確認します。

if(isset($_POST['user_name']) && $_POST['handle_form'] == 'ajax'):

    //your form is trying to be submit
    //handle the submission
    echo 'We submit the form!'; //is the 'data' in our success function

elseif(isset($_POST['user_name']) && !isset($_POST['handle_form'])):

    //regular form submission, no ajax, no javascript.

endif;     
于 2013-05-18T09:18:06.603 に答える