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.

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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)
}
?>

4 Comments

Thanks, Is there a reason why you left out method = 'post' in book2.html?
Sry! I forgot to put method = 'post' in book2.html. See the above updated one.
No problem, also the 3 equals === is new to me, I use ==
You can find the difference b/w === and == from here, stackoverflow.com/questions/80646/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.