简体   繁体   English

更改操作页面后,表单将不会提交

[英]Form won't submit when action page is changed

I have a simple page (below) with a form. 我有一个带有表单的简单页面(下)。 The form will submit if the action='ask.php'. 如果action ='ask.php',则表单将提交。 (ask.php is the current page and the code below). (ask.php是当前页面和下面的代码)。 When i change ask.php to question.php (another page on in the folder) the form does not submit. 当我将ask.php更改为question.php(文件夹中的另一页)时,表单未提交。 Why is this? 为什么是这样? Is the form redirecting before the data is placed in the database? 在将数据放入数据库之前,表单是否会重定向?

ask.php code; ask.php代码;

<?php session_start(); ?>
       <!DOCTYPE html>
      <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <title></title>

      <script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
      <script type='text/javascript' src='http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js'></script>
        <link rel="stylesheet" href="../css/bootstrap.css" type="text/css" media="screen" />

         </head>
          <body>
       <?php include '../css/bar.php'; ?>
    <div id='content'>
<?php include '../nav.php'; 
?>



<?php


if (isset($_POST['submit'])){
include '../connect.php';
$question=mysql_real_escape_string($_POST['question']);
$detail=mysql_real_escape_string($_POST['detail']);
$date=date("d M Y");
$time=time();
$user=$_SESSION['id'];
$put=mysql_query("INSERT INTO questions VALUES ('','$question','$detail','$date','$time','$user','subject','0')");

$result=mysql_query("SELECT * FROM questions WHERE user='$user' AND time='$time'");
    while ($row = mysql_fetch_assoc($result)){

         $q=$row['id'];

        }
}

?>

<form method='POST' action='question.php'>   //won't submit because action is question.php?
    <p>Question:</p>
    <p><input type='text' name='question' id='question'  maxlength='200'></p>
    <p>Add some detail (optional):</p>
    <p><textarea id='detail' name='detail' ></textarea></p>

    <p><input type='submit' value='submit' name='submit'></p>
</form>


</div>
<?php include '../footer.php'; ?>
</body>

Jeroen is right: Jeroen是对的:

The action page should include this kind of information, in order to get the information to the database: 操作页面应包含此类信息,以便将信息获取到数据库:

Part one and two make the data ready to be inserted into database: 第一部分和第二部分准备将数据插入数据库:

$question = mysql_real_escape_string($_POST['question']); // Part 1
$detail = mysql_real_escape_string($_POST['detail']); // Part 2
$con = mysql_connect("server","username","password");
mysql_select_db("YourDatabase", $con);

$sql="INSERT INTO YourTable (question, details) VALUES ('$question','$detail')";
if (!mysql_query($sql,$con)) {
    //Some error handling here,
    // Stay away from using die in all but development
}
mysql_close($con)

You should test format with preg_match, too, to check data is in expected format. 您也应该使用preg_match测试格式,以检查数据是否符合预期格式。 Hope that helps 希望能有所帮助

EDIT! 编辑!

This method of connection is outdated! 这种连接方法已经过时了! Use PDO for db connection. 使用PDO进行数据库连接。 Some docs 一些文档

PDO Article PDO文章

PDO php docs PDO PHP文档

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

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