I'm validating my form with php and displaying error messsages but after every submit , the form is refreshed and the inputs are blank again . I was wondering if there was a way i could re-insert the user past data into the fields again .
I'm using POST method .
<form id="registration-form" action="" method="post">
<input type="text" name="username" id="username" placeholder="Username">
<button type="submit" name="_submit" id="submit" class="button-submit">Submit</button>
</form>
PHP function :
if( !isset( $_POST['_submit'] ) ){
return array( $array, $arrayexport );
}
$username = $_POST['username'];
// test function for username
if ( ($valid_username ) != 0 ) {
array_push($array, "Input Required"); }
else{
header( 'Location: /thankyou.php' );
}
YOu can do something as
<input type="text" name="username"
id="username" placeholder="Username"
value="<?php if(isset($_POST['username'])){ echo $_POST['username'];}else{ echo '';}?>">
made a cleaner version .
Thanks to Abhik Chakraborty
<input type="text" name="username" id="username" placeholder="Username" value="<?php user() ?>">
External php file :
function user(){
if(isset($_POST['username'])){ echo $_POST['username'];}else{ echo '';}}
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I have created a simple form validation, the PHP code when used inside the value attribute to auto type the form, the name and he comments display properly expect the email (input type="text").Have I written the code correctly or has my code has any code smells.
Edit: My question is not that, I don't receive a main it is that
I get a value of 1 or the value gets cleared of.
PHP code :-
<?php
if($_SERVER['REQUEST_METHOD']=='POST') {
$error="";
if(strlen ( $_POST['name'] ) < 5 ) {
$error = "Please type more than 4 characters<br/>";
}
if ( $_POST['email']="" || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) ){
$error.= "Please type a valid Email<br/>";
}
if(strlen($_POST['comment']) < 4){
$error.= "Please type more than 4 characters";
}
if( !empty($error) ){
$result = "<div class='alert alert-danger'>$error</div>";
}
else {
if( mail('jokersspirit#gmail.com','test message',"Name:".$_POST['name'].
"Email:".$_POST['email'].
"Comment:".$_POST['comment']) ){
$result = "<div class='alert alert-success'>Your form has been submitted</div>";
}
else{
$result = "<div class='alert alert-success'>Error occurred
while submitting your form please try again later</div>";
}
}
}
?>
HTML code : -
<form action="" method="post">
<label for="name">Your Name:</label>
<input type="text" class="form-control" name="name" id="name" placeholder="name" value="<?php echo isset($_POST['name'])?$_POST['name']:""; ?>">
<label for="email">Your Email:</label>
<input type="text" class="form-control" name="email" id="email" placeholder="email" value="<?php echo isset($_POST['email'])? $_POST['email']:""; ?>">
<label for="comment">Your Comments:</label>
<textarea name="comment" placeholder="comments" class="form-control" id="comment">
<?php echo isset($_POST['comment'])?$_POST['comment']:""; ?>
</textarea>
<input type="submit" class="btn btn-success btn-lg " value="Submit">
</form>
Your error is here
if ( $_POST['email']="" || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) ){
Replace it by :
if ( $_POST['email']=="" || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) ){
(= replaced by ==)
Without it, your $_POST['email'] is an empty string.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input onclick="this.value=''" type="text" name="first_name" value="First Name">
<input onclick="this.value=''" type="text" name="last_name" value="Last Name">
<input onclick="this.value=''" type="text" name="pwd" value="The Passcode">
<input name="submit" type="submit" value="Submit" class="submit_">
<textarea name="comments" id="comments" rows="4" cols="50"></textarea>
</form>
PHP :
if(isset($_POST['submit']))
{
$name = mysql_real_escape_string($_POST['first_name']);
$pwd = mysql_real_escape_string($_POST['pwd']);
$text = mysql_real_escape_string($_POST['comments']);
// print_r($_POST);
if(is_null($text)) {
debug_to_console('some fields empty');
echo "<p class=\"warning\">* Your Message Did Not Contain Any Characters.</p>";
}
else if($name === 'First Name'){
debug_to_console('name isnt set');
echo "<p class=\"warning\">* You Have Not Entered Your First Name Correctly.</p>";
}
else if($pwd !== 'XyZ'){
debug_to_console('password not set');
echo "<p class=\"warning\">* Passcodes Do Not Match.</p>";
} else {
Keeps returning 'password not set' even though the form input matches the variable. Used print_r and $pwd states XyZ.
tried removing onClick from the form input. I'm assuming this is a caps thing?
Please help, thank you.
Check $pwd with
var_dump($pwd);
I do believe $pwd is a string (because you use !== instead of !=) AND doesn't contain: XyZ
(if you didn't change the value in input field pwd, its value is still: The Passcode)
I am working on a simple login form for a mobile site that will save the form data in local storage but will also post data to a php file for url redirection with appended form data. I need help as the storage is working but the post for this is not.
Html
<form class="form-signin" action="siteurl/processing/login-app.php" method="post">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="input-block-level" placeholder="Eventname" id='eventname'>
<input type="text" class="input-block-level" placeholder="Username" id='username'>
<input type="password" class="input-block-level" placeholder="Password" id="password">
<label class="checkbox">
<input type="checkbox" value="remember-me" id="remember_me"> Remember me
</label>
<button class="btn btn-large btn-primary" type="submit">Sign in</button>
</form>
Now the script
<script>
$(function() {
if (localStorage.chkbx && localStorage.chkbx != '') {
$('#remember_me').attr('checked', 'checked');
$('#username').val(localStorage.usrname);
$('#pass').val(localStorage.pass);
} else {
$('#remember_me').removeAttr('checked');
$('#username').val('');
$('#pass').val('');
}
$('#remember_me').click(function() {
if ($('#remember_me').is(':checked')) {
// save username and password
localStorage.usrname = $('#username').val();
localStorage.pass = $('#pass').val();
localStorage.chkbx = $('#remember_me').val();
} else {
localStorage.usrname = '';
localStorage.pass = '';
localStorage.chkbx = '';
}
});
});
</script>
And the php
<?php
if (isset($_POST["eventname"]) && isset($_POST["username"]) && isset($_POST["password"])) {
$eventname = $_POST["eventname"];
$username = $_POST["username"];
$password = $_POST["password"];
$url = "siteurl/index.php/$eventname?user=$username&passw=$password";
header( "Location: $url" ) ;
} else {
echo "Username and Password not found";
}
?>
What am I missing?
Your POST variables work off the input names, not ids.
Add a name attribute to your inputs and access them.
This is a part of my registration form. I want to display back the input user inserted if they forgot to enter all the info needed. However, I get this on my textbox in register form where everyone including my user can see it.
Notice:Undefined variable: name in D:\XAMPP\htdocs\registration.php on line 113
I want it to echo back the input that user had inserted and display it again so that user does not have to enter the same input over again. Help ?
$myusername=($_POST['username']);
$name=($_POST['name']);
if(isset($_POST['username'])) {
echo $_POST['username'];
}
if(isset($_POST['name'])) {
echo $_POST['name'];
}
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
Assuming you send the user back to the page they were at previously if the form fails to validate, the POST array is emptied. POST will only carry the information to the page that the form is submitting to.
You can use sessions to save the data in an array, indexed by form field name. Then when the user is sent back to the form, if there are any entries in the array, you can iterate over them through to your form fields.
you can controll the variables with if clause; means you can write:
if ( isset($_POST['send']) && isset($myusername) ) {
echo $myusername;
}
else {
echo '<span style="color:red;">Please complete this field</span>';
}
and do the same in html value for textfiels...
$myusername = isset($_POST['username']) ? ($_POST['username']) : '';
$name = isset($_POST['name']) ? ($_POST['name']) : '';
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
Try this, this should remove your error?
if(isset($_POST['username'])) {
$myusername=($_POST['username']);
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
echo $_POST['username'];
}
if(isset($_POST['name'])) {
$name=($_POST['name']);
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
echo $_POST['name'];
}
Here we are checking for POST value first then we using it.
Write the isset function inside the value of each of your .
Exemple :
<input type="text" name="username" size="60" value="
<?php if( isset( $_POST["myusername"] ) )
echo $_POST["myusername"] ?> "/>
you can use this tutorial for validation of input fields in php
http://www.w3schools.com/php/php_form_validation.asp
or you can use this method
<?php
$myusername="";
$name="";
if(isset($_POST['submit'])){
$myusername = $_POST['username'];
$name = $_POST['name'];
}
?>
<form method="POST" action="">
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
<input type="submit" name="submit" size="60" value="submit"/>
</form>
I need your help.. I'm trying to make the textbox red whenever there's an error in a form...
This is what i've being able to do. But when i submit the fore, I get an Undefined Index error_css in the form
if (isset($_POST['submit'])) {
if (empty($_POST['username'])) {
$error_css='background-color:red';
}
Form
<label for="username">Username:</label>
<input id="username" type="text" value="<?php if(isset($_POST['username'])){ echo $_POST['username']; } ?>" name="username" title='Username' />
thanks for your time...
Try something like this:
<?php
$username = "";
$error_css = "";
if (isset($_POST['submit'])) {
if(isset($_POST['username']))
$username = $_POST['username'];
else
$error_css='background-color:red';
}
?>
<label for="username">Username:</label>
<input id="username" type="text" value="<?php echo $username; ?>" name="username" title='Username' style="<?php echo $error_css; ?>"/>
If you can, why not look into a solution like jQuery validation plugin? It's not a complete solution, as you still need some sort of server side validation, but it'll get you on your way.
you never set the style in the html tag and the first if{if{}} statements are redundant. It should be:
<label for="username">Username:</label>
<input type="text" <?php
if(isset($_POST['username'])) {
echo 'value="'.$_POST['username'].'"';
} else {
echo 'style="background-color:red"';
}
?> name="username" title='Username' >
Form
<label for="username">Username:</label>
<input id="username" type="text" value="<?php echo #$_POST['username']; ?>"
name="username" title='Username' style="<?php echo #$error_css; ?>"/>
Your variable $error_css is empty/does not exist.
I think the 2 if cases in the beginning of your script never reach the point where $error_css gets its content.