<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class="header">
<H2>Login</H2>
</div>
<form method="post" action="https://..." enctype="multipart/form-data">
<?php include 'errors.php'; ?>
<div class="input-group">
<input type="text" name="username" placeholder="Username..." value="<?php echo $username; ?>">
</div>
<div class="input-group">
<input type="password" name="password_1" placeholder="Password...">
</div>
</div>
<div class="input-group">
<input type="file" name="image" placeholder="Image..." value="<?php echo $image; ?>"> // The File doesn't get echo in after submit and an error
</div>
<div class="input-group">
<button type="submit" name="login" class="btn">Login</button>
</div>
</form>
</body>
</html>
This Form is to register Users with an Image. After submit I check if there Name is all ready taken and look for other errors if there is an error I want that everything is still in the Form except the password. It is working with the Username but not with a file (Image). So how can I do that?
It is by default impossible to set a default value for a file picker due to security reasons
Learn more about it here
Others have explained why you can't do it. But if it is very important for you to achieve this behaviour (retaining all elements if there is an error in submission details), you can do this by setting up Ajax calls.
Since you would also add preventDefault() to the submit button event handler, the page won't be refreshed and the user won't lose entered data.
Your first ajax call will verify if entered data is valid. And then, if the response is positive, you send another ajax request to process the form submission.
Related
<form method="POST">
<div class="form-outline form-white mb-4">
<input type="password" name="password" class="form-control" required/>
<label class="form-label" for="form">Pass</label>
</div>
<button type="Submit" value="send" class="btn transparent btn-block" style="color: white;">Submit</button>
</form>
if (isset($_POST['submit'])) {
$password=$_POST['password'];
if ($password=='password') {
header("location:ari.html");
exit();
}
else
echo "Incorrect.";
}
I'm beginning php currently and I'm trying to use a login method, but whenever I press my submit button my code refreshes and doesn't move onto the next page I indicated. (location:ari.html)
If I could get help on what I did wrong that would be great thanks.
you should consider using action attribute in form tag and point into some POST url that retrieve your data
use action line this
<form method="POST" action="/ari.php">
submit button refresh the page without action attribute it just point to current URL if you dont fill it
<form method="POST" >
<div class="form-outline form-white mb-4">
<input type="password" name="password" class="form-control" required/>
<label class="form-label" for="form">Pass</label>
</div>
<button type="Submit" name="submit" value="send" class="btn transparent btn-block" style="color: white;">Submit</button>
</form>
<?php
if (isset($_POST['submit'])) {
echo $password=$_POST['password']=='password'?header("location:ari.html"):"Incorrect.";
}
?>
have updated this code and working fine for me...hope u got the answer
Take a look at this line:
<form method="POST">
What happening is that when you are submitting the form it is sending data to the same page.
In this line:
if (isset($_POST['submit']))
I found no tag with name='submit' attribute. You should put this in your submit button.
If you want to send it to another page then use the action attribute in form tag and point your data to that page like action=yourpage.php
I have the following code for a simple registration page, called 'register.php':
<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>Registration system PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Register</h2>
</div>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo $username; ?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo $email; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm password</label>
<input type="password" name="password_2">
</div>
<div class="input-group">
<button type="submit" class="btn" name="reg_user">Register</button>
</div>
<p>
Already a member? Sign in
</p>
</form>
</body>
</html>
Which is from a tutorial from this website: http://codewithawa.com/posts/complete-user-registration-system-using-php-and-mysql-database.
The problem I have is that whenever I click the "Register" button, I get a 404 Not found response page:
The registration form
The 404 Error Page
Yet the path to the resource in the URL is the same, and I can't seem to find anyone else with this issue elsewhere online. Is it an issue with PHPStorm?
The "register" button is a submit button. This means that when you click on the button the form will be sent (by POST method) to the page specified in the ACTION attribute of your form, in this case: register.php
If you get a 404 error page, this should mean that the page register.php does not exist.
Am I wrong?
It is simply telling you that you have either not loaded the "register.php" file, didn't load it into the proper directory or it is mis-spelled.
Create a php file using this, upload it to the same directory as your registration file, run it and post the results ::
<?php
$files = scandir(".", 1);
echo "<pre>";
print_r($files);
echo "</pre>";
?>
You should see something like this ::
Array
(
[0] => scandir.php
[1] => index.php
[2] => register.php
[3] => Core
[4] => ..
[5] => .
)
after many days of trying to get a previously working php form converted to submitting the variables inside a new div I realized that I'm missing something. Other posts show javascript, but Iv'e never used that before and don't understand the need. The new page draws correctly, but the php variables are not being received on the destination page.
HTML for the submit,
<form action="entrance2.php">
<div class="medium-12 columns m-b20"><h4 class="heading">Existing users log-in here :-</h4></div>
</div>
<div class="row">
<div class="user medium-12 columns text-center m-b15"><img src="images/user-img.png" alt=""/></div>
</div>
<div class="row">
<div class="medium-10 columns medium-offset-1"><label for="User Name"></label>
<input id="OwnerEmaili" type="text" placeholder="User Name" name="UserName"></div>
</div>
<div class="row">
<div class="medium-10 columns medium-offset-1"><label for="Password"></label>
<input id="OwnerPasswordi" type="password" placeholder="Password" name="Password"></div>
</div>
<div class="row">
<div class="medium-12 columns text-center"><button class="grd-button">Log In</button></div>
<input type="submit" id="save" name="save" value = "Submit"/>//simple submit for testing
<div class="grd-button1" onClick="document.forms['submit-form'].submit();"></div>
</form></div>
</div>
</div>
Receiving page,
<?php
$p_OwnerEmaili=$_POST["OwnerEmaili"];
$p_OwnerPasswordi=$_POST["OwnerPasswordi"];
echo "$p_OwnerEmaili;$p_OwnerPasswordi";
Only shows the ;.
Is javascript required to submit from inside a div?
You're accessing the wrong items.
You'll need to set your forms input name attributes to this if you want to access them the way you currently have in your php script:
<input id="OwnerEmaili" type="text" placeholder="User Name" name="OwnerEmaili">
And
<input id="OwnerPasswordi" type="password" placeholder="Password" name="OwnerPasswordi">
That will allow you to access them as you do in your PHP script.
You can always check what values have been sent to your php script by using var_dump() or print_r().
<?php print_r($_POST); ?>
Would've shown you that you had UserName & Password set instead of what you wanted.
As Ghost pointed out in the comments, your form will always send user input via GET if you dont specify a method in it. So set this in your form tag:
<form action="entrance2.php" method="post">
I have been having problems for the past two days trying to submit a form using jquery ajax, php to mysql database. I reused a code for an animation form which I found online. The html code as follows (file name: "Slider.html"):
<html>
<body>
<div id="form_wrapper" class="form_wrapper">
<form class="register" style="right:-500px;backgroundcolor:grey;">
<h3>Register</h3>
<div class="column">
<div>
<label>First Name:</label>
<input type="text" />
<span class="error">This is an error</span>
</div>
<div>
<label>Last Name:</label>
<input type="text" />
<span class="error">This is an error</span>
</div>
</div>
<div class="column">
<div>
<label>Username:</label>
<input type="text"/>
<span class="error">This is an error</span>
</div>
<div>
<label>Email:</label>
<input type="text" />
<span class="error">This is an error</span>
</div>
<div>
<label>Password:</label>
<input type="password" id="r_passwordField"/>
<span class="error">This is an error</span>
</div>
</div>
<div class="bottom">
<div class="remember">
<input type="checkbox" />
<span>Send me updates</span>
</div>
<input type="submit" id="registrationform" value="register"/>
<a href="index.html" rel="login" class="linkform">You have an account already? Log in
here</a>
<div class="clear"></div>
</div>
</form>
<form class="login active">
<h3>Login</h3>
<div>
<label>Username:</label>
<input type="text" />
<span class="error">This is an error</span>
</div>
<div>
<label>Password: <a href="forgot_password.html" rel="forgot_password" class="forgot
linkform">Forgot your password?</a></label>
<input type="password" id="l_passwordField" size="10"/>
<span class="error">This is an error </span>
</div>
<div class="bottom">
<div class="remember"><input type="checkbox" /><span>Keep me logged in</span></div>
<input type="submit" id="loginform" value="Login"></input>
<a href="register.html" rel="register" class="linkform">You don't have an account yet?
Register here</a>
<div class="clear"></div>
</div>
</form> <form class="forgot_password">
<h3>Forgot Password</h3>
<div>
<label>Username or Email:</label>
<input type="text" />
<span class="error">This is an error</span>
</div>
<div class="bottom">
<input type="submit" id="forgortform" value="Send reminder"></input>
Suddenly remebered? Log in here
<a href="register.html" rel="register" class="linkform">You don't have an account?
Register here</a>
<div class="clear"></div>
</div>
</form>
</div>
<div id="graph-wrapper" class="graph-container" style="display:none">
<h3>Standard Diviation Bell Curve<h3>
<div class="graph-info">
Visitor
<span></span>
</div>
<div class="graph-container">
<div id="graph-lines"></div>
<div id="graph-bars"></div>
</div>
</div>
</div>
</body>
</html>
jquery ajax code as follows (jquery script on the same file as the html code):
<script>
$(document).ready(function(){
//animated code and other codes omitted
("#registrationform").click(function(){
$.post("postdata.php",
{fname:"Ibrahim",lname:"Ahmadu",email:"ibrostay#yahoo.com",uid:"ibro2stay",pwd:"ibro2stay",mean:"0.1",varience:"0.1",sdev:"0.
1",duration:"0.1"},function(responce){
if(responce==0){
alert("There was an error updating the record");
}else{
alert("update successful");
}
});
});
});
</script>
Below is the php code (php code file name: "postdata.php"):
<?php
$fname=$_REQUEST["fname"];
$lname=$_REQUEST["lname"];
$email=$_REQUEST["email"];
$uid=$_REQUEST["uid"];
$pwd=$_REQUEST["pwd"];
$mean=$_REQUEST["mean"];
$varience=$_REQUEST["varience"];
$sdev=$_REQUEST["sdev"];
$duration=$_REQUEST["duration"];
$con=mysql_connect('localhost','root','');
if(!$con)
{
die("Error Connecting to database;"+mysql_error());
}
$database=mysql_select_db('mydb');
if(!$database)
{
die("Error Connecting to database;"+mysql_error());
}
$update = mysql_query("insert into users values('$fname','$lname','$email','$uid','$pwd','$mean','$varience','$sdev','$duration')");
if(!$update)
{
die("Update wasn't Success full"+mysql_error());
}
echo "update successfull";
mysql_close($con);
?>
Whenever I click the register button nothing happens. The page only refreshes back to the login form since it has class "active" as the default form, and the browser address bar changes from this url: "localhost/slider.html" to this url: "localhost/slider.html?".
I hope my question was explicit enough, because I need an urgent answer, as this is my thesis project and I am running out of options.
and the browser address bar changes from this url: "localhost/slider.html" to this url: >"localhost/slider.html?".
use e.preventDefault();
$("#registrationform").click(function(e){
^
|
|_______________ you forgot the $
then add,
`e.preventDefault();
check javascript console and you will see all the errors. you have to eliminate one by one. For the start, check above.
advice: when you copy/paste codes, atleast try to know the structures. sometimes it wil conflict with your code and may stop running your code.
Looks like your Javascript has some errors and not executing hence. So, the form submit resorts to its default behavior - that is, submit to the tag's action attribute. Since you have not specified the action attribute in the tag, it will post the parameters to the current page.
You are getting the url localhost/slider.html? because of two reasons:
You have not specified method="post" in your . So, the form is trying to submit as GET. That is why the appended "?"
You do not have names for the inputs. So, the query string is empty. You need to specify name attribute for every input.
What you have to do first is to debug your Javascript. Use Firebug in Firefox or the console in Chrome or any similar tools to capture your JS errors and also examine your Ajax requests and responses.
Also, make sure that the last statement in the "click" function is a :
return false;
to force the form to stay on the page.
Another important thing is, as #steve pointed out, add method and action attributes to your tag. This will make sure that your script will not fail entirely on a JS disabled browser or on any such conditions.
I'm currently creating a simple jQuery Mobile sign in page. Here's my code:
<div data-role="page">
<div data-role="header"><h1>Header</h1></div>
<?php
if(isset($_POST['username']) && isset($_POST['password'])){
die("hello");
}
?>
<div data-role="content">
<form method="post">
<div data-role="fieldcontain">
<label for="username">Username:</label>
<input id="username" type="text" name="username" />
</div>
<div data-role="fieldcontain">
<label for="password">Password:</label>
<input id="password" type="password" name="password" />
</div>
<input type="submit" value="Sign In" data-inline="true" data-theme="b" />
</form>
</div>
<div data-role="footer"><h4>Footer</h4></div>
</div>
Whenever I type in a username and password, using firebug, I can tell that the correct post was sent, however, the page that is returned through ajax to load into the page does not have the die("hello") in it. It is just the same page that I entered the login information on.
Am I using jQuery Mobile's form system wrong?
You may want to consider turning off Ajax, while you are troubleshooting your form submission. The Ajax handling of form submissions inside JQuery Mobile is implemented automatically to create a smooth transition between the form and the result. Details, here. To submit a form without Ajax, you can either disable Ajax form handling globally, or per form via the
data-ajax="false"
attribute. Or, globally,
$(document).bind("mobileinit", function () {
$.mobile.ajaxEnabled = false;
});