I have this piece of code and I want to show result in the third input box not in the input box and hole page and i want to know if i can a $_GET['$vairbles'] whiten functions
NOTE : I’m beginner in php programming
Here is my code :
<?php
$email = "mail#somedomain.com";
if (isset($_GET['txt1']) and isset($_GET['txt2'])) if (!empty($_GET['txt1']) and!empty($_GET['txt2'])) {
$tex1 = $_GET['txt1'];
$tex2 = $_GET['txt2'];
echo "They are all filled.<br>";
} else {
echo "Please fill in first and second Fields";
}
shwor();
Function shwor() {
global $email;
global $tex1;
global $tex2;
$len1 = strlen($tex1);
$len2 = strlen($tex2);
if ($len1 and $len2 > 0) if ($tex1 == $tex2) echo "matched ";
else echo "Does not match";
}
?>
<form action:"email.php" method="GET">
<input 1 type="text" name="txt1"><br><br>
<input 2 type="text" name="txt2"><br><br>
<input 3 type="text" name="Result" value = "<?php shwor();?>"><br><br>
<input type="submit" value="Check matches"><br>
</form>
everything works, just misspelled in this line:
<form action:"email.php" method="GET">
should be:
<form action="email.php" method="GET">
..and remember to name your file email.php that the data will be sent to the file itself ;-)
Related
Hello Every One i am new to coding and now i am learning php and html below i submited the code and i want to print Hello World in a new page if the given user name and password is correct but it is printing in the same page can any one help me Thanks in Advance
<!doctype html>
<html>
<body>
<form action="#" method="post">
<input type="text" name="username" placeholder="Enter The Username"><br>
<input type="password" name="password" placeholder="Enter Password"><br>
<input type="submit" name="submit" value="Login">
<input type="reset" value="Cancel">
</form>
<?php
$a = 123;
$b = 234;
$c = $_POST["username"];
$d = $_POST["password"];
$e = "Hello World!!";
$f = "Error 404 Page Not Found";
if(isset($_POST['submit'])){
if ($a == $c && $b == $d )
{
print "$e";
}
else
{
echo "$f";
}
}
?>
</body>
</html>
you could use the header function and it would look like this:
if ($a == $c && $b == $d )
{
header("location: newpage.php");
exit;
}
else
{
echo "$f";
}
or indeed as stated above me redirect the form to the new page
First of all this will be the html part in a separate part like login.html:-
<!doctype html>
<html>
<body>
<form action="login.php" method="post"><!-- if you will not give action then form will posted to the same page -->
<input type="text" name="username" placeholder="Enter The Username"><br>
<input type="password" name="password" placeholder="Enter Password"><br>
<input type="submit" name="submit" value="Login">
<input type="reset" value="Cancel"><!-- about this i cannot say anything -->
</form>
</body>
</html>
Now in login.php:-
<?php
$original_user_name = 123; // take variable name that are self descriptive
$original_user_password = 234;
$form_username = $_POST["username"];
$form_password = $_POST["password"];
// $e = "Hello World!!"; no need of extra variable
// $f = "Error 404 Page Not Found"; no need of extra variable
if(isset($_POST['username']) && isset($_POST['password'])){ // check with POSTED values not with button value
if ($original_user_name == $form_username && $original_user_password == $form_password){
echo "hello World!";
}else{
echo "Error 404 Page Not Found!";
}
}else{
echo "please fill both user name and password!";
}
?>
Note:- both files must be in the same working directory.
Create a new .php file where you can put your PHP code & in the html file, change form tag to < form action="path of that php file" method="post">
test.php
<?php
//CLICK SUBMIT BUTTON
if(isset($_POST['submit']))
{
$membername = $_POST['membername'];
$errors = '';
if(empty($membername))
{
$errors = "Please enter member name!<br />";
}
if($errors)
{
//MEMBER NAME TEXTFIELD EMPTY
//SHOW ERROR MESSAGE AND DISPLAY FORM AGAIN
echo '<span style="color:red;font-weight: bold;">'.$errors.'</span>';
displayForm();
}
else
{
//GO TO OUTPUT.PHP PAGE
header("Location:output.php");
exit();
}
}
else
{
displayForm();
}
?>
<?php
//DISPLAY FORM
function displayForm()
{
?>
<html>
<head></head>
<body>
<form action="test.php" method="post">
Member Name
<input type="text" name="membername" value="<?php if(isset($_POST['membername'])) echo $_POST['membername'];
else echo ''; ?>" /><br />
<input type="submit" name="submit" value="add" />
[HERE]
</form>
</body>
</html>
<?php
}
?>
In [HERE] section, I write the hidden input field:
<input type="hidden" name="mname" value="<?php echo $_POST['membername']; " />
After that, I go to output.php get the hidden field value:
<?php
echo $_POST['mname'];
?>
When I run the code, I get this error:
Undefined index: mname
What happened to my program?
header() function cannot applied to $_POST method?
Any solutions to solve it?
There Could be two solutions to get the value on redirected page :
1. By Session :
You can put the value in session and get on the redirected page.
$_SESSION['mname'] = $_POST['mname'];
2. Using GET :
You can send values in header.
header("Location:output.php?val=$_POST['mname']");
I have created a PHP form to take 4 text fields name, email, username and password and have set validation for these. I have my code currently validating correctly and displaying messages if the code validates or not.
However, I would like for it to keep the correctly validated fields filled when submitted and those that failed validation to be empty with an error message detailing why.
So far I have the following code, the main form.php:
<?php
$self = htmlentities($_SERVER['PHP_SELF']);
?>
<form action="<?php echo $self; ?>" method="post">
<fieldset>
<p>You must fill in every field</p>
<legend>Personal details</legend>
<?php
include 'personaldetails.php';
include 'logindetails.php';
?>
<div>
<input type="submit" name="" value="Register" />
</div>
</fieldset>
</form>
<?php
$firstname = validate_fname();
$emailad = validate_email();
$username = validate_username();
$pword = validate_pw();
?>
My functions.php code is as follows:
<?php
function validate_fname() {
if (!empty($_POST['fname'])) {
$form_is_submitted = true;
$trimmed = trim($_POST['fname']);
if (strlen($trimmed)<=150 && preg_match('/\\s/', $trimmed)) {
$fname = htmlentities($_POST['fname']);
echo "<p>You entered full name: $fname</p>";
} else {
echo "<p>Full name must be no more than 150 characters and must contain one space.</p>";
} }
}
function validate_email() {
if (!empty($_POST['email'])) {
$form_is_submitted = true;
$trimmed = trim($_POST['email']);
if (filter_var($trimmed, FILTER_VALIDATE_EMAIL)) {
$clean['email'] = $_POST['email'];
$email = htmlentities($_POST['email']);
echo "<p>You entered email: $email</p>";
} else {
echo "<p>Incorrect email entered!</p>";
} }
}
function validate_username() {
if (!empty($_POST['uname'])) {
$form_is_submitted = true;
$trimmed = trim($_POST['uname']);
if (strlen($trimmed)>=5 && strlen($trimmed) <=10) {
$uname = htmlentities($_POST['uname']);
echo "<p>You entered username: $uname</p>";
} else {
echo "<p>Username must be of length 5-10 characters!</p>";
} }
}
function validate_pw() {
if (!empty($_POST['pw'])) {
$form_is_submitted = true;
$trimmed = trim($_POST['pw']);
if (strlen($trimmed)>=8 && strlen($trimmed) <=10) {
$pword = htmlentities($_POST['pw']);
echo "<p>You entered password: $pword</p>";
} else {
echo "<p>Password must be of length 8-10 characters!</p>";
} }
}
?>
How can I ensure that when submit is pressed that it will retain valid inputs and empty invalid ones returning error messages.
Preferably I would also like there to be an alternate else condition for initial if(!empty). I had this initially but found it would start the form with an error message.
Lastly, how could I record the valid information into an external file to use for checking login details after signing up via this form?
Any help is greatly appreciated.
Try using a separate variable for errors, and not output error messages to the input field.
You could use global variables for this, but I'm not fond of them.
login.php
<?php
$firstname = '';
$password = '';
$username = '';
$emailadd = '';
$response = '';
include_once('loginprocess.php');
include_once('includes/header.php);
//Header stuff
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8");?>" method="post">
<fieldset>
<p>Please enter your username and password</p>
<legend>Login</legend>
<div>
<label for="fullname">Full Name</label>
<input type="text" name="fname" id="fullname" value="<?php echo $firstname ?>" />
</div>
<div>
<label for="emailad">Email address</label>
<input type="text" name="email" id="emailad" value="<?php echo $emailadd; ?>"/>
</div>
<div>
<label for="username">Username (between 5-10 characters)</label>
<input type="text" name="uname" id="username" value='<?php echo $username; ?>' />
</div>
<div>
<label for="password">Password (between 8-10 characters)</label>
<input type="text" name="pw" id="password" value="<?php echo $password; ?>" />
</div>
<div>
<input type="submit" name="" value="Submit" />
</div>
</fieldset>
</form>
<?php
//Output the $reponse variable, if your validation functions run, then it
// will contain a string, if not, then it will be empty.
if($response != ''){
print $response;
}
?>
//Footer stuff
loginprocess.php
//No need for header stuff, because it's loaded with login.php
if($_SERVER['REQUEST_METHOD'] == 'POST'){//Will only run if a post request was made.
//Here we concatenate the return values of your validation functions.
$response .= validate_fname();
$response .= validate_email();
$response .= validate_username();
$response .= validate_pw();
}
//...or footer stuff.
functions.php
function validate_fname() {
//Note the use of global...
global $firstname;
if (!empty($_POST['fname'])) {
$form_is_submitted = true;
$trimmed = trim($_POST['fname']);
if(strlen($trimmed)<=150 && preg_match('/\\s/', $trimmed)){
$fname = htmlentities($_POST['fname']);
//..and the setting of the global.
$firstname = $fname;
//Change all your 'echo' to 'return' in other functions.
return"<p>You entered full name: $fname</p>";
} else {
return "<p>Full name must be no more than 150 characters and must contain one space.</p>";
}
}
}
I wouldn't suggest using includes for small things like forms, I find it tends to make a mess of things quite quickly. Keep all your 'display' code in one file, and use includes for functions (like you have) and split files only when the scope has changed. i.e your functions.php file deals with validation at the moment, but you might want to make a new include later that deals with the actual login or registration process.
Look at http://www.php.net/manual/en/language.operators.string.php to find out about concatenating.
How do I make error show on top of form so that if $user->success == true, it wont show my form then. Removing that last else would help, but then form shows after success. One way is to redirect that. Maybe tehre
if (isset($_POST["submit"]))
{
if ($_POST["formid"] == $_SESSION["formid"])
{
$_SESSION["formid"] = '';
$User->signin($_POST['username'], $_POST['password']);
}
else
$User->CheckUser();
if ($User->success == true) {
include ('in.php');
}
if ($User->error)
echo "<p>" . $User->error . "</p>";
else
echo 'Don\'t process form';
$_SESSION["formid"] = md5(rand(0,10000000));
} else {
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
Username:
<input id="username" name="username" type="text" /><br />
Password:
<input id="password" name="password" type="password" /><br />
<input type="hidden" name="formid" value="<?php echo $_SESSION["formid"]; ?>" />
<input type="submit" name="submit" />
<br />
Register
</form>
<?php }?>
Perhaps the simplest approach is to just create a variable $show_form to use to determine whether form is to be shown,
$show_form = true;
if(isset($_POST['submit'])) {
// do your form processing here.
// If you decide everything is good and you don't want to show the form,
// just add this line:
$show_form = false;
} // don't use else here
if (true === $show_form) {
?>
<form>...</form>
<?
}
?>
add this code before your form tag
<?php if (isset($User->error) AND $User->error)?>
<p><?php echo $User->error?></p>
<?php?>
I have a form that a user can enter in first name, last name and email. You can submit this form multiple times in case you would like to send to more users by simply clicking on submit (of course, so long as the fields are entered correctly using sanitation and validation).
if (isset($_POST['Submit'])) {
//sanitize if field (like email) is not empty, then validate it using a function
// such as FILTER_VALIDATE_EMAIL and FILTER_SANITIZE_STRING
//like if ($_POST['email'] != "") { do the sanitation and validation here }
//then if no error send the email
//then $_POST = array(); to clear up form for the next entry
}
<form> form here</form>
Do you guys have an idea by just laying out this concept? Or do you need a sample code? Thanks for your help.
Was trying to use what Joe suggested but didn't work. Any further help?
session_start();
if (isset($_POST['Submit'])) {
if (isset($_SESSION['submission_count'])) {
if ($_SESSION['submission_count'] > 10) {
echo "You can't submit so many!";
exit;
}
}
else {
$_SESSION['submission_count'] = 0;
}
$_SESSION['submission_count'] += 1;
// Form validation and sanitation
// Send email
}
?>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name:
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" size="50" /><br />
Last Name:
<input type="text" name="lastname" value="<?php echo $_POST['lastname']; ?>" size="50" /><br />
Email Address:
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/> <br/><hr />
<br/>
<input type="submit" class="button" name="Submit" />
</form>
You can store a counter in $_SESSION.
http://www.php.net/manual/en/intro.session.php
<?php
// Starting the session
session_start();
if (isset($_POST['Submit'])) {
if (isset($_SESSION['submission_count'])) {
if ($_SESSION['submission_count'] > 5) {
echo "You can't submit so many!";
exit;
}
}
else {
$_SESSION['submission_count'] = 0;
}
$_SESSION['submission_count'] += 1;
// Do the rest of your form submission
}