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.

4

2 回答 2

4

您可以使用$_SERVER['HTTP_REFERER'], 来获取请求页面,但这并不总是完全可靠的。解决此问题的更好方法是将特定页面独有的隐藏输入放入该页面的表单中,然后进行检查。

于 2013-05-28T15:16:08.590 回答
2

我认为最好使用具有hidden要检查的值的输入字段。

例如:

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>

你可以通过以下方式检查它,

<?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)
}
?>
于 2013-05-28T15:17:33.147 回答