I have a form in a php page with a lot of text inputs in it, with a presetted value each. When someone submit the page by POST, I would like to know which of them have been changed, so I thought I could set a property of the text (like background colour) when someone type in the text input.
After the submit, how can I get the properties of inputs to see if they have been changed? $_POST['name_of_text_input'] return only the value of the input?
You can't see if they have been changed as simply as that.
PHP runs on the server, and once the data has been sent to the user, as far as PHP is concerned, that's the end of it. It doesn't remember anything else unless you specifically tell it to remember (via a session
or some data in the database).
You will have to do one of the following:
You can add hidden fields for each text input and set a value in those hidden fields with javascript (change event of the input).
If you use html5 you might be able to use the placeholder
<input type="text" placeholder="default value">
In PHP you should check if no value entered, then the default value should be taken.
There is no way to get properties of input using $_POST. But you can use javascript code to remember initial input's state and send only changed fields.
A simple solution would be creating hidden fields for your entries and comparing them afterwards:
<form>
<!-- old values -->
<input type="hidden" name="old_name" value="<?=$name;?>" />
<input type="hidden" name="old_surname" value="<?=$surname;?>" />
<!-- current values -->
<input type="text" name="name" value="<?=$name;?>" />
<input type="text" name="surname" value="<?=$surname;?>" />
</form>
Now, after submit, you can compare old_name
with name
and see if they are different.
Any time you have a FORM
in HTML, what a POST (HTML)
does is send it to your .php
server side POST
array all the variables with name
. So, you can access them as $_POST['name_of_the_input']
For example:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
Can be access in PHP
as :
<?php
echo $_POST["fname"];
echo $_POST["age"];
?>