Pass Form Data to Another Page with PHP

Pass form data to another page with php

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" .

How to pass form data to another page and database both?

You can pass your tablename through get method

CreateTable.php

<?php 

$conn = new mysqli("localhost","root","","mywebsite");
$tableName = $_POST['tableName'];

if (isset($_POST['tbButton'])) {
$qry = "Create Table ".$tableName ."(firstname varchar(25),lastname varchar(25));";
$res = mysqli_query($conn,$qry);
if ($res) {
header("Location: EnterData.php?tableName=".$tableName);
}
else{
die("query failed!");
}
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Create Table</title>
</head>
<body>
<form action="CreateTable.php" method="post">
<p><input type="text" name="tableName" placeholder="Enter Table Name..."></p>
<p><input type="submit" name="tbButton"></p>
</form>
</body>
</html>

EnterData.php

<?php 
$tbname = $_GET['tableName'];
$conn = new mysqli("localhost","root","","mywebsite");

if (isset($_POST['dataButton'])) {

$qry = "Insert into ".$tbname."(firstname,lastname) values('".$_POST['firstname']."','".$_POST['lastname']."');";
$res = mysqli_query($conn,$qry);
if ($res) {
echo "Data Inserted!";
}
else{
die("query failed!");
}
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Create Table</title>
</head>
<body>
<form action="EnterData.php?tableName=<?php echo $tbname;?>" method="post">
<p><input type="text" name="firstname" placeholder="Enter First Name..."></p>
<p><input type="text" name="lastname" placeholder="Enter Last Name..."></p>
<p><input type="submit" name="dataButton"></p>
</form>
</body>
</html>

How to pass form data to another page in Laravel

I was given an answer on another forum. This solution will take the id that the form passes it, then will fetch the corresponding area objects and pass these in an array to the show_comparisons.blade.php view.

public function show_comparison(Request $request)
{

$areas= Area::whereIn('id',$request->area)->get();

return view('areas.show_comparison', compact('areas'));
}

Passing form data from one web page to another with PHP

I would say for security reasons, do not use Get method "$_GET[]" as people described, keep POST as you have it.

All you need to do on register/ page is get all the values passed on using the POST method and populate them into your HTML. So the second form should look like:

    <form id="register" name="form1" method="post" action="send_contact.php">
<fieldset>
<li><label>First Name</label>
<input type="text" id="first_name" name="first_name" value="<?=$_POST[first_name]?>" />
</li>

<li>
<label>*Last Name</label>
<input type="text" id="last_name" name="last_name" value="<?=$_POST[last_name]?>" />
</li>

<li>
<label>*Email</label>
<input type="text" id="email" name="email" value="<?=$_POST[email]?>" />
</li>

<li>
<label>*Confirm Email</label>
<input type="text" id="confirm-email" name="confirm_email" />
</li>

<li>
<label>Street Address</label>
<input type="text" id="address" name="address" value="<?=$_POST[address]?>" />

<li class="full-width">
<input id="submit" type="submit" value="Register" />
</li>

</fieldset>
</form>

Above, I am using shorthand version of "echo" and php tags, if you do not have that enabled under php.ini, please change "" to "; ?>. Also, script will not populate "confirm" email as I assume you would like the user to retype that.

That should do it.

Sending Form Data Using POST To The Current Page And Another PHP Page At The Same Time

Here is your current page

<?php
$_POST["studioName"] = 'testdata';
$valid = true;

if(empty($_POST["studioName"])){
$studioNameError = " *This Field Cannot Be Empty";
$valid = false;
}

// send data to getstudio.php if valid
if($valid){ ?>
<form method="post" action="getstudio.php" id="senddata">
<input type="hidden" name="postdata" value="<?php print_r($_POST); ?>">
</form>
<script>
document.getElementById("senddata").submit();
</script>
<?php } ?>

Here is your getstudio.php file to receive validated post data

<?php
echo '<pre>';

print_r($_POST['postdata']);

die;

How can I pass form data from one html file to another without JS/PHP?

So this is long gone but I was actually able to resolve my problem without using anything other than basic HTML , so here's how I did it for anyone else who's trying to find the answer to problem (probably not, you don't usually do this professionally and basically this was a challenge from a friend).

So, two things.

  1. SessionStorage
  2. LocalStorage

This is built-in to your browser and you can use it to achieve simple tasks by simply assigning values to it. They'll remain there and you can use however you want.

But, as the name implies, sessionstorage will only retain those values during the session (the time you have your browser open for) while localstorage can retain it indefinitely. Not sure if I can link other sites over here so just Google these terms to learn more and how to use them.



Related Topics



Leave a reply



Submit