I have a password element which I use for signing in process. But, I get this weird username and password once the page gets opened !!
but the problem is when I don't user password element but only input I don't get this weird username and password, what should I do to make them blank?
Here's the code
echo"
<center><small>
<br />
<p> Welcome Guest you can sign in here:</p></br>
<form action = \"HomeWork.php\" method = \"post\">
User name*: <input type=\"text\" name=\"Username\" />
Password*: <input type=\"password\" name=\"Password\" />
or Sign up !
<br /> <br />
<input type=submit value=Submit align=\"middle\" />
</form>
</small></center>";
Can you help me??
This is your browser remembering the username/password you've entered in for your localhost site. This isn't to do with your html or php. To test, try it out in another browser that you have used to view the localhost site and see if it adds it to the form elements.
EDIT
Note, following kag's answer, you can add autocomplete=off to the form to prevent the browser from autocompleting.
https://developer.mozilla.org/en/how_to_turn_off_form_autocompletion
echo "
<center>
<small>
<br />
<p>Welcome Guest you can sign in here:</p></br>
<form action='HomeWork.php' method='post' autocomplete='off'>
Username*: <input type='text' name='Username' />
Password*: <input type='password' name='Password' />
<a href='Signup.php'>or Sign up !</a>
<br />
<input type='submit' value='Submit' align='middle' />
</form>
</small>
</center>
";
Also, consider not using tags like center and small; instead use css and classes/selectors.
This is your browser trying to help you by automatically filling the fields for you. If you want to stop it from doing so, you can add autocomplete="off" in both <input>.
Try adding the "value" attribute to the input tags and set it to empty:
<input type="text" name="Username" value="" />
If that doesn't do it, try using javascript:
<head>
<script type="text/javascript">
function clearMyFields() {
document.getElementById('input_u').value = "";
document.getElementById('input_p').value = "";
}
</script>
</head>
<body onload="clearMyFields();">
<input type="text" id="input_u" name="Username" value="" />
<input type="password" id="input_p" name="Password" value="" />
Try with:
<meta http-equiv="Pragma" content="no-cache">
<!-- Pragma content set to no-cache tells the browser not to cache the page
This may or may not work in IE -->
and
<meta http-equiv="expires" content="0">
<!-- Setting the page to expire at 0 means the page is immediately expired
Any vales less then one will set the page to expire some time in past and
not be cached. This may not work with Navigator -->
Related
I have a simple html form and want to set a cookie value to 1, for example, if the submit button was pressed. How can I do it?
The form looks like that:
<form action="auth.php">
<label for="fname">Enter your name: </label>
<input type="text" id="fname" placeholder="John" required />
<br />
<br />
<label for="fsurname">Enter your surname:</label>
<input type="text" id="fsurname" placeholder="Doe" required />
<br />
<br />
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email" placeholder="stupidlady123#yandex.ru" required>
<br />
<br />
<input type="submit" value="Register!" />
<!-- <?php
setcookie('Form', '1');
echo $_COOKIE['Form'];
?> -->
</form>
I've tried setting cookie in an action file (auth.php) and after submit button. But, as I understand, once data is sent to the server, I cant set the cookie any longer.
First of all, you need to put your setcookie before any html tag as you can see in php doc : https://www.php.net/manual/fr/function.setcookie.php ;
Secondly, your cookies can't be used because you're not setting him for every documents. You have to place '/' in your setcookie like :
<?php setcookie('form','1',time()+60*60*24*30,'/'); ?>
You can use instead $_SESSION which is more modulable.
I would to submit 2 buttons in same php page, every button show deferent message on label,
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<title>Test Submit 2 buttons</title>
<body>
<?php
$add_result = "";
if (isset($_POST['addnews']))
{
$add_result = "Hello, add new news";
}
if (isset($_POST['deletenews']))
{
$add_result = "Hello, delete all news";
}
?>
<br /> <br /> <br /> <br /> <br /> <br/>
<form accept-charset="utf-8" method="POST" dir="rtl" style="text-align:center" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>Enter the news before click on any button::</label>
<br /> <br />
<input type="text" name="news_text" size="100" id="partNumber" required/>
<br /> <br /> <br /> <br />
<input type="submit" name="addnews" id="round" value="Add" />
<br /> <br/> <br />
<input type="submit" name="deletenews" id="round" value="Remove" />
<label for="message">
<?php echo $add_result; ?>
</label>
</form>
<style type="text/css">
input#round{
....
}
</style>
I used PHP_SELF in the form, I want the result show directly after click on any button without change the page
Thanks
You can't do anything with PHP without reloading the page, because each time it needs to be interprrted by server.
In case of 2 submit buttons both of them just submit the form, irrelevant what is the button name, and on the server-side you can't know, what button was pushed.
Either do some tricky js stuff, like changing some hidden input value or form action etc.
Or just keep it simple and use radio boxes
I am trying to build a simple HTML form that is spread over two pages.
First page asks for user's first and last name. Second page asks for his country & whether he would like to receive additional info.
The first page has a single button named "Next" for going to the second page. The second page has two buttons named "Next" to go the next page (non-existent at present) and "Back" to go back to the first page.
On the first page when I click the "Next" button, nothing happens. How can I go to the next page on clicking it ?
Here is my code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
if(!isset($_POST['next1']))
Display1();
else if(isset($_POST['next1']))
Display2();
else if(isset($_POST['back1']))
Display1();
?>
<?php
function Display1()
{
?>
<form id="form1" action="Multi.php" method="post">
<label>Enter First Name: </label>
<input type="text" name="text1" />
<br />
<br />
<label>Enter Last Name: </label>
<input type="text" name="text2" />
<br />
<br />
<input type="button" name="next1" value="Next" />
<?php
}
function Display2()
{
?>
<form id="form2" action="Multi.php" method="post">
Select Country
<select name="country[]" multiple="multiple">
<option value="India">India</option>
<option value="USA">USA</option>
<option value="China">China</option>
</select>
<br />
<br />
Do you want to receive latest info ?
<input type="checkbox" name="checkbox1[]" />Yes
<br />
<br />
<input type="button" name="next2" value="Next" />
<input type="button" name="back1" value="Back" />
<?php
}
?>
</body>
</html>
The input type needs to be "submit"
<input type="submit" name="next1" value="Next" />
So for all the inputs that has "button" you need to change to "submit"
And as an addition to Abhik's answer.
<input type="button"> is only a clickable input. You (mostly) use it when you want to call a JS script. So if for some obscure reason you want to use this instead of the more simple solution provided by Abhik go ahead and add a onclick event handler.
<input type="button" onclick="document.getElementById("form1").submit();">
You could also use a <button> instead, but make sure you always specify the type. The advantage is that they offer a higher degree of customization. Basically you can put HTML inside a <button> tag.
So try also:
<button type="submit" value="Next">Next</button>
The other types of buttons are (source: w3schools.com):
button: The button is a clickable button
submit: The button is a submit button (submits form-data)
reset: The button is a reset button (resets the form-data to its initial values)
i am trying to make the login form in my website appears in the middle of the page with gray background, so i have done it like that:
<style type="text/css">
#cover5{position:absolute;top:0px;left:0px;overflow:hidden;display:none;width:100%;height:100%;background-color:gray;text-align:center}
#warning{top: 150px;margin:auto;position:relative;width:600px;height:fit-content;background-color:white;color:black;padding:10px; zIndex:20; border: 5px solid #003366;}
</style
<div id="cover5" onclick="javascript:cover5();">
<div id="warning" onclick="javascript:cover5();">
<?php include 'SignIn.php'; ?>
</div>
</div>
<script>
function cover5()
{
var cover=document.getElementById('cover5');
if(vis)
{
vis=0;
cover.style.display='block';
}
else
{
vis=1;
cover.style.display='none';
}
}
</script>
an this the code of the SignIn.php
<form id='login' action='SignIn.php' method='post' accept-charset='UTF-8' onsubmit="return valid()">
<fieldset >
<legend>SignIn</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='username' >Email:</label>
<input type='text' name='email' id='username' maxlength="50" /> <br />
<label for='password' >Password:</label>
<input type='password' name='password' id='password' maxlength="50" /> <br />
<input type='submit' name='Submit' value='Login' /> <br />
<a href='resetPassword1.php'> Forgot/Reset Password? Click Here</a>
</fieldset>
</form>
the problem is that when use clicks on Forgot/Reset Password? or enter wrong data everything opens in new window, i have used iframe instead of div however when the use signIn successfully the home page opens in the same iframe, so anyone know how to solve the problem?
sorry for the long question :)
You need to add this to your link:
target='_self'
so it would look like this:
<a href='resetPassword1.php' target='_self'> Forgot/Reset Password? Click Here</a>
there are others you can play with too, like to open in new window:
target='_blank'
hope this helps
inside of the link try setting target="_self" so
Forgot/Reset Password? Click Here
If that doesn't work try it in another browser. Sometimes browsers have default settings that could mess stuff up. Another possible problem may be Javascript somewhere in your site sending your links to new windows.
for example the following file is SamePage.php
<!DOCTYPE html>
<html lang="en">
<head><title>Personal Information</title></head>
<body>
<?php
if(!empty($_POST))
{
echo $_POST['var'].'<br />';
//do a bunch of stuff
...
echo "Inserted into database succesfully! You will now be redirected.<meta http-equiv=refresh content=4;url=database.php><br /></body></html>";//meta refresh gets rid of old POST data
exit;
}
?>
Add another entery:<br />
<form method="POST" action="SamePage.php"><--!It sends the information to itself-->
name: <input type="text" name="name" size="10" maxlength="25" /><br />
telephone: <input type="text" name="telephone" size="10" maxlength="25" /><br />
birthday: <input type="text" name="birthday" size="10" maxlength="25" /><br />
<input type="submit" value="add" />
<input type="reset" value="clear" />
</form>
</body>
</html>
It's not very nice to have the exit there and sometimes I don't want to refresh the page, but it has to be done encase the user presses the refresh on the browser and the same POST data gets sent again and things get messed up. Can anyone think of a better way?
Best is to send the form to another page and the reload your page... But if you don't want, you can:
Use AJAX
Put all your HTML code between { } symbols
Use templates (avoid mix PHP and HTML)