Home > Back-end >  Saving and displaying user input using HTML and PHP
Saving and displaying user input using HTML and PHP

Time:05-21

I am trying to save a Users data from an input field so that it can be displayed later in their profile of a webpage, for example the user inputs data of a cinema(name, address) and can see it later under Saved Restaurants and call up the previously saved information. Can the PHP and HTML code be written together in one .PHP file?

So far I have this:

<html lang>

<head>

    <link rel="stylesheet" href="css/addOrEditCinemaPage.css">

</head>

<?php include "php/head.php" ?>
<?php include "php/navigation.php" ?>

<body>

<div >
    <form>
        <h2>Add or Edit a Cinema</h2>

        <label for="name"><b>Name of Cinema</b></label>
        <input type="text" placeholder="Enter name">

        <label for="str"><b>Street</b></label>
        <input type="text" placeholder="Enter street">

        <label for="nr"><b>Nr.</b></label>
        <input type="number" placeholder="Enter Nr.">


        <label for="plz"><b>Post Code</b></label>
        <input type="number" placeholder="Enter Post Code"><br><br>

        <label for="ct"><b>City</b></label>
        <input type="text" placeholder="Enter City">

        <label for="sta"><b>State</b></label>
        <input type="text" placeholder="Enter State">

        <label for="descr"><b>Description</b></label><br>
        <textarea placeholder="Enter Discription"></textarea>


        <div >

            <a href="image_upload_form.php"   aria-disabled="false">Add Image</a>

        </div>
        <button type="submit">Save</button>

    </form>
</div>

<?php include "php/footer.php" ?>
</body>

</html>```

Can the PHP to save and display to input infomration also be written here?

CodePudding user response:

Yes, you can by setting the 'action' attribute of the form to the same file, and by setting the 'method' attribute to POST. Instead of using

<form>

use

<form action="<?PHP echo $_SERVER['php_self'];?>" method="POST">

Then, set the 'name' attribute of each input.

For example, Instead of using

<input type="text" placeholder="Enter name">

use

<input type="text" name="name" placeholder="Enter name">

You'll also have to set the 'name' attribute of the submit button to 'submit':

<button type="submit" name="submit">Save</button>

Once you've done that, the PHP code to access the form data would be:

if (isset($_POST['submit'])) {
    echo $_POST['name'];
}

CodePudding user response:

send your data with html to another php page

you must use get or post for sending form data to php page

like below

  • Related