Reputation: 5
I have this code of php for the method of o the posted form.
config.php
<?php
include('db.php');
function string_limit_words($string, $word_limit) {
$words = explode(' ', $string);
return implode(' ', array_slice($words, 0, $word_limit));
}
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$title=mysql_real_escape_string($_POST['title']);
$body=mysql_real_escape_string($_POST['body']);
$title=htmlentities($title);
$body=htmlentities($body);
$newtitle=string_limit_words($title, 6);
$urltitle=preg_replace('/[^a-z0-9]/i',' ', $newtitle);
$newurltitle=str_replace(" ","-",$newtitle);
$date=date("Y/m/d");
$url=$date.'/'.$newurltitle.'.html';
mysql_query("insert into blog(title,body,url) values('$title','$body','$url')");
}
?>
With the code above, I successfully posted the form and I can find the file. However, after posting the form, the config.php just leave blank. What I want is it redirect to another page like congratz.html page.
congratz.html
<body>
<h2>CONGRATZ, YoOU HAVE SUCCESSFULLY POST THE FORM TO THE DATABASE</h2>
</body>
What I know to achieve this is to add the header line like:
header( 'Location: http://www.example.com/congratz.html');
in the config.php.
However I am not sure how to do that or where to put the header in the config.php.
I am just learning php and self-taught. Please help. Thanks.
Upvotes: 0
Views: 62
Reputation: 2509
Add the this after your query
if(mysql_affected_rows()>0)//checking weather the query worked or not
{
header( 'Location: http://www.example.com/congratz.html');
}
So your whole code should look like this
<?php
include('db.php');
function string_limit_words($string, $word_limit) {
$words = explode(' ', $string);
return implode(' ', array_slice($words, 0, $word_limit));
}
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$title=mysql_real_escape_string($_POST['title']);
$body=mysql_real_escape_string($_POST['body']);
$title=htmlentities($title);
$body=htmlentities($body);
$newtitle=string_limit_words($title, 6);
$urltitle=preg_replace('/[^a-z0-9]/i',' ', $newtitle);
$newurltitle=str_replace(" ","-",$newtitle);
$date=date("Y/m/d");
$url=$date.'/'.$newurltitle.'.html';
mysql_query("insert into blog(title,body,url) values('$title','$body','$url')");
if(mysql_affected_rows()>0)//Checking weather the query worked or not
{
header( 'Location: http://www.example.com/congratz.html');
}
}
?>
Also mysql is deprecitaed, learn mysqli or PDO
For mysqli function check this link http://php.net/manual/en/book.mysqli.php
For PDO function check this link http://php.net/manual/en/book.pdo.php
And for learn about header check this link http://php.net/manual/en/function.header.php
Upvotes: 1
Reputation: 1136
Simply put the header after the query.
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$title=mysql_real_escape_string($_POST['title']);
$body=mysql_real_escape_string($_POST['body']);
$title=htmlentities($title);
$body=htmlentities($body);
$newtitle=string_limit_words($title, 6);
$urltitle=preg_replace('/[^a-z0-9]/i',' ', $newtitle);
$newurltitle=str_replace(" ","-",$newtitle);
$date=date("Y/m/d");
$url=$date.'/'.$newurltitle.'.html';
mysql_query("insert into blog(title,body,url) values('$title','$body','$url')");
header( 'Location: http://www.example.com/congratz.html');
}
Upvotes: 0