简体   繁体   English

提交表单按钮后停留在同一页面

[英]staying in same page after submiting form button

I'm trying to stay on the same page after submitting a form button. 提交表单按钮后,我试图停留在同一页面上。 Depending on the button the user clicks, I want to display certain information on that same page the button was clicked. 根据用户单击的按钮,我想在单击按钮的同一页面上显示某些信息。

For some reason it keeps redirecting me back to my "ask_login.php" page after I click on the button. 由于某些原因,在我单击按钮后,它一直将我重定向回我的“ ask_login.php”页面。 I've read around and some people recommend using AJAX or JQuery but I don't really understand much about it. 我已经阅读了很多,有人建议使用AJAX或JQuery,但是我对此并不了解。 I'd be appreciated if I could get some help. 如果能得到帮助,我将不胜感激。 Thanks. 谢谢。

loggedin.php loggedin.php

   <?php
    $username = $_POST['username'];
    $password = $_POST['password'];

    if($username && $password) {
        //info is provided
        $queryget = mysql_query("SELECT * FROM users WHERE id='$username' AND password='$password'");
            $numrows = mysql_num_rows($queryget);

            if($numrows != 0) {

                //something has been found
                $_SESSION['id'] = $username;



            } else {
                echo "Wrong username/password";
                echo "<script>alert('Username/Password are wrong');</script>";
                echo "<script language='javascript'>window.location = 'ask_login.php';</script>";
            }
    }
    else {
        echo "Wrong username/password";
        echo "<script>alert('Username/Password are wrong');</script>";
        echo "<script language='javascript'>window.location = 'ask_login.php';</script>";
    }

    ?>


              <form method="POST" action="">
              <input type="submit" name="details" value="Details">
              </form>


                <form method="POST" action="">
            <input type="submit" name="optionB" value="option B">
                </form>

            <form method="POST" action="">
            <input type="submit" name="optionC" value="option C">
                </form>

<form action="#"> . <form action="#"> no action means 'submit to current url', the pound sign mean 'the current document' and should not navigate. 没有动作表示“提交到当前网址”,井号表示“当前文档”,因此不应浏览。 though i'm not sure forms semantically allow this. 尽管我不确定表单在语义上是否允许这样做。 I would recommend posting the form data over ajax though. 我建议尽管通过ajax发布表单数据。 Its not hard to do, Jquery offers the most used ajax function and you basically loop though each input in a form and push the value into a uri-encoded string and post that via an ajax call. 这并不难,Jquery提供了最常用的ajax函数,您基本上遍历了表单中的每个输入,并将值推入uri编码的字符串中,并通过ajax调用将其发布。

I think this following code is not correct... if($username && $password) 我认为以下代码不正确... if($ username && $ password)

Try this 尝试这个

if(!empty($username) && !empty($password))

i think it shout be 我认为这是

 <?php
if($_POST)
{
$username = $_POST['username'];
$password = $_POST['password'];

if($username && $password) {
    //info is provided
    $queryget = mysql_query("SELECT * FROM users WHERE id='$username' AND password='$password'");
        $numrows = mysql_num_rows($queryget);

        if($numrows != 0) {

            //something has been found
            $_SESSION['id'] = $username;



        } else {
            echo "Wrong username/password";
            echo "<script>alert('Username/Password are wrong');</script>";
            echo "<script language='javascript'>window.location = 'ask_login.php';</script>";
        }
}
else {
    echo "Wrong username/password";
    echo "<script>alert('Username/Password are wrong');</script>";
    echo "<script language='javascript'>window.location = 'ask_login.php';</script>";
}
}
?>


          <form method="POST" action="">
          <input type="submit" name="details" value="Details">
          </form>


            <form method="POST" action="">
        <input type="submit" name="optionB" value="option B">
            </form>

        <form method="POST" action="">
        <input type="submit" name="optionC" value="option C">
            </form>

you can use jquery to process the form 您可以使用jQuery来处理表格

$.post( "test.php", $( "#testform" ).serialize(),function(e){alert(e);} );

test.php test.php的

<?php
    $username = $_POST['username'];
    $password = $_POST['password'];

    if($username && $password) {
        //info is provided
        $queryget = mysql_query("SELECT * FROM users WHERE id='$username' AND password='$password'");
        $numrows = mysql_num_rows($queryget);

        if($numrows != 0) {

            //something has been found
            $_SESSION['id'] = $username;



        } else {
            echo "Wrong username/password";
            echo "<script>alert('Username/Password are wrong');</script>";
            echo "<script language='javascript'>window.location = 'ask_login.php';</script>";
        }
}
else {
    echo "Wrong username/password";
    echo "<script>alert('Username/Password are wrong');</script>";
    echo "<script language='javascript'>window.location = 'ask_login.php';</script>";
}

?>

http://api.jquery.com/jquery.post/ http://api.jquery.com/jquery.post/

beware, no input validation! 当心,没有输入验证!

read this page 阅读此页

http://www.php.net/manual/en/function.mysql-real-escape-string.php http://www.php.net/manual/en/function.mysql-real-escape-string.php

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

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