2011-06-12 62 views
0

我有這段代碼。試圖將表單值從一個內部頁面傳遞到另一個,並且它不工作。這段代碼有什麼問題嗎? (表單傳遞變量)

下面的代碼:

<div data-role="page" id="home"> 

    <div data-role="header"> 
     <h1>Page One</h1> 
    </div><!-- /header --> 

    <div data-role="content"> 
     <form action="post" name="myform"> 
     <input type="text" value="" name="mytext" /> 
       <input type="submit" value="submit" /> 
       </form> 
    </div><!-- /content --> 
</div><!-- /page --> 

//和頁面2

<div data-role="page" id="page2"> 

    <div data-role="header"> 
     <h1>Page Two</h1> 
    </div><!-- /header --> 

    <div data-role="content"> 
     <?php if (isset($_POST['mytext'])) { 
    // do something with $_POST['value'] 
    echo 'it works'; } ?> 
    </div><!-- /content --> 
</div><!-- /page --> 

它基本上不工作......沒有錯誤,但沒有任何價值。

+0

*「這基本上不工作......沒有錯誤,但沒有數值。」*這似乎很奇怪。我懷疑有一個錯誤,但你沒有注意到它。遠遠不止的jQuery示例(甚至jQuery API函數)都省略了錯誤處理。 **總是**請務必處理['error'](http://api.jquery.com/jQuery.ajax/)回調(無論是在每個調用中,還是全局通過['ajaxSetup']](http:///api.jquery.com/jQuery.ajaxSetup/))。在這種情況下,您應該看到'error'被404調用,除非您發佈表單的URL確實是「post」(這可能,不太可能)。 – 2011-06-12 10:11:32

回答

2

最有可能的錯誤是在這裏:

<form action="post" name="myform"> 
     <input type="text" value="" name="mytext" /> 
     <input type="submit" value="submit" /> 
</form> 

行動被認爲是形式的處理器,無論是在同一頁面或另一個(這裏說闡述的形式PHP腳本所在) 。 POST是方法。(可GET或POST)

所以它應該是:

<form action="" method="POST" name="myform"> <!-- action = "" reloads the same page, otherwise you could write action="myphppage.php" or whatever --> 
    <input type="text" value="" name="mytext" /> 
    <input type="submit" value="submit" /> 
</form> 
3

你的動作應該是將要處理你的帖子變量和方法應該後的PHP腳本。

<form action="somefile.php" method="post"> 
1
<form action="post" name="myform"> 

是錯誤的。

它應該是這樣的:

<form method="post" name="myform" action=""> 

您需要發送一個POST方法。該動作是空的,因此它將其發送到頁面本身。

1

'action'應該是目標網址。您已將method="post"action="post"混淆。設置爲"second_page.php".

我並沒有完全理解你的內部頁面是什麼意思,但是如果它是相同的頁面,只有不同的div,那麼請將操作留空(action='')。

+1

我懷疑他正在使用jQuery手機,因此是內部頁面。它在最終的移動應用程序中使用'data-role =「頁面」'呈現div作爲單獨的頁面。從一個純粹的結構POV,代碼都在同一個'頁面'上。以爲我會爲你清楚:) – cabaret 2011-06-12 10:22:29

+0

謝謝,歌舞表演 – 2011-06-12 10:24:34