简体   繁体   English

Onclick事件不适用于提交类型按钮

[英]Onclick event not working with submit type button

I am new to PHP and HTML nad am working on a web page with a form and submit button, and i want to make submit button as a ref button too ie it will refer to some other page after submitting information to database. 我是PHP和HTML的新手,目前在使用表单和提交按钮的网页上工作,我也希望将提交按钮作为ref按钮,即在将信息提交到数据库后将引用其他页面。 My problem is, with i connect the page with php and forward information with database, onclick event does nothing otherwise it works fine. 我的问题是,随着我用php连接页面并通过数据库转发信息,onclick事件无济于事。 I have tried 我努力了

    onclick="location.href='https://www.google.com';">

and i even tired to make a function to refer to some link and called it in onclick event but of no use. 我什至不愿意创建一个函数来引用某个链接,并在onclick事件中调用它,但没有用。

PS 聚苯乙烯

Sorry if i have asked something funny. 抱歉,我问过些有趣的事。

You should use window.location 您应该使用window.location

onclick="window.location='https://www.google.com'"

but instead of using the onclick attribute, you should attach the event directly to the element. 但您不应使用onclick属性,而应将事件直接附加到元素。 If you use jquery, this could be 如果您使用jquery,则可能是

<button id="mybutton" />

and the script 和脚本

$('#mybutton').on('click', function(e) {
    window.location = "http://www.google.com";
    e.preventDefault();
});

but in this case, your form isn't submitted. 但是在这种情况下,不会提交您的表单。 If you want to submit the form to the server, and then redirect, you should use 如果要将表单提交到服务器,然后重定向,则应使用

header('Location: http://www.google.com');

which is an http redirect, after your script is executed 执行脚本后,这是一个http重定向

Use something like this as Ram Sharma has suggested 如Ram Sharma所建议的那样使用

 <form method="post" action="mypage.php">
        <input type="text" name="name" value="RN">
    <button type='submit' name="submit" value="Submit">submit</button>
</form>

then in mypage.php 然后在mypage.php中

    if($_POST['submit']){
       //save to database 
        header("Location: myotherpage.php");
       exit();
    }

There can be different ways 可能有不同的方式

1) You can call some javaScript function to redirect the page 1)您可以调用一些javaScript函数来redirect页面

<script type = "text/javascript">
   function redirectUser(){
       window.location = "http://www.yoururl.com";

       window.navigate("http://www.yoururl.com"); //works only with IE
   }
</script>

<form>
    <button name="myBtn" value="Submit" onclick = "redirecrUser()">Submit</button>
</form>

2) You can create a link a bit styling it to look like button to redirect 2)您可以创建一个styling有点像按钮redirect

<style>
    .linkToBtn{
        text-decoration: none;
        color: #ffffff;
        background-color: black;
        font-size: 20px;
        padding: 5px;
    }
</style>
<a href = 'yourUrl.com' class = 'linkToBtn'>Submit</a>

3) you can redirect it using the PHP 3)您可以使用PHP redirect

<form method = 'post' action = ''>
    <input type = 'submit' name = 'submitBtn' value = 'Redirect' />
</form>
<?php
    if(isset($_POST['submitBtn'])){
        header('Location: yourURL.com');
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM