Cannot get POST variables - php

I have a PHP file where I am posting variables to the same page but it is not working.
Here is the snippet of that page.
<?php
session_start();
require_once 'connect.php';
require_once 'protect.php';
//$_SESSION['uid'] = 1;
if(isset($_POST['shout']))
echo 'Posted';
if(isset($_POST['submit']))
echo 'Posted';
?>
<form method="POST" action="">
<input type="text" name="shout">
<input type="text" name="name">
<input type="submit" value="Shout!">
</form>

<input name="submit" type="submit" value="Shout!">
Name attribute is missing

<?php session_start();
require_once 'connect.php';
require_once 'protect.php';
//$_SESSION['uid'] = 1;
if(isset($_POST['button_name']))
echo 'Posted';
?>
<form name="form_name" method="POST" action="">
<input type="text" name="shout">
<input type="text" name="name">
<input name="button_name" type="submit" value="Shout!">
</form>
Not tested...Hope this helps...

Comment out the required files. If it shows "Posted", then PHP couldn't find the required files, or $_POST was changed within these files.
<?php session_start();
//require_once 'connect.php';
//require_once 'protect.php';
//$_SESSION['uid'] = 1;
if(isset($_POST['shout']))
echo 'Posted';
if(isset($_POST['submit']))
echo 'Posted';
?>
<form method="POST" action="">
<input type="text" name="shout">
<input type="text" name="name">
<input type="submit" name="submit" value="Shout!">
</form>

Related

the server request method prints GET even though the form method is POST

i wrote this code below and when i submit the form the $_SERVER["REQUEST_METHOD"] prints GET instead of POST even though the form method = "POST"
<?php
include "init.php";
include $temp . "navbar.php";
echo $_SERVER["REQUEST_METHOD"];
if($_SERVER["REQUEST_METHOD"]=='POST'){
echo $_POST["name"];
}
?>
<input type="hidden" value="admin" class="title">
<form action="index.php" method="POST">
<input type="text" name="name">
<input type="submit" value="ok">
</form>
<?php
include $temp . "foot.php";
?>
You can simply check if $_POST['name'] is set:
if(isset($_POST['name'])) {
// ...
}
And since you are posting to the same page you don't need to include the action attribute in your form.
<?php
include "init.php";
include $temp . "navbar.php";
if(isset($_POST['name']) {
echo $_POST['name'];
}
?>
<input type="hidden" value="admin" class="title">
<form method="POST">
<input type="text" name="name" value="" />
<input type="submit" value="ok">
</form>
<?php
include $temp . "foot.php";
?>

Cannot show data from PHP session

My point is after sending form show session data.
form.html:
<form action="sendForm.php" name="f" method="post" onsubmit="return sendForm()" >
<div class="form">
<label for="imie">Name</label>
<input type="text" id="nam" name="nam"/><br/>
<label for="nazwisko">Surname</label>
<input type="text" id="surname" name="surname"/><br/>
</div>
<input type="submit" name="send" id="send" value="send form"/><br/><br/>
</form>
sendForm.php:
<?php
session_start();
$_SESSION['nam'] = $_POST['nam'];
$_SESSION['surname'] = $_POST['surname'];
header("Location: view-struct.php");
?>
view-struct.php:
<span class="label">Name</span><?php echo $_SESSION['nam']; ?><br/>
<span class="label">Surname</span><?php echo $_SESSION['surname']; ?><br/>
[EDIT] Question is closed, forgot to add for session_start() in view-struct.php

Session variable can't be access to different page

I have a problem on getting the session variables to work on different pages
I have three php scripts
1. index.php (includes a html form)
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['billTo_email'] = $_POST['billTo_email'];
}
?>
<form id = 'form_submit' method="post" action = "NetworkOnline.php">
<td><input type="text" name="billTo_email" id="f_name2" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>
?>
NetworkOnline.php
<?php
session_start();
$_SESSION['billTo_email'] = $_POST["billTo_email"];
?>
$f_name=$_POST["f_name"];
$l_name=$_POST["l_name"];
$billTo_country=$_POST["billTo_country"];
$billTo_city=$_POST["billTo_city"];
$billTo_state=$_POST["billTo_state"];
$billTo_street1=$_POST["billTo_street1"];
$billTo_pin=$_POST["billTo_pin"];
$billTo_email = $_SESSION["billTo_email"];
response.php
<?php
session_start();
$_SESSION["billTo_email"] = $_POST["billTo_email"];
?>
<form name = "form1" method = "post" action="mail.php" >
<input type="hidden" name="order" id="order" value="<?php echo $order ?>" >
<input type="hidden" name="bank" id="bank" value="<?php echo $bank ?>" >
<input type="hidden" name="amount" id="amount" value="<?php echo $amount ?>" >
<input type = "test" name= "email" id ="email" value= "<?php echo $billTo_email ?>">
<input type="submit" id="submit" value="submit" name="submit">
</form>
Try this,
index.php:
<html>
<form id = 'form_submit' method="post" action = "NetworkOnline.php">
<td><input type="text" name="billTo_email" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>
</html>
NetworkOnline.php:
<?php
session_start();
$_SESSION['billTo_email'] = $_POST["billTo_email"];
header('Location: response.php');
?>
response.php:
<?php
session_start();
$billTo_email=$_SESSION["billTo_email"]
?>
<form name = "form1" method = "post" action="mail.php" >
<br />
<br />
<input type = "test" name= "email" value= "<?php echo $billTo_email ?>">
<input type="submit" id="submit" value="submit" name="submit">
</form>
Try below,
Your closing tags are wrong in place,
index.php
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['billTo_email'] = $_POST['billTo_email'];
}
?>
<form id = 'form_submit' method="post" action = "NetworkOnline.php">
<td><input type="text" name="billTo_email" id="f_name2" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>
NetworkOnline.php
<?php
session_start();
$_SESSION['billTo_email'] = $_POST["billTo_email"];
$f_name=$_POST["f_name"];
$l_name=$_POST["l_name"];
$billTo_country=$_POST["billTo_country"];
$billTo_city=$_POST["billTo_city"];
$billTo_state=$_POST["billTo_state"];
$billTo_street1=$_POST["billTo_street1"];
$billTo_pin=$_POST["billTo_pin"];
$billTo_email = $_SESSION["billTo_email"];
?>
response.php
<?php
session_start();
$_SESSION["billTo_email"] = $_POST["billTo_email"];
?>
<form name = "form1" method = "post" action="mail.php" >
<input type="hidden" name="order" id="order" value="<?php echo $order; ?>" >
<input type="hidden" name="bank" id="bank" value="<?php echo $bank; ?>" >
<input type="hidden" name="amount" id="amount" value="<?php echo $amount; ?>" >
<input type = "test" name= "email" id ="email" value= "<?php echo $billTo_email; ?>">
<input type="submit" id="submit" value="submit" name="submit">
</form>
UPDATE:
your index.php
<form id = 'form_submit' method="post" action = "NetworkOnline.php">
<td><input type="text" name="billTo_email" id="f_name2" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>
NOTE: NO need to check if (isset($_POST['submit'])) { ... } here, because index.php send data to NetworkOnline.php.
Your NetworkOnline.php:
<?php
ob_start(); // turn on output buffering
session_start();
if( isset( $_POST['billTo_email'] ) ) { // work only if **billTo_email** has some value
$_SESSION['billTo_email'] = $_POST["billTo_email"];
}
// ?> <-- remove this closing tag
$f_name=$_POST["f_name"]; // this line of code shows undefined variable,
//because in your index.php there is no any f_name field presnet.
// your remaining codes
?>

