in php, why input form when submit it takes 2 time

2019-09-05 03:03发布

    <?php
        if ($_POST['submit']) {
            mysql_connect ("localhost", "root", "swt") or die ('Error: ' . mysql_error());
            mysql_select_db("db") or die ('Data error:' . mysql_error());
            $text = mysql_real_escape_string($_POST['comments']); 
            $query="INSERT INTO greetings (msg) VALUES ('$text')";
            mysql_query($query) or die ('Error updating database' . mysql_error());
            $id= mysql_insert_id();
            $url = "preview.php?id=".$id;
        }
    ?>
<form method="post" action="<? echo $url ?>" enctype="multipart/form-data" >
    <textarea name="comments" placeholder="please input your message"></textarea>
    <input name="submit" type="submit" value="submit" />
</form>

hello, sorry im newbie in PHP. i want to ask, why when i submit it must takes 2 times pressed before go to the preview.php

thanks.

标签: php forms submit
4条回答
Animai°情兽
2楼-- · 2019-09-05 03:13

First time the $url is empty so the browser is requesting same page, then the $url is changed, then injected to form so the next post will redirect to your preview.php file.
Just sent header for redirect.

header("Location: /preview.php?id=".$id);

so it will be:

  <?php
        if ($_POST['submit']) {
            mysql_connect ("localhost", "root", "swt") or die ('Error: ' . mysql_error());
            mysql_select_db("db") or die ('Data error:' . mysql_error());
            $text = mysql_real_escape_string($_POST['comments']); 
            $query="INSERT INTO greetings (msg) VALUES ('$text')";
            mysql_query($query) or die ('Error updating database' . mysql_error());
            $id= mysql_insert_id();
            $url = "preview.php?id=".$id;
            header("Location: $url");
        }
    ?>
<form method="post" enctype="multipart/form-data" >
    <textarea name="comments" placeholder="please input your message"></textarea>
    <input name="submit" type="submit" value="submit" />
</form>

Dont use action when you are wanting to send the request to the same page.
(Im assuming the php part in your snippet is the same file)

Also to send the redirect you need to not have html output before sending the header

查看更多
霸刀☆藐视天下
3楼-- · 2019-09-05 03:19

Change <?= $url?> to <?php echo $ url;?>.

Your server do not have short_open_tags enabled.

查看更多
再贱就再见
4楼-- · 2019-09-05 03:21

Your form action value ($url) is generated after first post is submitted, which means it is empty on first submit click. Better solution is to keep action value empty, and in post logic, instead only build $url value you can forward user to $url:

...
  $id= mysql_insert_id();
  $url = "preview.php?id=".$id;
  header('Location: '.$url);
}
查看更多
小情绪 Triste *
5楼-- · 2019-09-05 03:27

First change <? to <?php like..

<form method="post" action="<?php echo $url ?>" enctype="multipart/form-data" >
<textarea name="comments" placeholder="please input your message"></textarea>
<input name="submit" type="submit" value="submit" />

i like it that way..

Second open the firebug, click on the button and see what is the error you are getting..There is a possibility that you may have some error in the page, or check if your request is initiating or not on the first click. Also check if $url is not empty, on the first click..

查看更多
登录 后发表回答