我需要保持在同一页面上,这样我就不用提交按钮了。如何转到php页面并发送表单值?依西佩尔:
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label> <input type="file" name="csv" id="csv"/>
<input type= "button" value="immport" onClick = "window.location.href =readFile.php'
.(this.form);" >
</form>
这行不通。其他选择?
如果您不想使用AJAX调用(?…请参阅注释),有一种经典的提交表单的方法,但这会导致重新加载实际页面:
表单和逻辑在同一个脚本中。在您的情况下,应该是readFile.php
。
使用$_SERVER['PHP_SELF']
作为表单操作:
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label> <input type="file" name="csv" id="csv"/>
<input type= "submit" value="immport" />
</form>
表格将提交到实际页面。来自文档:
CCD_ 3是包含诸如报头、路径等信息的数组,以及脚本位置。
'PHP_SELF'
当前正在执行的脚本的文件名,相对于文档根。。。。
Try This Code Will Work For You:
<form action="readFile.php" name="myform" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label> <input type="file" name="csv" id="csv"/>
<input type="button" value="immport" onClick = "document.myform.submit();" >
</form>
或者,您可以使用此javascript函数在没有提交按钮的情况下提交表单:
<form action="readFile.php" name="myform" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label> <input type="file" name="csv" id="csv"/>
<input type="button" value="immport" onClick = "submitform();" >
</form>
<script type="text/javascript">
function submitform()
{
if(document.myform.submit())
{//this check triggers the validations
document.myform.submit();
}
}
</script>
这在HTML5中其实很容易。查看W3学校:http://www.w3schools.com/tags/tag_button.asp
对于HTML5,BUTTON标记可以选择指定一个"formtarget"。W3学校:http://www.w3schools.com/tags/att_button_formtarget.asp
这允许您告诉表单提交到不同的目标窗口。从那里,你会在提交页面上添加一条在提交时关闭的指示,这样用户只会看到打开的窗口的闪光。
您的提交按钮可能如下所示:
<button type="submit" formtarget="submission">Submit</button>
而您的提交页面可以包括一个简单的JavaScript,如:
window.close();
我希望这能有所帮助。^^