1

I want to go to other php page on pressing html button of same site. I have tried it by using header function but it was not working.
Here is the simple html code:

<input type="button" name="Insert_Ad" value="Post and ad">

If user clicks this button it will take it to another php page For Example with URL adddress 'http://localhost/Product.php/Product.php'

Here is the code of that PHP page to which I want to go on pressing button

   <html>
    <head>
        <meta charset="UTF-8">
        <title>Insert form data</title>
    </head>
    <body>
    <form method="post" action ="Product.php" id="contact-form">

        <input type="text" name="product_category" placeholder="product_category"  required />



        <input type="text" name="product_name"  placeholder="product_name" required />

        <textarea id = "address" name="product_description" placeholder="product_description"  required /></textarea>


        <input type="text" name="product_image1"  placeholder="product_image1" required />


        <input type="text" name="product_image2"  placeholder="product_image2" required />

        <input type="text" name="product_image3"  placeholder="product_image3" required />

        <input type="text" name="product_image4"  placeholder="product_image4" required />

        <input type="text" name="product_image5"  placeholder="product_image5" required />


        <div class="btn-group" role="group">
            <input type="submit" class="btn btn-default" name="Add" value="Enter the box" style="margin-top: 15px; margin-right: 15px; border-radius: 4px;">

        </div>

    </form>

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

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

if(isset($_POST['Add']))
{
    $product_category = $_POST['product_category'];
    $product_name = ($_POST['product_name']);
    $product_description = ($_POST['product_description']);
    $product_image1 = ($_POST['product_image1']);
    $product_image2 = ($_POST['product_image2']);
    $product_image3 = ($_POST['product_image3']);
    $product_image4 = ($_POST['product_image4']);
    $product_image5 = ($_POST['product_image5']);


    $sql = "INSERT INTO zain.product (product_category,product_name,product_description,product_image1,product_image2,product_image3,product_image4,product_image5)
VALUES ('$product_category','$product_name','$product_description','$product_image1','$product_image2','$product_image3','$product_image4','$product_image5')";
mysqli_query($conn,$sql);
    if (mysqli_query($conn,$sql))
    {
        echo "New record created successfully";
    }
    else
    {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    $conn->close();
}
?>


    </body>
   </html>

Kindly help me how to do that

Zain Farooq
  • 2,956
  • 3
  • 20
  • 42

2 Answers2

1

This is the simplest way:
<input type="button" name="Insert_Ad" value="Post and ad" onclick="location.href='your_url_here'">

Or you can use a form to submit to the given url:
<form action="your_url_here"> <input type="submit" name="Insert_Ad" value="Post and ad"> </form>

trank
  • 952
  • 11
  • 8
1

I would recommend using an anchor tag. If you want to give it the appearance of a button, then use CSS to style it into a button. For instance, if you were using twitter bootstrap, this would involve giving it a class of btn

<a href="/Product.php" class='btn'>Post an ad</a>

If you insist on using a button, then you might want to wrap it around a HTML form, where the action attribute points to the page you would like to navigate to. The method has to be GET. If it is the only button in the form, then it will default to being the submit button even in the absence of a type="submit" attribute.

<form action="/Product.php" method="GET">
    <input type="button" name="Insert_Ad" value="Post and ad">
</form>