Pradeep Rao
Pradeep Rao

Reputation: 11

Passing php variable data from one page to another

I have passed value from a page saying a.php to b.php. That value has been saved in a variable. I'm trying to pass that value to a different page say c.php to save in db, but the value being passed is NULL.

I have tried to use session, cookie and other form passing methods. A.php also has other values and fields which is working fine. I only need this one to be fixed. Thanks.

a.php

<form action="b.php" method="post">
<input type="text" name="varname">
<input type="submit">
//then there is rest of the other fields and codes on the page

b.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "edg_dsh";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$a_num = $_POST['varname']; //value sent by a.php
?>
<html>
<head></head>
<body>
<p> <?php echo $a_num ?> This is the value a has passed </p>

<form action="c.php" method="post">
<input type="text" name="b_var" value="<?php $a_num ?>" placeholder="<?php echo $a_num ?>" readonly/>
<input type="text" name="name"/>
<input type="submit">
</form>
</body>
</html>

c.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "edg_dsh";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$b_var=$_POST["b_var"];
$name=$_POST["name"];

if($b_var==NULL)
{
    echo "Field is empty";
}
else{
$sql = "INSERT INTO testing (var, name) VALUES ('$b_var', '$name')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}

$conn->close();

?>

I need the $b_var to have value passed from a.php, not null.

Upvotes: 0

Views: 76

Answers (3)

Pupil
Pupil

Reputation: 23958

Your HTML <input tag has problems:

1) You have not put echo in the value

2) You have not closed the value and placeholder attributes.

3) Also, suggestion: HTML <input tag is self closing, so please put /> instead of > in the end.

Corrected HTML:

<input type="text" name="b_var" value="<?php echo $a_num;?>" placeholder="<?php echo $a_num ?>: readonly/>

Upvotes: 1

karthzDIGI
karthzDIGI

Reputation: 403

In b.php

Replace this line

<input type="text" name="b_var" value="<?php $a_num ?> placeholder="<?php echo $a_num ?> readonly>

with

<input type="text" name="b_var" value="<?php echo $a_num; ?>" placeholder="<?php echo $a_num; ?>" readonly>

and try.

Upvotes: 1

Syed mohamed aladeen
Syed mohamed aladeen

Reputation: 6755

you have missed echo in that textbox value

try this

<input type="text" name="b_var" value="<?php echo $a_num ?>" placeholder="<?php echo $a_num ?>" readonly>

Upvotes: 1

Related Questions