我想使用php显示javascript警报和重定向。逻辑错误是它不会显示javascript警报。
这是我的代码:
if($sql_update==true){
echo "<script type='text/javascript'>alert('Updated');</script>";
header("location:?tag=student_entry&opr=upd&rs_id=".$_POST['stud_id_txt']."");
}
答案 0 :(得分:0)
如果你在php中嵌入js以进行提醒,那么也可以在其中使用JS重定向
$redirectURL="Your URL";
print("<script>");
print("alert('Updated');");
print("var t =setTimeout(\"window.location='".$redirectURL."';\", 3000);");
print("</script>");
答案 1 :(得分:0)
您可以创建一个javascript函数来帮助您提醒用户并重定向,如下所示:
function alertAndRedirect(studId) {
alert('Updated');
window.location = "?tag=student_entry&opr=upd&rs_id="+studId;
}
在php中你称之为:
if($sql_update==true){
echo "<script type='text/javascript'>alertAndRedirect(".$_POST['stud_id_txt'].");</script>";
}
答案 2 :(得分:0)
您无法在标头之前提供输出,即使您设置了输出缓冲区,也无法发出警报。所以通过js重定向。
public class AsyncSemaphore
{
private readonly static Task s_completed = Task.FromResult(true);
private readonly Queue<TaskCompletionSource<bool>> m_waiters =
new Queue<TaskCompletionSource<bool>>();
private int m_currentCount;
public AsyncSemaphore(int initialCount)
{
if (initialCount < 0)
{
throw new ArgumentOutOfRangeException("initialCount");
}
m_currentCount = initialCount;
}
public Task WaitAsync()
{
lock (m_waiters)
{
if (m_currentCount > 0)
{
--m_currentCount;
return s_completed;
}
else
{
var waiter = new TaskCompletionSource<bool>();
m_waiters.Enqueue(waiter);
return waiter.Task;
}
}
}
public void Release()
{
TaskCompletionSource<bool> toRelease = null;
lock (m_waiters)
{
if (m_waiters.Count > 0)
toRelease = m_waiters.Dequeue();
else
++m_currentCount;
}
if (toRelease != null)
toRelease.SetResult(true);
}
}