Instructions on transferring input data from a text box to the next page using a session in PHP

Is there a way I can transfer the data from a text box to another page (demo2) and store all the information in my database? I attempted to use sessions, but although the value is being passed to the next page, the text box data isn't...

Check out the following code:

demo1.php

<?php
session_start();
 if($_POST)
 {
        if($_POST['act'] == "add")
    {
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $nationality = $_POST['nationality'];   

        $conn = mysql_connect("localhost","root","");
        $db = mysql_select_db("reg",$conn);

    }
}

echo "Session variables are set.";

if(@$_GET)
{
      $_SESSION["firstname"] = "$firstname";
      $_SESSION["lastname"] = " $lastname"; 
      $_SESSION["nationality"] = "$nationality"; 
      echo $firstname;
      echo $lastname;
      echo $nationality;
}
 ?>
    <!DOCTYPE html>
<html lang="en">

<head>

</head>

<body>
    <form  method="POST">

            <?php if(isset($_GET['id'])) { ?>
            <input type="hidden" name="act" value="edit"/>
            <input type="hidden" name="id" value="<?php echo $id; ?>"/>

            <?php } else{ ?>
            <input type="hidden" name="act" value="add"/>
            <?php } ?>

                                                                                <label> FirstName</label>

<input type="text" name="firstname" value="<?php echo @$firstname; ?>"><br>

     <label> LastName</label>

<input type="text" name="lastname" value="<?php echo @$lastname; ?>"><br>

<label> Nationality</label>

<input type="text" name="nationality" value="<?php echo @$nationality; ?>"><br>

 <a href="demo2.php"> <input type="button" id="next" name="next" value="next" >

<?PHP $_SESSION["firstname"] = "Preethi";
                                                          $_SESSION["lastname"] = " Rajan"; 
                                                          $_SESSION["nationality"] = "Indian";  ?>       
 </form>

</body>

</html>

demo2.php

<?php
session_start();
 if($_POST)

    {
        if($_POST['act'] == "add")

    {                                    

        $firstname= $_SESSION['firstname']; 
        $lastname= $_SESSION['lastname'];   
        $nationality= $_SESSION['nationality'];         

        $mobileno = $_POST['mobileno'];

        $email = $_POST['email'];

        $commaddr = $_POST['commaddr'];

        $city = $_POST['city'];

        $pincode = $_POST['pincode'];

        $state = $_POST['state'];

        $permaddr = $_POST['permaddr'];

        $conn = mysql_connect("localhost","root","");
        $db = mysql_select_db("reg",$conn);

    $sql = "INSERT INTO registration (firstname,lastname,nationality,mobileno,email,commaddr,city,pincode,state,permaddr) VALUES ('".$_SESSION["firstname"]."','".$_SESSION["lastname"]."' ,'".$_SESSION["nationality"]."', '".$mobileno."' , '".$email."' , '".$commaddr."','".$city."','".$pincode."','".$state."','".$permaddr."')";

    $rep = mysql_query($sql);
    }
}

    ?>
    <!DOCTYPE html>
<html lang="en">

<head>
</head>
    <form action="demo2.php" method="POST">

<?php if(isset($_GET['id'])) { ?>
            <input type="hidden" name="act" value="edit"/>
            <input type="hidden" name="id" value="<?php echo $id; ?>"/>

            <?php } else{ ?>
            <input type="hidden" name="act" value="add"/>
            <?php } ?>
<label> Mobile Number</label>

<input type="text" name="mobileno" value="<?php echo @$mobileno; ?>"><br>

<label> E Mail ID</label>

<input type="text" name="email" value="<?php echo @$email; ?>"><br>

<label> Communication Address</label>

<input type="text" name="commaddr" value="<?php echo @$commaddr; ?>"><br>

<label> City</label>

<input type="text" name="city" value="<?php echo @$city; ?>"><br>

<label>Pin Code</label>

<input type="text" name="pincode" value="<?php echo @$pincode; ?>"><br>

<label>State</label>

<input type="text" name="state" value="<?php echo @$state; ?>"><br>

<label> Permanent Address</label>

<input type="text" name="permaddr" value="<?php echo @$permaddr; ?>"><br>

<input type="submit" name="submit" value="submit"  style="height:50px; width:150px">

</form>
</body>

</html>

Answer №1

Swap out the anchor tag code for:

<a href="demo2.php?firstname=<?php $_POST['firstname'] ?>&lastname=<?php $_POST['lastname'] ?>&nationality=<?php $_POST['nationality'] ?>"> <input type="button" id="next" name="next" value="next" ></a>

Within demo2.php:

$fname = $_GET['firstname'];
$lname = $_GET['lastname'];
$nationality = $_GET['nationality'];

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Delaying CSS Keyframe animations

I'm having trouble getting the color squares in my table to appear one by one. I want each color to show up, then disappear, followed by the next color appearing and disappearing, creating a light sequence effect. Any advice or assistance would be gre ...

