gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:47:02 24 4
gpt4 key购买 nike

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

更新:我决定使用 $_SESSION 来记录用户的 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>

最佳答案

像这样使用

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

关于php - 重定向到同一页面后如何调用jquery函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6327415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com