How to remove cache data on page reload? - php

I have a registration form. If a user fill out all the required fields.
And when he reloads the page with out submitting form, the fields contain the values entered by the user. I want them to be empty as they were on first page load.
I have given value="" to the fields but they still contain the previously entered values.
<form id="registration_form" name="registration_form" method="post">
<input name="first_name" id="first_name" value="">
<input name="last_name" id="last_name" value="">
<input name="user_email" id="user_email" value="">
<input name="status" id="status" type="hidden" value="active">
</form>

This is form auto-fill, not page caching. You can disable autocomplete with:
<form id="registration_form" name="registration_form" method="post" autocomplete="off">

Remember to add autocomplete="off" to your form. This is auto-complete issue with some browsers, then you need to use id trick, every time you're loading the registration page, let say:
http://localhost/register.php
you can try it manually to see if it's works for browsers you've been testing, for example load this address on your browser and see if it still auto complete the form:
http://localhost/register.php?id=23
you can add id=anynumber, this happens to work perfectly, I do this with my CSS imports. To make it more PHP you can write some code like this:
$number = rand(1,100);
header("Location: register.php?id=$number");
Hope it helps

If you want to remove cache as you asked first (before edit), There are many ways,
Adding meta tag on page
<meta http-equiv="Cache-control" content="no-cache">
By redirecting using javascript,
window.location.href+'?eraseCache=true';
These are discussed in an other topic here.
If you just want to delete the values on the fields,
use
document.getElementById('text_box_id').value="";
on the top inside the head tag.

Related

How to use action attribute in HTML properly?

I am trying to create a form in HTMl. However, I don't know how to make the action attribute work. Below I have attached my code. The form appears, but when I enter the information, it isn't added to the PHP file.
<form action="info.php">
<p>Username:
<input type="text" name="username" size="15" maxlegnth="30" />
</p>
</form>
Another question. When I'm linking things (images, videos, etc.) to my website, do I need to attach the whole link (file:///C:/Users/Name/Desktop/Web%20Developement%20Code/notes.php) or is notes.php enough?
Thank you!
You need to set method="post" to form, and add submit button, then you will be able to get your information.
<form method="post" action="info.php">
<p>Username:
<input type="text" name="username" size="15" maxlegnth="30" />
</p>
<p><input type="submit"></p>
</form>
Then in info.php:
<?php
$username = $_POST['username'];
echo($username) //or any other actions with this variable
In response to your second question, you don't need to provide the full path. It is enough to specify the path from the root folder of your project.
Another question. When I'm linking things (images, videos, etc.) to my website, do I need to attach the whole link (file:///C:/Users/Name/Desktop/Web%20Developement%20Code/notes.php) or is notes.php enough?

How do I get real text (not a gray hint or visible-yet-blank suggestion) to be in an HTML text field of a web page by default?

I am using the "placeholder" attribute in HTML5. I use it in a PHP web page with an HTML section. It works as expected.
Here is my code (it was modified slightly from the w3schools website):
<!DOCTYPE html>
<html>
<body>
<form action="/nextpage.php">
<input type="text" name="firstname" placeholder="John"><br>
<input type="text" name="lastname" placeholder="Doe"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
As a user with the web page I tried using a form with the placeholder element and I clicked submit, but it acts like the field is empty if I do not change the default text that appears in light gray.
I want the text to be real -- and not act as though the field is empty. If the user changes the placeholder text in the fields, then that is fine. But if the user does not touch the placeholder text, I want the "Submit" click to ingest the placeholder text as if the user typed it in manually. How do I do this? I tried a variety of different things, but I cannot get it to work. The text could appear black -- but this does not matter. I want the text to be in the field if the user does not delete it or change it.
Also set the "value" param
<input type="text" name="firstname" placeholder="John" value="John">
It will have the user delete the placeholder but if nothing is changed then it is sent
Change placeholder to value
<!DOCTYPE html>
<html>
<body>
<form action="/nextpage.php">
<input type="text" name="firstname" value="John"><br>
<input type="text" name="lastname" value="Doe"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<script>
$("#firstname").on("click", function() {
$(this).val("")
});
$("#lastname").on("click", function() {
$(this).val("")
});
</script>
working jsfiddle : http://jsfiddle.net/Yu97Z/291/
A good approach to handling this type of thing is to provide the field with a default value when the page is loaded. For example:
<input type="text" name="firstname" value="John" placeholder="John">
That will pre-populate "John" in the input field making it so the user would not have to modify it if they chose not too. Perhaps then it would also make sense for placeholder to read something like "First Name", should someone remove the text altogether.

