Post method prevents the page to load
I have created a page called profile.php that gets a value from mainpage.php, by using "get" method. But, If the value that was sent from mainpage.php is empty or wrong, the page redirects to the mainpage.php. However, I used "post" method in profile.php that allows users to post something in profile.php. So that If the user submits something, profile.php reloads and reloading profile.php makes the variable, that i get from mainpage, empty and directs the page to mainpage.php unwillingly.How can i fix it? Codes;
$another_user = $_GET['username'];//gets a value from mainpage.php
$check = mysql_query("SELECT * FROM users WHERE user_name = '".$another_user."'");
$sent = mysql_fetch_array($check);
if(!$sent)
{
header('Location: mainpage.php');
exit();
}
//some codes around here
<form action="profile.php" method="post">
Commet: <input type="text" name="comment" placeholder = "comments?"/>
<input type="submit"/>
</form>
<?php开发者_运维问答
if(isset($_POST['comment'])&&!($_POST['comment']=""))
{
$writing = $_POST['comment'];
echo $writing;
}
Thanks
To start with you have a syntax error in your second line, as the colors show you already. So change it into:
$check = mysql_query("SELECT * FROM users WHERE user_name = '".$another_user."')";
There are many solutions to your problem. One of them could be to verify which page the user is coming from. If this is mainpage.php
you can verify the username
and if this is profile.php
you should check the comment
variable.
You can make use of the $_SERVER['HTTP_REFERER']
variable to check which is the referer. So something like:
if ($_SERVER['HTTP_REFERER'] == "http://www.domain.com/mainpage.php") {
//do your username check
}
else if ($_SERVER['HTTP_REFERER'] == "http://www.domain.com/profile.php") {
//do your comment check
}
Another and maybe easier way would be to make sure that your $_POST['comment']
has not been set when dealing with the username
.. Like this:
if (!isset($_POST['comment'])) {
$another_user = $_GET['username'];//gets a value from mainpage.php
$check = mysql_query("SELECT * FROM users WHERE user_name = '".$another_user."'");
$sent = mysql_fetch_array($check);
if(!$sent) {
header('Location: mainpage.php');
exit();
}
}
else {
//...
}
精彩评论