Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go back to page after submitting form using PHP

Tags:

html

php

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.

like image 951
GeorgeDopeG Avatar asked Nov 16 '25 22:11

GeorgeDopeG


2 Answers

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");
?>
like image 129
SacrumDeus Avatar answered Nov 18 '25 13:11

SacrumDeus


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.

like image 30
Louis Loudog Trottier Avatar answered Nov 18 '25 13:11

Louis Loudog Trottier