How to combine get method parameter and div id name without javascript?

This is simplified code of index.php :
<form action="index.php" method="get">
<input type="text" name="course">
<button>Find</button>
</form>
Suppose, in the text field "ADAM" is put and after pressing Find button url becomes
myweburl/index.php?course=ADAM
But I want to make it
myweburl/index.php?course=ADAM#courseid
NB: Here courseid is a div id name inside index.php. By this way, I will be able to scroll down the result area.
Do you know how to do this?
Why not do it in a more legitimate way
<form action="index.php" method="get">
<input type="text" name="course">
<input type="hidden" name="courseid" value="<?php echo $courseId;?>">
<button>Find</button>
</form>
Then you get a url like this
myweburl/index.php?course=ADAM&courseid=1234
Now you dont have to do any text manipulation in the script that processes the data you just use
$_GET['course']
$_GET['courseid']
Simply add the hashtag to the action attribute.
The browser would know to move it to the end of the url string (after the GET parameters).
<form action="index.php#courseID" method="get">
<input type="text" name="course">
<button>Find</button>
</form>
Would result:
index.php?course=xxxx#bla
Tested on Chrome, if someone find other results please update.

External form action. Post Validate in PHP. Without Javascript

I have this form:
<form name="form2" method="post" action="http://1.1.101.1/reg.php">
<input id="field12" type="text" value="{$username}" name="username" maxlength="32" placeholder="Username" required="required" />
<input id="field22" type="text" value="{$password}" name="password" maxlength="32" placeholder="Password" required="required" />
<input name="checkbox" type="hidden" id="checkbox" value="checkbox" />
<input type="hidden" name="url" value=""/><br/>
<input type="submit" value="Connect to WiFi" name="button1" /><br/>
</form>
the action is a external url.
How can i check in my php when the button submit is posted (name = button1) before it goes to that url.
Right now i have this but its not working becasuse it goes directly to the action url from the form.
if ($_SERVER['REQUEST_METHOD'] == "post") {
var_dump($_POST);
exit;
}
You can't.
The only way to validate it without using client side code is to submit the form to your own server side code.
You then won't be able to reliably redirect the request while maintaining POST.
You have basically two options the way I see it.
If it's not necessary for the user to see the output of the external script, you could do the posting yourself from your backend. I.e. change the action of your form to your own script and do something like the following:
Validation the fields
If validation OK, POST the data to the external URL via CURL (or similar)
If POST to external URL went OK, redirect to wherever the user should end up in the end
If the user must end up at this external URL, you could do it in two steps. First have your form action set to your own server side validation. If it passes, give the user a confirmation page with a form containing the same data which would then post it to the external URL. The fields should probably be hidden/read-only on this page to prevent them from being changed before the final submit.
This last method is definitely possible to mess with since it's easy to first use valid values, and then change the data in the HTML before doing the final submit. So if security is important here, you're stuck with the first option.
Try this
<?php
if(isset($_POST['button1'])){
//action
header('Refresh:3; url=http://1.1.101.1/reg.php');
}
?>

Retrieve Values with Post method from Separate Form Fields in PHP

This might be a basic question of HTML but I'd like to know if it is possible in PHP. Usually when building a form submission page, the form tag is set per one form group. So the submit button is assigned per one form tag. The elements of form fields have to be close as enclosed in the form tag. If I want more complicated layouts, then is it possible to separate form tags to divide form fields?
For example,
<?php
print_r($_POST);
?>
<form name="test" action="" method="post">
First name: <input type="text" name="firstname" value="" /><br />
<input type="submit" value="submit" />
</form>
<p>some other contents</p>
<form name="test" action="" method="post">
Last name: <input type="text" name="lastname" value="" />
</form>
In this case, even if the second field is filled, the value won't be sent. If possible, I'd like to have just one submit button and control multiple forms.
Wrap your entire page into a single form and use some kind of deliminator for your elements (for instance "form2_field1"). I have never found having multiple forms useful (even if there are hide/unhide values) and my guess is that the submit button will probably only submit the form it is wrapped in. As one of the comments has mentioned, use JQuery if you want more complex forms. However, my recommendation is just to send over the entire form, whether it is complex or not and process it according to what you want.

Categories