on my login form, i am putting server side validations and if error occurs i want to display those error just below the validated control. Now for this, i am trying to call javascript function to show validation message in php code but not able to call.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['txtUsername']=='')
{
//here i want to call javascript function to display message
}
}
?>
<form action="login.php" method="POST">
Username <input type="text" size="30" name="txtUsername" id="user" /><br />
Password <input type="password" size="30" name="txtPassword" id="pass" /><br />
<input type="submit" value="Login" name="loginSubmit"/>
</form>
<script type="text/javascript">
function showMessage(value)
{
document.getElementById(value).innerHTML= value+"can not be empty.";
}
</script>
Please tell me how to display server side validation just below the validated control in form.
use this
if($_POST['txtUsername']=='')
{
echo '<script> showMessage("txtUsername"); </script>';
}
Something like this
<html>
<head>
<script type="text/javascript">
function showMessage(value)
{
document.getElementById(value).innerHTML= value+"can not be empty.";
}
</script>
</head>
<body>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['txtUsername']=='')
{
echo '<script> showMessage("txtUsername"); </script>';
}
}
?>
<form action="login.php" method="POST">
Username <input type="text" size="30" name="txtUsername" id="txtUsername" /><br />
Password <input type="password" size="30" name="txtPassword" id="txtPassword" /><br />
<input type="submit" value="Login" name="loginSubmit"/>
</form>
</body>
</html>
You can put your php code anywhere you like let's say in the body like attribute. You can try the following code:
<body <?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['txtUsername']=='')
{
echo "onload = 'showMessage("VALUE")'";
}
}
?> > // end of body start tag
<form action="login.php" method="POST">
Username <input type="text" size="30" name="txtUsername" id="user" /><br />
Password <input type="password" size="30" name="txtPassword" id="pass" /><br />
<input type="submit" value="Login" name="loginSubmit"/>
</form>
</body>
<script type="text/javascript">
function showMessage(value)
{
document.getElementById(value).innerHTML= value+"can not be empty.";
}
</script>
If the validation is successful the php code wont echo anything and the javascript function will not be called. Works for me :). Tell me if this helps.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['txtUsername']=='')
{
?>
<script>
//Define the function somewhere in the top or in external js and include it.
callyourfunction();
</script>
<?php
}
}
?> //Its not working
Related
I'm pretty new to php and have been trying to create a site using it. I have created a login page and am trying to provide the option to create a new user by inserting a username & pass into the DB. I feel like this is something simple that I'm missing but i'm not sure so hopefully someone can help :)
php ~
<?php
session_start();
unset($_SESSION["currentUser"]);
unset($_SESSION["currentUserID"]);
if (isset($_POST["action"]) && $_POST["action"]=="create") {
$formUserC=$_POST["name"];
$formPassC=$_POST["pword"];
include("dbConnect.php");
$dbQuery=$db->prepare("select * from users");
$newAcc = "INSERT INTO users (username, password) VALUES ('$formUserC', '$formPassC')";
if($newAcc){
echo "<p>Your account was successfully created.";
}
else{
echo "<p>Error</p>";
}
}
if (isset($_POST["action"]) && $_POST["action"]=="login") {
$formUser=$_POST["username"];
$formPass=$_POST["password"];
include("dbConnect.php");
$dbQuery=$db->prepare("select * from users where username=:formUser");
$dbParams = array('formUser'=>$formUser);
$dbQuery->execute($dbParams);
$dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC);
if ($dbRow["username"]==$formUser) {
if ($dbRow["password"]==$formPass) {
$_SESSION["currentUser"]=$formUser;
$_SESSION["currentUserID"]=$dbRow["id"];
if (isset($_SESSION["homePage"]))
header("Location: addToBasket.php");
else header("Location: homePage.html");
}
else {
header("Location: login.php?failCode=2");
}
} else {
header("Location: login.php?failCode=1");
}
} else {
?>
html (mostly) ~
<html >
<head>
<meta charset="UTF-8">
<title>Flat HTML5/CSS3 Login Form</title>
<link rel="stylesheet" href="css/loginstyle.css">
</head>
<body>
<div class="login-page">
<div class="form">
<form class="register-form" name="create" method="post" action="login.php">
<input type="text" name="name" placeholder="name"/>
<input type="password" name="pword" placeholder="pword"/>
<input type="hidden" name="action" value="create">
<input type="submit" value="create"/>
<p class="message">Already registered? Sign In</p>
</form>
<form name="login" method="post" action="login.php">
<input type="text" name="username" placeholder="username"/>
<input type="password" name="password" placeholder="password"/>
<input type="hidden" name="action" value="login">
<input type="submit" value="login"/>
<p class="message">Not registered? Create an account</p>
</form>
<?php
if (isset($_GET["failCode"])) {
if ($_GET["failCode"]==1)
echo "<h3 style='color:red;'>Invalid username entered</h3>";
if ($_GET["failCode"]==2)
echo "<h3 style='color:red;'>Invalid password entered</h3>";
}
?>
</div>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/loginindex.js"></script>
</body>
</html>
<?php
}
?>
P.S. some of it's code I nabbed from others. Thanks in advance.
I coded the log-in page as follows
<!DOCTYPE html5>
<html>
<head>
<? if (isset($_REQUEST['page1_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password on the first try!</h2></center>
<? } else { ?>
<center><h2>Wrong Password! Try again.
<form method="POST" action="password.php">
Password: <input type="password" NAME="pword" /><br /><br />
<input type="submit" name="page2_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<? } ?>
<? } elseif (isset($_REQUEST['page2_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password! But it took you two tries.</h2></center>
<? } else { ?>
<center><h2>Wrong Password Again! This was your second try.
<form method="POST" action="password.php">
Password: <input type = "password" name="pword" /><br /><br />
<input type="submit" name="page3_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<? } ?>
<? } elseif (isset($_REQUEST['page3_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password! But it took you three tries.</h2></center>
<? } else { ?>
<center><h2>Wrong Password Again! You're out of luck.</h2></center>
<? } ?>
<? } else { ?>
<!-- This is the first pass for the user – page 1 -->
<center><h2>Please enter your password to access this site!<br />
<form method="POST" action="password.php">
Password: <input type="password" name="pword"><br /><br />
<input type="submit" name="page1_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<? } ?>
</body>
</html>
When I run this php, everything is appearing on the same page while password function is not properly working.
I even followed the guideline that our professor provided but can't find any clear solution to this..
In PHP some times php.ini is set to not allow short code.
<? is short for <?PHP if your server is not set to read <? then it will look the way it does. Try Adding <?PHP to all <? short codes.
If the server cant read the php starts and ends then it will just run them all together.
EX:
<?PHP
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?PHP if (isset($_REQUEST['page1_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password on the first try!</h2></center>
<?PHP } else { ?>
<center><h2>Wrong Password! Try again.
<form method="POST" action="password.php">
Password: <input type="password" NAME="pword" /><br /><br />
<input type="submit" name="page2_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<?PHP } ?>
Notice all the <?PHP code starts
PLEASE DO NOT CODE LIKE THIS THIS IS TERRIBLE AND YOUR INSTRUCTOR NEEDS A LESSON ABOUT CLEAN CODE
http://php.net/manual/en/language.basic-syntax.phptags.php
I have these two forms that submits to different blocks of the same script. i am unable to access variable of one block in other, though both variables are in same script.
html form :(1.php)
<html>
<form method="POST" action="2.php" enctype="multipart/form-data">
</br>
Choose a user name:</font>
<input type="text" name="username">
<input type="submit" name="submit" value="Save and Proceed">
</form>
</html>
2.php:
<?php
$name=$_POST['username'];
if ((isset($_POST['username'])) && ($_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font>
<input type="text" name="age">
<input type="submit" name="submit" value="done">
</form>
</html>
<?php
}
}
if((isset($_POST['age'])) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "hi" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
?>
How do I access $_POST['username'] in the code following the html form in the same script? Thank you in advance.
On the second form you can use a hidden input, i.e.:
<input type="hidden" name="username" value="$name">
Example:
<?php
$name=$_POST['username'];
if (!empty($_POST['username']) && $_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
echo <<< LOL
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="hidden" name="username" value="$name">
<input type="submit" name="submit" value="done">
</form>
</html>
LOL;
}
}
if((isset($_POST['age'])) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "hi" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
?>
If I understand well you want to pass username twice.
Than you can use hidden input, which is not visible (only transports data):
<input type="hidden" name="username" value="<?php echo $_POST['username']; ?>" />
I'm using jquery for registration form. But after email and password are entered and submit button is pressed control doesn't get transferred to php as I expected. I m not fully aware with jquery, just want to add this functionality on registration page.
How can this be done.
<?php
include "config.php";
if( isset($_POST['submit']) ){
$email = $_POST['email'];
$pass = $_POST['password'];
if( $email=="" || $pass==''){
echo "Fields Left Blank","Some Fields were left blank. Please fill up all fields.";
exit;
}
if( !$LS->validEmail($email) ){
echo "E-Mail Is Not Valid", "The E-Mail you gave is not valid";
exit;
}
$createAccount = $LS->register($email, $pass);
if($createAccount === "exists"){
echo "User Exists.";
}elseif($createAccount === true){
header("Location: /rootTesting/dashboardInitial.php", true, 302);
die();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.easing.1.3.js.js"> </script>
<script type="text/javascript">
$(function()
{
$("ul li:first").show();
var ck_email = /^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
var ck_password = /^[A-Za-z0-9!##$%^&*()_]{3,20}$/;
$('#email').keyup(function()
{
var email=$(this).val();
if (!ck_email.test(email))
{
$(this).next().show().html("Enter valid email");
}
else
{
$(this).next().hide();
$("li").next("li.password").slideDown({duration: 'slow',easing: 'easeOutElastic'});
}
});
$('#password').keyup(function()
{
var password=$(this).val();
if (!ck_password.test(password))
{
$(this).next().show().html("");
}
else
{
$(this).next().hide();
$("li").next("li.submit").slideDown({duration: 'slow',easing: 'easeOutElastic'});
}
});
$('#submit').click(function()
{
var email=$("#email").val();
var password=$("#password").val();
if(ck_email.test(email) && ck_password.test(password) )
{
$("#form").show().html("<h1>Thank you!</h1>");
}
else
{
}
return false;
});
})
</script>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<ul>
<li class="email">
<label>Email: </label><br/>
<input type="text" name="email" id="email" />
</li>
<li class="password">
<label>Password: </label><br/>
<input type="password" name="password" id="password" />
</li>
<li class="submit">
<input type="submit" value=" Register " id='submit'/>
</li>
</ul>
</form>
</body>
</html>
Just give a name attribute to your submit button and make sure to make it's value submit. So it becomes:
<input type="submit" value=" Register " id='submit' name='submit'/>
PHP sends the data by name not by id.
Try with
$('form[name=foo]').submit(function(){
var email=$("#email").val();
var password=$("#password").val();
if(!ck_email.test(email) || !ck_password.test(password) ) {
return false;
// errors found
}
});
<form method="post" name="foo">
<ul>
<li class="email">
<label>Email: </label><br/>
<input type="text" name="email" id="email" />
</li>
<li class="password">
<label>Password: </label><br/>
<input type="password" name="password" id="password" />
</li>
<li class="submit">
<input type="submit" value=" Register " id='submit'/>
</li>
</ul>
</form>
You have missed out the name to the button.
Since you are checking the $_POST['submit'], You need to modify your button HTML as
<input type="submit" name="submit" value="Register" id="submit"/>
A php die function question.
when I use die(), it clean all page elements.
Is any way to echo error message and not clean all page, It looks like jump to another page when I use die() to stop code and call out the message.
Here are my code
<?PHP
$message="";
if(isset($_POST['submit'])){
$name=$_POST['name'];
$password=$_POST['password'];
//Field check
if($name && $password){$message=$name . $password;}
else{die($message="please enter name and password");}
//Check name
if($name=="alex" && $password==123){$message="Welcome ". $name;}
else{$message="wrong user or password";}
}
?>
<html>
<p>SIGN UP</p>
<form action="testing.php" method="POST">
<input type="text" name="name" placeholder="Enter Name" />
<input type="password" name="password" placeholder="Enter Password"/>
<input type="submit" name="submit" value="Sign up"/>
</form>
<div><?PHP echo $message?></div>
</html>
You should read your script for top to bottom, including anything outside of <?php ?>. When using die() your script stops then and there.
<?php $a = "something"; ?>
<html>
<p><?php echo $a?></p>
<?php die(); ?>
<p>Never here</p>
</html>
Would output
<html>
<p>something</p>
In your case do
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$password=$_POST['password'];
//Field check
if(!$name || !$password) {
$message="please enter name and password");
//Check name and password
} elseif ($name=="alex" && $password=="alex1") {
$message="Welcome ". $name;
} else {
$message="Username or password incorrect"
}
?>
<html>
<p>SIGN UP</p>
<form action="testing.php" method="POST">
<input type="text" name="name" placeholder="Enter Name" />
<input type="password" name="password" placeholder="Enter Password"/>
<input type="submit" name="submit" value="Sign up"/>
</form>
<div><?php echo $message?></div>
</html>
Also note that I'm using '==' to compare, not '='.