<?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.
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:
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
Change
<?= $url?> to <?php echo $ url;?>.
Your server do not have short_open_tags enabled.
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:
First change
<?
to<?php
like..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..