2011-04-11 27 views
1

我正在使用PHP和JQuery創建一個網站,並進入問題rearding形式。不能理解爲什麼我的網站保持導航到其索引頁?

例如,我的index.php如下所示:

<body> 
    <?php 
     echo "<div id=\"web_Page\">"; 
     require("public/templates/header.php"); 
     require("public/templates/menu.php"); 
     require("public/templates/home.php"); 
     require("public/templates/footer.php"); 
     echo "</div>"; 
    ?> 
</body> 

它然後加載頁眉,頁腳,菜單區和起始頁。使用JavaScript代碼

<div id="web_Page"> 
    <div id="web_Header"> 
     //contents of header.php loaded into this div// 
    </div> 
    <div id="web_Menu"> 
     //contents of menu.php loaded into this div// 
    </div> 
    <div id="web_Contents"> 
     //contents of home.php loaded into this div// 
    </div> 
    <div id="web_Footer"> 
     //contents of footer.php loaded into this div// 
    </div> 
</div> 

菜單項,然後裝入web_contents DIV區域:

$('#web_Menu a').click(function(e) 
{ 
    e.preventDefault(); 
    $('#web_Content').load($(this).attr('href'), function() 
    { 
    }); 
}); 
已加載的文件也被包圍在DIV區域,以便最終的index.php呈現像這樣

現在,代碼將從菜單中選擇的所有頁面加載到web_Contents div區域。但是我現在創建了一個名爲register.php的頁面,它有一些非常意想不到的功能。下面是它呈現的HTML,它也將在web_Contents加載DIV面積:

<div id="reg_Div"> 
    <form id="reg_Form> 
     <label>Desired Username: </label><input type="text" name="uname" id="uname" /> 
     <button>Register</button> 
    </form> 
</div> 

index.php頁面將被呈現:

<div id="web_Page"> 
    <div id="web_Header"> 
     //contents of header.php loaded into this div// 
    </div> 
    <div id="web_Menu"> 
     //contents of menu.php loaded into this div// 
    </div> 
    <div id="web_Contents"> 
     <div id="reg_Div"> 
      <form id="reg_Form> 
       <label>Desired Username: </label><input type="text" name="uname" id="uname" /> 
       <button>Register</button> 
      </form> 
     </div> 
    </div> 
    <div id="web_Footer"> 
     //contents of footer.php loaded into this div// 
    </div> 
</div> 

的問題是,即使這個表單沒有功能,按鈕也沒有,只要點擊按鈕,網站導航到index.php頁面。我真的不明白爲什麼?

任何人都可以看到爲什麼代碼會導致觸發按鈕導航到index.php。另外,由於index.php只是require()頁面的集合,並且register.php也在那裏加載,導致頁面自動丟棄包含register.php的web_Contents div並將其替換爲home的內容。 PHP的?

這真的讓我困惑?

任何反饋將不勝感激。 謝謝。

回答

1

首先,你在這方面是缺失報價

<form id="reg_Form> // <--- missing a quote 

還,你要更新到這樣的事情...

<form id="reg_Form" onsubmit="return(false);"> // <--- should prevent form submittion 
+1

你可以把聽者在提交事件太...: '$( 'reg_Form')提交(函數(){ e.preventDefault();返回false; });'。 – 2011-04-11 17:30:51

1

形式有action屬性,其告訴瀏覽器在點擊提交按鈕後瀏覽的位置。如果沒有指定action屬性,那麼默認情況下它是當前頁面,在你的情況下是「index.php」。

0

HTML表單提交他們的數據到一個頁面,如果沒有指定,那麼它提交到當前頁面(index.php在你的情況)。

<form id="reg_Form"> 

相同

<form id="reg_Form" action="index.php" method="GET"> 

要停止提交表單,創建一個onsubmit處理,告訴它不要提交。

$('#reg_Form').submit(function(e){ 
    e.preventDefault(); // Tells the form not to submit 
}); 
0

表單點擊按鈕,儘管沒有一個action屬性之後提交的原因,就是action屬性默認爲當前頁面的URL,如果不指定。

阻止提交表單的一種方法是在表單標記中包含onsubmit屬性。如果此屬性中包含的JavaScript代碼返回false,則表單將不會被提交。

<form id="reg_Form" onsubmit="return false;"> 
相關問題