Asked  7 Months ago    Answers:  5   Viewed   35 times

I have a form on my homepage and when it is submitted, it takes users to another page on my site. I want to pass the form data entered to the next page, with something like:

<?php echo $email; ?>

Where $email is the email address the user entered into the form. How exactly do I accomplish this?

 Answers

41

The best way to accomplish that is to use POST which is a method of Hypertext Transfer Protocol https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

index.php

<html>
<body>

<form action="site2.php" method="post">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
<input type="submit">
</form>

</body>
</html> 

site2.php

 <html>
 <body>

 Hello <?php echo $_POST["name"]; ?>!<br>
 Your mail is <?php echo $_POST["mail"]; ?>.

 </body>
 </html> 

output

Hello "name" !

Your email is "whatyou@addedonindex.com" .

Wednesday, March 31, 2021
 
VieStar
answered 7 Months ago
32
while($row = mysqli_fetch_array($sql)){
    echo '<td>
              <img src="'.$row['image'].'"><br/>
              <form method="post" action="edit-record.php">
                  <input type="text" name="imgID" value="'.$row['id'].'">
                  <input type="submit" value="Edit" id="edit_btn" class="admin_btn">
              </form>
          </td>';
}

in edit-record.php...

<?php
    echo $_POST['imgID'];
?>

There is no reason your code technically wouldn't work but instead you could just eliminate the form and use a simple link...

while($row = mysqli_fetch_array($sql)){
    echo '<td>
              <img src="'.$row['image'].'"><br/>
              <a href="edit-record.php?id='.$row['id'].'">edit</a>
          </td>';
}

and in edit-record.php...

<?php
    echo $_GET['id'];
?>
Wednesday, March 31, 2021
 
TuomasR
answered 7 Months ago
73

Page 1:

session_start();

if(isset($_POST['post'])) {
    $_SESSION['country'] = $_POST['country'];
    if (($_POST['country'] == 'US')) {
        header("Location: US_Products.php"); }
    elseif (($_POST['country'] == 'CDN')) {
        header("Location: CDN_Products.php"); }
    else { die("Error"); }
    exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>

Page 2:

session_start();
<?php echo $_SESSION['country']; ?>

Or using include method, just use one page:

session_start();

if(isset($_POST['post'])) {
    if (($_POST['country'] == 'US')) {
        include("US_Products.php"); }
    elseif (($_POST['country'] == 'CDN')) {
        include("CDN_Products.php"); }
    else { die("Error"); }
    exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>

and you should be able to use echo $_POST['country'] on US_Products.php and CDN_Products.php, or

Wednesday, March 31, 2021
 
Student
answered 7 Months ago
98

Try using a separate variable for errors, and not output error messages to the input field.

You could use global variables for this, but I'm not fond of them.

login.php

<?php 
$firstname = '';
$password  = '';
$username  = '';
$emailadd  = '';
$response  = '';
include_once('loginprocess.php');
include_once('includes/header.php);
//Header stuff
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8");?>" method="post">
    <fieldset>
        <p>Please enter your username and password</p>
    <legend>Login</legend>
        <div>
        <label for="fullname">Full Name</label>
            <input type="text" name="fname" id="fullname" value="<?php echo $firstname ?>" />
        </div>
        <div>
         <label for="emailad">Email address</label>
         <input type="text" name="email" id="emailad" value="<?php echo $emailadd; ?>"/>
        </div>
        <div>
        <label for="username">Username (between 5-10 characters)</label>
        <input type="text" name="uname" id="username" value='<?php echo $username; ?>' />
        </div>
        <div>            
        <label for="password">Password (between 8-10 characters)</label>
        <input type="text" name="pw" id="password" value="<?php echo $password; ?>" />
        </div>
        <div>            
            <input type="submit" name="" value="Submit" />
        </div>
    </fieldset>
</form>
<?php
//Output the $reponse variable, if your validation functions run, then it
// will contain a string, if not, then it will be empty.
if($response != ''){
    print $response;         
}     
?>
//Footer stuff

loginprocess.php

//No need for header stuff, because it's loaded with login.php
if($_SERVER['REQUEST_METHOD'] == 'POST'){//Will only run if a post request was made.
    //Here we concatenate the return values of your validation functions.
    $response .= validate_fname();
    $response .= validate_email();
    $response .= validate_username();
    $response .= validate_pw();
}    
//...or footer stuff.

functions.php

function validate_fname() {
    //Note the use of global...
    global $firstname;
    if (!empty($_POST['fname']))    {
        $form_is_submitted = true;
        $trimmed = trim($_POST['fname']);
        if(strlen($trimmed)<=150  && preg_match('/\s/', $trimmed)){
            $fname = htmlentities($_POST['fname']);
            //..and the setting of the global.
            $firstname = $fname;
            //Change all your 'echo' to 'return' in other functions.
            return"<p>You entered full name: $fname</p>";
        } else {
            return "<p>Full name must be no more than 150 characters and must contain one space.</p>";
        }
    }
}

I wouldn't suggest using includes for small things like forms, I find it tends to make a mess of things quite quickly. Keep all your 'display' code in one file, and use includes for functions (like you have) and split files only when the scope has changed. i.e your functions.php file deals with validation at the moment, but you might want to make a new include later that deals with the actual login or registration process.

Look at http://www.php.net/manual/en/language.operators.string.php to find out about concatenating.

Saturday, May 29, 2021
 
LoicTheAztec
answered 5 Months ago
84

If you have no option to use server-side programming, such as PHP, you could use the query string, or GET parameters.

In the form, add a method="GET" attribute:

<form action="display.html" method="GET">
    <input type="text" name="serialNumber" />
    <input type="submit" value="Submit" />
</form>

When they submit this form, the user will be directed to an address which includes the serialNumber value as a parameter. Something like:

http://www.example.com/display.html?serialNumber=XYZ

You should then be able to parse the query string - which will contain the serialNumber parameter value - from JavaScript, using the window.location.search value:

// from display.html
document.getElementById("write").innerHTML = window.location.search; // you will have to parse
                                                                     // the query string to extract the
                                                                     // parameter you need

See also JavaScript query string.


The alternative is to store the values in cookies when the form is submit and read them out of the cookies again once the display.html page loads.

See also How to use JavaScript to fill a form on another page.

Wednesday, June 2, 2021
 
KingCrunch
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share