doulin4844
2011-06-13 06:55
浏览 58
已采纳

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

Let's say after my user fills a form and clicks the submit button, I want the user redirected to the same page with the form and a jquery animation will play. The header function is there to prevent the user from refreshing the page and resubmitting the same data.

UPDATE: I've decided to use $_SESSION to record the user's session to try to get the jquery animation to play after they get redirected to the same page:

<?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条回答 默认 最新

  • duane9322 2011-06-13 08:50
    已采纳

    Use like this

    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>';
    }
    

    Or

    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>';
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanji9481 2011-06-13 06:59

    Can't you just keep the user on the same page, and process the form submit on the same page (by setting the action attribute of the form to the page that the form is on)?

    That would deal with the redirection problem. You can then have your jQuery animation code in the original page, and start the animation in a if(isset($_POST['submit'])) statement.

    评论
    解决 无用
    打赏 举报
  • dongnu4254 2011-06-13 09:09

    You can use the jQuery form plugin.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题