Javascript 如何在提交时打开同一页?
我有一个html表单,它的动作是一个php脚本。php代码基本上替换了该文件。 HTML代码:Javascript 如何在提交时打开同一页?,javascript,php,html,Javascript,Php,Html,我有一个html表单,它的动作是一个php脚本。php代码基本上替换了该文件。 HTML代码: <form name="input" action="//copy.php" method="POST" onsubmit="return alertbox();"> <input type="hidden" name="path1" value=path to image 1/> <input type="hidde
<form name="input" action="//copy.php" method="POST" onsubmit="return alertbox();">
<input type="hidden" name="path1" value=path to image 1/>
<input type="hidden" name="path2" value='path to image 2' />
<input type="submit" name="submit" value="Copy image"/>
</form>
?>
现在,当我单击“提交”时,会打开一个警告框“文件已成功更新”,当我单击“确定”时,会加载一个空白页面。如何避免加载空白页?我想在单击“提交”并弹出消息后保持在同一页面上
解决方案
由于我没有看到“回答你自己的问题”选项,我在这里发布解决方案。
此链接为您提供问题的文本答案。而我则通过代码提供答案
所以基本上,这很简单。放
header("HTTP/1.0 204 No Response");
在php文件中,它将在所有浏览器上成功运行,而无需打开新页面。这将避免使用jquery。将操作留空
<form method="post" action="">
这将使您在“提交”按钮后保持在同一页面上
<form name="input" action="" method="POST" onsubmit="return alertbox();">
<input type="hidden" name="path1" value=path to image 1/>
<input type="hidden" name="path2" value='path to image 2' />
<input type="submit" name="submit" value="Copy image"/>
</form>
您是否尝试更改输入的类型:
<input type="submit" name="submit" value="Copy image"/>
有关更多详细信息,请参阅
No。这将从同一URL加载一个新页面。No。这将从同一URL加载一个新页面。@Quentin我猜alertbox()是一个纯粹的“是否继续?”?“没有别的消息。因为PHP操作调用copy.PHP文件,所以它有点明显。否则,jquery将调用该文件,而不是PHP操作。但这并不重要,所以只能使用jquery来完成,对吗?或者有一种方法可以避免使用jquery(因为我不知道它)。谢谢你的帮助。我这样做了,并把它放在html上面,但它仍然加载另一个页面。
<form name="input" action="" method="POST" onsubmit="return alertbox();">
<input type="hidden" name="path1" value=path to image 1/>
<input type="hidden" name="path2" value='path to image 2' />
<input type="submit" name="submit" value="Copy image"/>
</form>
if(isset($_POST['submit']))
{
//include "//copy.php"; // this file probably contains functions, so lets load functions first IF needed..
$image1 = $_POST['path1'];
$image2 = $_POST['path2'];
copy($image1, $image2);
}
<input type="submit" name="submit" value="Copy image"/>
<input type="button" name="submit" id="submit" value="Copy image"/>
$('#submit').click(function (event) {
event.preventDefault();
//Do whatever you need to
//Submit if needed: document.forms[0].submit();
});
action="<?php echo $_SERVER['PHP_SELF']; ?>