2017-04-25 84 views
-1

我正在處理聯繫人頁腳。 HTML看起來像這樣:同時運行Javascript函數和PHP

<form action="indexTest.php" method="post"> 
        <input spellcheck="false" class="first" type="text" name="name" placeholder="name"> 
        <input spellcheck="false" class="first" type="text" name="email" placeholder="email"> 
        <textarea rows="5" spellcheck="false" class="last" type="text" name="message" placeholder="message"></textarea> 
        <input type="submit" name="submit" value="" id="button" onclick="banner()">  
       </form> 

現在,如果我點擊「提交」,PHP文件正在運行。但是正如你在代碼中看到的那樣,你會看到「onclick =」banner()「,但這是行不通的,只有PHP的工作原理,它想運行Javascript函數和PHP。要做到這一點,如果你點擊「提交」;在PHP文件運行和橫幅被使用Javascript顯示

+0

[Ajax](https://en.wikipedia.org/wiki/Ajax_(programming))。 – 3ocene

+2

當你提交表單時,它會將你重定向到indexTest.php,你的旗幟將會消失。你如何期待它同時做到這一點? – mpen

+0

[使用Ajax的表單示例](http://codegarycode.com/post.php?title=Submitting+an+HTML+form+via+Ajax) –

回答

0

可以使用onsubmit()功能,首先執行的功能則操作事件將工作

<form onsubmit="return banner();" action="indexTest.php" method="post" name="theForm"> 
         <input spellcheck="false" class="first" type="text" name="name" placeholder="name"> 
         <input spellcheck="false" class="first" type="text" name="email" placeholder="email"> 
         <textarea rows="5" spellcheck="false" class="last" type="text" name="message" placeholder="message"></textarea> 
         <input type="submit" name="submit" value="" id="button">  
        </form> 

        <script type="text/javascript"> 
         function banner(){ 
          alert('working'); 
          document.theForm.submit(); 
         } 
        </script> 

希望它會幫助你

0

JavaScript函數可以附加到表單onsmit事件。

<form action="indexTest.php" method="post" onsubmit="banner()"> 
       <input spellcheck="false" class="first" type="text" name="name" placeholder="name"> 
       <input spellcheck="false" class="first" type="text" name="email" placeholder="email"> 
       <textarea rows="5" spellcheck="false" class="last" type="text" name="message" placeholder="message"></textarea> 
       <input type="submit" name="submit" value="" id="button"> 
</form> 

onclick事件可能只在按鈕類型爲「按鈕」時運行。