TwoPHP blocks in file

i have two HTML forms and two PHP blocks in one file (index.php). for example i want the second php script belonged to the second form. i dont know, how to do it. What i write to the action atribute ?
here is my code:
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php
echo $_POST ["name"];
?>
<?php
echo $_POST ["age"];
?>
Hope it helps you,
First form,
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?> ">
<input type="text" name="name"> <br>
<input type="submit" name='submit' >
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST ["name"];
}
?>
Second form
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?>">
<input type="text" name="age"> <br>
<input type="submit" name='submitsecond' > // name submitsecond indicates as second form
</form>
<?php
if(isset($_POST['submitsecond'])){
echo $_POST ["age"];
}
?>
You can use a hidden input field to distinguish both scripts. And you'll have to echo/print the script name ($_SERVER['PHP_SELF']), htmlspecialchars is not needed...
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="name_form" />
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="age_form" />
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php if($_POST['form'] == 'name_form'): ?>
The name form is submitted.<br>
Name: <?php echo $_POST['name']; ?>
<?php endif; ?>
<?php if($_POST['form'] == 'age_form'): ?>
The age form is submitted.<br>
Age: <?php echo $_POST['age']; ?>
<?php endif; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER ["PHP_SELF"]); ?>">
<input type="text" name="name"> <br>
<input type="submit" name="name_sub">
</form>
<?php
if(isset($_POST ["name_sub"])) // check if name form is submit
echo $_POST ["name"];
?>

value preserving across multiple pages

i am trying to post the some data on another page at this i have a back button onclick of back button i wanna reload the original page with posted data.
use $_SESSION:
Page1.php
<?php
session_start();
$name = isset($_SESSION['name']) ? $_SESSION['name'] : '';
?>
<html>
<form method="post" action="Page2.php">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="submit" value="Submit" />
</form>
</html>
Page2.php
<?php
session_start();
if (isset($_POST['name']))
{
$_SESSION['name'] = $_POST['name'];
}
$name = $_SESSION['name'];
?>
<html>
<form>
<span><?php echo $name; ?>" </span>
Back
</form>
</html>
you can use $_SESSION
<?php
if (!isset($_SESSION)) session_start();
if ($_POST['btnsubmit']){
$_SESSION['data1'] = $_POST['data1'];
$_SESSION['data2'] = $_POST['data2'];
$_SESSION['data3'] = $_POST['data3'];
//... and so on
}
?>
<form>
<input type="text" id="data1" value="<? echo $_SESSION['data1'];?>"/>
<input type="text" id="data2" value="<? echo $_SESSION['data2'];?>"/>
<input type="text" id="data3" value="<? echo $_SESSION['data3'];?>"/>
<input type="submit" id="btnsubmit" value="Submit"/>
</form>
<!-- and so on -->
Hope this gives you an idea

Categories