开发者

how to call jquery function after redirecting to same page?

开发者 https://www.devze.com 2023-03-13 01:14 出处:网络
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开发者_开

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>


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


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.


You can use the jQuery form plugin.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号