Ensuring the text remains positioned above another element constantly

Looking to have the text float above a regular box and resize the font size accordingly? Here's what I've tried: https://jsfiddle.net/a87uo3t2/ @import url('https://fonts.googleapis.com/css?family=Share+Tech+Mono'); body { margin: ...

Display function not functioning properly following AJAX request

I'm working on a functionality where I want to initially hide a table when the page loads, and then display it with the results when a form is submitted using Ajax. The issue I'm facing is that the code refreshes the page and sets the table back ...

Collecting, gathering content repeatedly

Hey there, just checking in. I'm currently working on extracting information related to CEO Pay Ratio and compiling links to this section for processing by a function. My goal is for the function to capture content starting from a specified tag and en ...

Guide to dynamically altering the header logo as you scroll further

I'm attempting to dynamically change the logo in my header based on how far I've scrolled down the page. While I have experimented with libraries like Midnight.js, I specifically need to display an entirely different logo when scrolling, not just ...

Getting a 400 Bad Request error while trying to send JSON data to a PHP page via socket

Currently, I am in the process of developing a program that can gather temperature and humidity data from a Pysense device and then store this information in a database on my laptop. To accomplish this, I have set up a socket to send Json data via POST to ...

Pixel information from previous canvas remains intact post resizing

I have crafted a canvas and loaded pixel data at specific locations using the following code snippet. let maskCanvas = document.createElement("canvas"); let patchWidth = 30; let patchHeight = 30; let scale = 3; maskCanvas.setAttribute("class", "mask"); ...

Styling the active selection in nav class with bold attribute in Bootstrap/AngularJs

How can I apply the bold attribute to the currently selected nav bar item? <ul class="nav"> <li role="presentation" ng-repeate="item in items" ng-class="{'active':navLink == item.header}"> </li> &l ...

Working with dynamic array sizes in methods and setters in PHP

I am relatively new to PHP and have been researching extensively, but I am struggling to find a solution to my current issue. My problem revolves around using setters in a class when dealing with an array of unknown size. For example: class Shirt { ...

What is the best way to transform this into an HTML readable code?

While browsing through a template, I came across this code and I'm unsure of how to get it functioning. I've tried removing the comments, but unfortunately it doesn't seem to be working as expected. li.dropdown.navbar-cart ...

What is a way to automatically run a function at specific intervals in PHP, similar to the setTimeout() function in JavaScript?

I have a JavaScript code snippet that looks like this: setTimeout('$.ajaxCall("notification.update", "", "GET");', 1000); Now, I want to execute the following PHP function every 1000 milliseconds, similar to the above JavaScript code: $notific ...

The <a href="#divtagid"> link is incapable of triggering the opening of the div tag when called from JavaScript

I need help with displaying the content of a div with the id "hello" in maindiv when clicking on the href "Click Here", but it's not working as expected. Here is the code I have: $(document).ready(function() { var newhtml = '<a href="#he ...

Creating an arrow line with CSS styling can be achieved easily

In order to create a horizontal line with a breakdown in the middle that displays an arrow, I want to use only HTML and CSS without relying on bitmap images. If needed, Font Awesome can be used to achieve the desired result. It's important for me to h ...

Utilize Laravel to trigger a route action based on a dropdown selection change, generating a unique URL

In my code, I have a dropdown select containing the list of the next 12 months: <select name="month" id="specificMonth"> @foreach(Carbon\CarbonPeriod::create(now()->startOfMonth(), '1 month', now()->addMon ...

Why isn't margin working with position: fixed?

I need help finding a solution for incorporating margin in a box within my App class. The issue is that the position fixed property doesn't seem to work as intended. <div className="App"> <div className="box"> ...

Why is my AngularJS application triggering two events with a single click?

<button id="voterListSearchButton" class="serachButton" ng-click="onSearchButton1Click()">find</button> This button allows users to search for specific information: $scope.onSearchButton1Click = function() { var partId = $("#partIdSearch" ...

Tips on showcasing array elements within a table's tbody section and including several values within the same array

I am struggling to figure out how to display array values in my table rows that I receive from input values. I have created an array, but I can't seem to find a way to display them in the table. Furthermore, I want to be able to add more values to th ...

Finding and removing the last portion of the innerHTML can be achieved by employing specific techniques

Looking to manipulate a <div> element that includes both HTML elements and text? You're in luck. I need to locate and remove either the last, nth-from-last, or nth plain text section within it. For example: <div id="foo"> < ...

Is your Ajax jQuery live search not functioning properly with JSON data?

My programming code is not functioning properly. Here is the file I am working on. When it does work, it does not display the list and gives an error in the Json file. I am unsure of the reason behind this issue. You will be able to view the error in the C ...

use triple quotes for python variable declaration

I am looking to extract information from an HTML file that contains elements with the class name "link". My challenge is to read each line into a variable and then parse it, all while using triple quotation marks. How can I create a string variable that ad ...