0

I am trying to pass data from one page to another in php file and I don't know a way to make that work.

Can someone help me please!

That is my form page and i try to submit it to the second page

First page

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="add.css">
</head>

<body>
    <div class="flex_container">
        <div class="product_add"><b>Product Add</b></div>
        <button type="submit" name="submit" id="save_btn">Save</button>
    </div>

    <hr size="2" color="rgb(156,1,0)" width="95%" style="clear: both;">
    <br>
    <div>
        <form action="main.php" method="post" id="product_form">
            <div>
                <label for="sku">SKU</label>
                <input type="text" id="sku" name="fname">
            </div>
            <div>
                <label for="name">Name</label>
                <input type="text" id="name" name="fname">
            </div>
            <div>
                <label for="number">Price ($)</label>
                <input type="number" id="price" min="1" step="any" />
            </div>
            <div>
                <label for="type">Type Switcher</label>
                <select id="productType" onclick="display();">
                    <option value="null" selected disabled>Choose one...</option>
                    <option value="disc" id="DVD">DVD-disc</option>
                    <option value="book" id="Book">Book</option>
                    <option value="furniture" id="Furniture">Furniture</option>
                </select>
                <div id="a" style="display: none;">
                    <label for="size">Size (MB)</label>
                    <input id="size" type="number" />
                    <h4>Please provide Size in MB !</h4>
                </div>
                <div id="b" style="display: none;">
                    <label for="weight">Weight (KG)</label>
                    <input id="weight" type="number" />
                    <h4>Please provide Weight in KG !</h4>
                </div>
                <div id="c" style="display: none;">
                    <div>
                        <label for="height">Height (CM)</label>
                        <input id="height" type="number" />
                    </div>
                    <div>
                        <label for="width">Width (CM)</label>
                        <input id="width" type="number" />
                    </div>
                    <div>
                        <label for="length">Length (CM)</label>
                        <input id="length" type="number" />
                    </div>
                    <h4>Please provide Height, Width, and Length in CM !</h4>
                </div>
            </div>
        </form>
    </div>
    <script src="add.js"></script>
</body>

</html>

And here is the page where i would like the form to be submitted

Second page

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="main.css">
</head>

<body>
    <div class="flex_container">
        <div class="product_list"><b>Product List</b></div>
        <a href="add-product.html"><button id="add_button">ADD</button></a>
        <button id="delete_button">MASS DELETE</button>
    </div>
    <hr size="2" color="rgb(156,1,0)" width="95%" style="clear: both;">
    <div>

    </div>
    <hr size="2" color="rgb(156,1,0)" width="95%" style="clear: both;">
</body>

</html>

I tried javascript also i saw that can be possible with php but I'm not sure.

  • 1
    You can use this anwser if u wanted to make in php https://stackoverflow.com/questions/871858/php-pass-variable-to-next-page. – David Bister Mar 01 '23 at 12:37
  • 1
    You can just enter your question in the search bar to find questions exactly like yours. Of course people must have solved this before you started web development so there is a lot if you just search your question instead of posting a duplicate. – Peter Krebs Mar 01 '23 at 12:47

2 Answers2

0

In the second page you have to use php to get the field values. The form is using method="post" , so you can get the field values from the $_POST array.

If you want the <button type="submit" ...> to submit the form, the button has to be within the <form>

A basic example to get the value of the fname text field:

<?php
$fname = (isset($_POST['fname'])) ? $_POST['fname'] : '';
if ($fname != '') {
  echo $fname
} else {
  // nothing in fname
}
?>
bron
  • 1,457
  • 1
  • 9
  • 18
0

I added the php but nothing is happening it is going to the next page but the page is blank

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="main.css">
</head>

<body>
    <div class="flex_container">
        <div class="product_list"><b>Product List</b></div>
        <a href="form.php"><button id="add_button">ADD</button></a>
        <button id="delete_button">MASS DELETE</button>
    </div>
    <hr size="2" color="rgb(156,1,0)" width="95%" style="clear: both;">
    <div>
    <?php
$sku = (isset($_POST['sku'])) ? $_POST['sku'] : '';
$name = (isset($_POST['name'])) ? $_POST['name'] : '';
$price = (isset($_POST['price'])) ? $_POST['price'] : '';
$size = (isset($_POST['size'])) ? $_POST['size'] : '';
$weight = (isset($_POST['weight'])) ? $_POST['weight'] : '';
$height = (isset($_POST['height'])) ? $_POST['height'] : '';
$width = (isset($_POST['width'])) ? $_POST['width'] : '';
$length = (isset($_POST['length'])) ? $_POST['length'] : '';
if ($sku != '') {
  echo $sku;
}
elseif ($name != '') {
    echo $name;
}
elseif ($price != '') {
    echo $price;
}
elseif ($size != '') {
    echo $size;
}
elseif ($weight != '') {
    echo $weight;
}
elseif ($height != '') {
    echo $height;
}
elseif ($width != '') {
    echo $width;
}
elseif ($length != ''){
    echo $length;
}
?>
    </div>
    <hr size="2" color="rgb(156,1,0)" width="95%" style="clear: both;">
</body>

</html>

Maybe because i have to create a database