Reputation: 175
I am new in PHP and I am just doing a simple input form webpage. problem is that after the submission website goes to the blank page, I want it to go back to the original one. I have read here that it may be helped by adding header() to my code but it doesn't help. Here is my code:
<!DOCTYPE html>
<html>
<head>
<title> Sample Form </title>
<link href = "stl.css" rel = "stylesheet" type="text/css" />
</head>
<body>
<form method="post" action="connect.php">
<input class = "PaganForm" id = "name" type="text" name="name" value="">
<input class = "PaganForm" id = "img" type="text" name="img" value="">
<input class = "PaganForm" id = "dsc" type="text" name="dsc" value="">
<input type="submit" id = "SubmitButton" >
</form>
<?php
header("Location: http://localhost/simple-form.php");
exit;
$servername = 'localhost';
$username = 'root';
$password = '';
$database = 'cero_db';
$conn = new mysqli($servername, $username, $password,$database);
$sql = "SELECT * FROM pg";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$newName = $row['Name'];
$newLink = $row['Link'];
$newDsc = $row['Description'];
echo "<h1>".$newName."</h1>";
echo "<img src = \"".$newLink."\" />";
echo "<p>".$newDsc."</p>";
}
?>
<body>
</html>
Here is connect.php:
<?php
$name = $_POST["name"];
$lnk = $_POST["img"];
$dsc = $_POST["dsc"];
$servername = 'localhost';
$username = 'root';
$password = '';
$database = 'cero_db';
$conn = new mysqli($servername, $username, $password,$database);
$sql = "INSERT INTO pg (Name, Link, Description)
VALUES ('".$name."', '".$lnk."', '".$dsc."')";
$conn->query($sql);
?>
Any suggestions?
P.S. I could not find any questions that helped me so if this is duplicate just link the original and I will delete this.
Upvotes: 4
Views: 3683
Reputation: 5987
Sample example for your code...
simple-form.php
<!DOCTYPE html>
<html>
<head>
<title> Sample Form </title>
<link href = "stl.css" rel = "stylesheet" type="text/css" />
</head>
<body>
<form method="post" action="connect.php">
<input class = "PaganForm" id = "name" type="text" name="name" value="">
<input class = "PaganForm" id = "img" type="text" name="img" value="">
<input class = "PaganForm" id = "dsc" type="text" name="dsc" value="">
<input type="submit" id = "SubmitButton" >
</form>
<body>
</html>
connect.php
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$database = 'cero_db';
$conn = new mysqli($servername, $username, $password,$database);
$sql = "SELECT * FROM pg";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$newName = $row['Name'];
$newLink = $row['Link'];
$newDsc = $row['Description'];
echo "<h1>".$newName."</h1>";
echo "<img src = \"".$newLink."\" />";
echo "<p>".$newDsc."</p>";
}
header("Location: simple-form.php");
exit;
?>
Upvotes: 0
Reputation: 156
Try this
header("Location: simple-form.php");
You have to add this in connect.php
, because your submit-button in the form will direct you to connect.php
.
Your connect.php should look like this:3
<?php
$name = $_POST["name"];
$lnk = $_POST["img"];
$dsc = $_POST["dsc"];
$servername = 'localhost';
$username = 'root';
$password = '';
$database = 'cero_db';
$conn = new mysqli($servername, $username, $password,$database);
$sql = "INSERT INTO pg (Name, Link, Description)
VALUES ('".$name."', '".$lnk."', '".$dsc."')";
$conn->query($sql);
header("Location: simple-form.php");
?>
Upvotes: 2
Reputation: 1377
I will write it here before it get clogged under comments:
you need to move your header function above anything that will be sent to the browser. Since you are sending some html before calling header, the headers are already sent with your html code. read about it http://php.net/manual/en/function.header.php it is widely asked on SO as well search for php header redirect
Therefore you need to handle the form beofre you send any output or use output buffering. I sugget you check out http://php.net/manual/en/function.ob-start.php
You could also use javascript to send you user to the right place.
Upvotes: 1