Javascript 按下按钮在php中打开一个新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7865669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening a new page in php on a button press
提问by Matrix001
I want a new page to be opened when a button is pressed. Here is my code so far:
我想在按下按钮时打开一个新页面。到目前为止,这是我的代码:
<input name="newThread" type="button" value="New Discussion" onclick="window.open('Political/Thread/thread_insert.php')"/>
It fails, however, ..I think the path is incorrect, but I dont know why..cause it is the correct directory path...
但是,它失败了,..我认为路径不正确,但我不知道为什么..因为它是正确的目录路径......
回答by Eric
This will open a new tab/window (depending on the user's settings):
这将打开一个新选项卡/窗口(取决于用户的设置):
<a class="button" href="Political/Thread/thread_insert.php" target="_blank">New Discussion</a>
To make this "a button":
要使其成为“按钮”:
a.button {
border: 1px solid #808080;
background: #a0a0a0;
display: inline-block;
padding: 5px;
}
回答by Mark Brown
Your code is correct (valid and function), you problem is the your path.
你的代码是正确的(有效和功能),你的问题是你的路径。
Remember that page location is the page inside the web server and relative to the current page location.
请记住,页面位置是 Web 服务器内的页面,并且相对于当前页面位置。
So if your web server root is for example c:\wamp\www
then the web address test/test.php
will look for the real page of c:\wamp\www\test\test.php
. However the same address encoded into a page already in a subdirectory will be relative unless it starts with /
So from the page test/test.php in the above example the link to test/test.php
will become /test/test/test.php
(with a real path of c:\wamp\www\test\test\test.php
)
因此,如果您的 Web 服务器根目录是例如,c:\wamp\www
那么该网址test/test.php
将查找c:\wamp\www\test\test.php
. 但是,编码到子目录中的页面中的相同地址将是相对的,除非它以/
So 从上面示例中的页面 test/test.php开始,链接test/test.php
将变为/test/test/test.php
(实际路径为c:\wamp\www\test\test\test.php
)
If you have copied the link from another page somewhere this is probably the problem
如果您从其他页面的某个地方复制了链接,这可能是问题所在
回答by Ashok KS
Try the below code,
试试下面的代码,
<INPUT type="button" value="Click" onClick="window.open('Political/Thread/thread_insert.php','windowname',' width=400,height=200')">
回答by Kevin Vandenborne
Try this:
尝试这个:
<form action="Political/Thread/thread_insert.php" target="_blank" action="post">
<input name="newThread" type="submit" value="New Discussion" />
</form>
Tested and working. You can leave the action empty if you want.
测试和工作。如果需要,您可以将操作留空。
Or
或者
<form action="Political/Thread/thread_insert.php" target="_blank" action="">
<button>New Discussion</button>
</form>
But as said in one of the other answers, you better go with a link and style it like a button. Using javascript or making forms to achieve the same results is just more work and well... bad.
但正如其他答案之一所述,您最好使用链接并将其样式设置为按钮。使用 javascript 或制作表单来实现相同的结果只是更多的工作,而且......很糟糕。
回答by RileyManda
Use this
用这个
<a href="../Myfolder/yourfile.html" type="button" class="btn btn-default subs-btn">Join</a>
class="btn btn is the style class of the button.
class="btn btn 是按钮的样式类。
And f you want a direct link to the file and not have to go into an external folder:
如果您想要直接链接到文件而不必进入外部文件夹:
<a href="yourfile.html" type="button" class="btn btn-default subs-btn">Join</a>
I hope this helps
我希望这有帮助
回答by Suraj Jadhav
Add below properties to element and it will look exactly same as button
将以下属性添加到元素,它看起来与按钮完全相同
Disable text-decoration for link for better effect
禁用链接的文本装饰以获得更好的效果
align-items: flex-start;
text-align: center;
cursor: default;
box-sizing: border-box;
padding: 2px 6px 3px;
border-width: 2px;
border-style: outset;
border-image: initial;
-webkit-appearance: push-button;
-webkit-user-select: none;
white-space: pre;
text-rendering: auto;
letter-spacing: normal;
word-spacing: normal;
text-transform: none;
text-indent: 0px;
text-shadow: none;
display: inline-block;
text-align: start;
margin: 0em 0em 0em 0em;
font: 13.3333px Arial;
I know it's too late but I'm putting it for other people who is looking for the same answer
我知道为时已晚,但我将它留给正在寻找相同答案的其他人
回答by Shahan Mehbub
if(isset($_POST['myButtonName'])){
header("Location:newPage.php");
exit();
}
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<button style="margin-left:5px;" type="submit" class="btn btn-primary" name="myButtonName">Open</button>
</form>