重定向到同一页面后如何调用jquery函数?

时间:2011-06-13 06:55:26

标签: php jquery html forms header

假设我的用户填写表单并单击提交按钮后,我希望用户将表单重定向到同一页面并播放jquery动画。标头功能用于防止用户刷新页面并重新提交相同的数据。

更新: 我决定使用$ _SESSION来记录用户的会话,以便在重定向到同一页面后尝试获取jquery动画:

<?php
session_start(); 

if (isset ($_POST['submit']))
{
$connect = mysql_connect("","","") or die("Error connecting to db");
mysql_select_db("") or die("Error connecting to db");

$text = $_POST['text'];



mysql_query ("INSERT INTO header VALUES('','$text')");

  $_SESSION['jq']=='a';
    header('Location: header.php');
    die();

}


if($_SESSION['jq']=='a') {
   $_SESSION['jq']='';

echo'
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

    $("div").hide(3000);

});
</script>';


}




?>



<html>
<head>

<style type="text/css">
#jquery {border:1px solid black;width:300px;}
</style>

</head>
<body>
<form name="header" action="header.php" method="post">


<input type="text" name="text" />
<input type="submit" name="submit" value="submit"    />
</form>
<div id="jquery">this is jquery animation</div>

</body>
</html>

3 个答案:

答案 0 :(得分:4)

像这样使用

if (isset ($_POST['submit']))
{
    $text = $_POST['text'];
    mysql_query ("INSERT INTO header VALUES('','$text')");

    //redirect to same page that user submitted form from
    header('Location: form.php?jq=a');
    die();
}
if($_GET['jq']=='a') {
   //play animation
   echo'<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
   <script type="text/javascript">
   $(document).ready(function(){

       //insert jquery animation code

   });
   </script>';
}

session_start();
if (isset ($_POST['submit']))
{
    $text = $_POST['text'];
    mysql_query ("INSERT INTO header VALUES('','$text')");

    //redirect to same page that user submitted form from
    //$_SESSION['jq']=='a'; commented for your understanding remove this in your code.
    $_SESSION['jq']='a';
    header('Location: form.php');
    die();
}
if($_SESSION['jq']=='a') {
   $_SESSION['jq']='';
   //play animation
   echo'<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
   <script type="text/javascript">
   $(document).ready(function(){

       //insert jquery animation code

   });
   </script>';
}

答案 1 :(得分:1)

您不能只是让用户保持在同一页面上,并在同一页面上处理表单提交(通过将表单的action属性设置为表单所在的页面)?

这将解决重定向问题。然后,您可以在原始页面中使用jQuery动画代码,并在if(isset($_POST['submit']))语句中启动动画。

答案 2 :(得分:1)

您可以使用jQuery form plugin