1

I want to somehow check the name of HTML file which has a submit button which goes to 'updatecart.php', in this php file I wanted to do an IF statement such as:

updatecart.php - Pseudo code:

IF(calling HTML file == "book1.html"){

// INSERT BOOK1 DATA INTO DATABASE TABLE (SQL)

}

IF(calling HTML file == "book2.html"){

// INSERT BOOK2 DATA INTO DATABASE TABLE (SQL)

}

etc...

I basically have multiple HTML files which all have a form with action set to action = "updatecart.php" and I want to insert different data into the same database table depending on which page the form was submitted.

So I need to find the name of the HTML page from which the form was submitted.

ChrisF
  • 127,439
  • 29
  • 243
  • 315
Ryman Holmes
  • 716
  • 3
  • 20
  • 39

2 Answers2

4

You can use $_SERVER['HTTP_REFERER'], to get the requesting page, but this is not always completely dependable. A better way to go about this would be to put a hidden input unique to the particular page in that page's form and check that instead.

Explosion Pills
  • 176,581
  • 46
  • 285
  • 363
2

I think it's better to use an input field hidden with value that you want to check.

Eg:

book1.html

<form action="updatecart.php" method="post">
 <input type="hidden" name="filename" value="book1" />
</form>

book2.html

<form action="updatecart.php" method="post">
 <input type="hidden" name="filename" value="book2" />
</form>

And you can check it by,

<?php

$filename = $_POST['filename'];

if ($filename === 'book1') {
 // INSERT BOOK1 DATA INTO DATABASE TABLE (SQL)
} else if ($filename === 'book2') {
 // INSERT BOOK2 DATA INTO DATABASE TABLE (SQL)
}
?>
Nikhil Mohan
  • 881
  • 6
  • 13