从已发布的表单加载页面时调用函数

时间:2014-02-20 19:33:57

标签: javascript jquery html forms post

在我的index.php页面上,我有一个表格可以发布到第三方新闻通讯系统。 该页面重新加载index.php?mail& 无论如何我可以在页面加载时检查并且表单已经发布了吗?如果有,它可以调用另一个函数吗?

谢谢Chris

代码:

所以我希望的是一种onload功能,可以检测表单名称=“subscribe”是否已发布。然后我调用一个函数(这将是一个弹出div) 我可以通过一些帮助到达那里,可以做一些PHP或JavaScript,也许不编程但是了解其中的一些。没有ajax的线索

mailbar8(实际的表单位于简报中):

<?php include("globals.php"); ?>   
<form action="<?php echo $website.$relative_string;?>" name="subscribe" onsubmit="javascript:return checkEmail(this);" method="post">
  <div id="cell8" class="titlecell2"><h3>Email:</h3></div>
     <div id="cell9" class="inputcell2">
        <input type="text" class="inputfield2" name="email" value="Your Email..." id="email2" maxlength="255" onfocus="this.value='';">
     </div>
     <div id="cell10" class="textcell3">
       <input name="group" type="hidden" id="group[]" value="<?php echo $group; ?>">
       <input name="subscribe" id="sub" type="radio" value="true" checked>
       </span>Subscribe</p>
     </div>
     <div id="cell11" class="buttoncell">
        <button type="submit" name="Submit2" value="Join" id="submitButton2" button" onClick="javascript:myFunction();"/>
        <span>Join</span>
        </button>
     </div>
     <div id="cell8" class="textcell4">
       <input type="radio" name="subscribe" id="unsub" value="false">
       </span>Un-Subscribe</p>
     </div>
  </form>

表单名称是订阅,它检查电子邮件条目是否合法,然后是否将用户重定向回:http://www.allcoles.com/index.php?page=mail&

index.php(这段代码显示了mailbar8.php格式):

<div id="guestArea"  class="siteAreas">
<div id="guestTitle" class="roundedTitle">Guests</div>
<div id="guestContent" class="roundedContent">
  <h4>Get Our Newsletter!</h4>
  <?php
                      $mailbar=8;
                      $group=1;
                      include("maillist/mailbar.php");
                       ?>
</div>
</div>
  <!-- End of User and Guest Areas -->

index.php(当页面打开并且表单已发布时我要加载的弹出式div,它有一个js文件,但这并不重要 - 弹出式div目前可以通过www.allcoles.com上的href链接工作):

<div id="toPopup"> 
    <div class="close"></div>
    <span class="ecs_tooltip">Press Esc to close <span class="arrow"></span></span>
    <div id="popup_content"> <!--your content start-->
      <h2 align="center">All Coles Newsletter System</h2>
      <h3 align="center">bringing News, Birthdays, Events and Invites to your mailbox!</h3>
      <hr align="center" width="75%">
      <p style="text-align:center"> <?php
            if(isset ($_GET['page']))
            {
                if ($_GET['page'] == "mail")
                {
                include("maillist/mailmain.php");
                }

                if ($_GET['page'] == "about")
                {
                include("about.php");
                }
            }else {
                print("THIS PRINT IS WHERE THE NEWSLETTER SAYS THAT THE EMAIL HAS BEEN SUBSCRIBED TO THE DATABASE, THIS POPUP DIV IS A NICE WAY TO SHOW THAT.  THIS BIT CHANGES IF THE USER IS ALREADY SUBSCRIBED, OR  ADDED OR REMOVED");
            }
        ?>  </p2>
        <hr align="center" width="75%">
        <p style="text-align:center; font-size: 12px;">
        <font style="text-decoration:underline; font-weight:bold;">TIP</font>
       : Remember to check your Junk Mail, and add 'administrator@allcoles.com' to your 
        <font style="text-decoration:underline; font-weight:bold;">SafeSenders</font>
         list.</p>
  </div>
</div>
<div class="loader"></div>
<div id="backgroundPopup"></div>

理想情况下 - 当(http://www.allcoles.com/index.php?page=mail&)加载时,它会识别已提交的名为subscribe的表单,然后调用一个函数。 例如函数:formPosted();

我想出的解决方案是(在每个人的帮助下):

<?php
if(isset ($_GET['page']))
{
if ($_GET['page'] == "mail")
{
echo "<script>window.alert('Found the reply!');var formPosted = true;</script>";
}
}
?>
<script>
if(formPosted) {
window.alert("popupclick!");
popupClick();
}
</script>

3 个答案:

答案 0 :(得分:0)

这样简单的事情怎么样?

<?php

    if( /* check if form has been postet */ ) {
        $form_posted = true;
    } else {
        $form_posted = false;
    }
?>

然后......

<?php if($form_posted) : ?>
    <script>
        formPosted();
    </script>
<? endif ?>

答案 1 :(得分:0)

<?php
$dispatched = isset($_POST['dispatched']) ? 1 : 0;

if($dispatched) {
  // handle data
   echo "<script>var formPosted = true;</script>";

}
?>

<script>
 if(formPosted) {
   // do something
 }
</script>

答案 2 :(得分:0)

使用Ajax:

var mail = ???;
$.post( "index.php", { mail : mail }, function( data ) {
    formPosted();
});

这很简单。它使用参数mail发布到您想要的页面,然后调用您想要的功能。