2017-04-25 86 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事件可能只在按钮类型为“按钮”时运行。