Second form on page not performing any action - php

I have two forms on a page. The first is simply to allow the user to access blocked content. (I understand the risks associated with going this route, but it's just to allow members to view certain information that does not pose any sort of risks).
This is at the top of the page...
<?php
if (isset($_POST['password']) && $_POST['password'] == 'password') {
setcookie("password", 'password', strtotime('+1 day'));
header('Location: index.php');
exit;
}
?>
.....
<form method="POST">
<input type="text" name="password">
<input type="submit" class="btn btn-full" value="Continue" />
</form>
I believe this is preventing the second form (below) from operating (when you click the submit button, nothing happens. It's as if you hadn't hit the button at all).
<form action="handler.php" method="post" class="contactForm">
<div class="form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
</div>
<div class="text-center">
<input type="submit" value="Send Message" class="btn btn-full" />
</div>
</form>
What can I do to fix the first part to allow the second form to function?

Well, I tried it and it worked.
1) Make sure you have the handler.php file at the directory of index.php(according to your code).
2) Check for any console errors. If there is any then please attach it to your post.
3) and did you override the form submit event or input button press event from js?
Also I don't see anything where you are restricting the second form to appear when password is not set in cookie..
so better use:
<?php
if (isset($_COOKIE['password'])): ?>
YOUR SECOND FORM
<?php endif; ?>
and hide the first form when password is entered.

Related

How show a automatic message after submit a form

I created a contact-form with username, email and a message.
Here is my code;
<form action="thanks.php" method="post">
<div>
<label for="nom">Nom :</label>
<input type="text" id="nom" name="user_name">
</div>
<div>
<label for="courriel">Courriel :</label>
<input type="email" id="courriel" name="user_email">
</div>
<div>
<label for="phone">Numéro de téléphone</label>
<input type="text" id="phone" name="phone_number">
</div>
<div>
<label for="message">Message :</label>
<textarea id="message" name="user_message"></textarea>
</div>
<div class="button">
<button type="submit">Envoyer votre message</button>
</div>
</form>
and I am looking for a solution after submitted this form, to make show a message with username, email and user's message.
something like;
"hi {username}, thank you for your message. we'll contact you by your {email} after reviewed your message"
I would like to know how I can make a new php page to show these details.
You would show the message on the page that processes your form request.
So if you form goes to processForm.php then when processForm.php is done processing the form it can then return whatever HTML you want it with access to the form values passed to it.
This is not really ideal and is generally handled via the page JS/HTML but if you have specific limits that require this then that is how you do it.
As for a specific example there are many on google for "php form processing"

PHP display cookies and remove suggested in browser

How do I get this cookies code to display properly? Both in Google Chrome and Firefox I get a dropdown suggestion from the browser. I would like to only have my own php "setcookie" to display.
PHP:
index.php:
<form action="index.php" method="POST">
<div class="row">
<div class="form-group">
<input type="email" class="form-control fill-bar" name="email" aria-describedby="emailHelp" placeholder="Email"
value="<?php
if(isset($_COOKIE["username"])) {
if(isset($_POST["email"])) {
echo($_POST["email"]);
}
} ?>" required>
</div>
<div class="form-group">
<input type="password" class="form-control fill-bar" id="passwd" name="password" placeholder="Password" value="<?php if(isset($_COOKIE["password"])) { echo $_COOKIE["password"]; } ?>" required>
</div>
<p><input type="checkbox" name="remember" class="mr-1">Remember Me</p>
Forgot your password?
<br><br><br>
<input type="submit" class="btn d-inline btn-primary register_button mr-3 w-100" id="login" name="login" value="Login">
<p class="text-center mt-3">No account yet? Register Here</p>
<br>
</div>
</div> <!-- end row -->
</form>
page-2.php: (inside if isset login button clicked)
if(!empty($_POST["remember"])) {
setcookie ("email",$_POST["email"],time()+ 3600);
setcookie ("password",$_POST["password"],time()+ 3600);
echo "Cookies Set Successfuly";
} else {
setcookie("email","");
setcookie("password","");
echo "Cookies Not Set";
}
Instead of my "setcookie", both Google and Firefox displayes these, I would like to have them not appear.
In your form, add the autocomplete attribute to your inputs and set it to off, like the following:
<form action="index.php" method="POST">
...
<div class="form-group">
<input type="password" class="form-control fill-bar" id="passwd" name="password" placeholder="Password" value="<?php if(isset($_COOKIE["password"])) { echo $_COOKIE["password"]; } ?>" autocomplete="off" required>
</div>
...
</form>
That should be enough to prevent autocomplete. However, browsers are not required to follow that attribute. You can check which browsers currently comply with the autocomplete attribute here. In the case of a browser not respecting the autocomplete attribute, you could try using methods to mangle the input names (such as jQuery plugins or by using PHP to generate random names and store those names in the user's session, and verify the form versus the session on submit).

AMP Project doesn´t excecute the PHP code as Form or even as echo

I have a website AMP project and I wanted to use PHP for forms.
On the webpage form which has a post method returns no action in the backend code.
So I think that the data inn´t transfered.
edit on answer:
My form code is:
<form method="POST" class="sit-form" action-xhr="ampsend.php" data-form-title="contactForm">
<div class="sit-row">
<div submit-success="" class="sit-col-lg-12 sit-col-md-12 sit-col-sm-12">
<template data-form-alert="" type="amp-mustache">success!
</template>
</div>
<div submit-error="" class="sit-col-lg-12 sit-col-md-12 sit-col-sm-12">
<template data-form-alert="" type="amp-mustache">failed! {{error}}</template>
</div>
</div>
<div class="dragArea sit-row">
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="firstnameHidden" value="Firstname"
id="firstnameHidden"
data-form-field="">
<input type="text" name="firstname" placeholder="Firstname" data-form-field="Name"
required="required" class="field-input display-4" value=""
id="firstname">
</div>
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="lastnameHidden" value="Lastname"
id="lastnameHidden"
data-form-field="">
<input type="text" name="lastname" placeholder="Lastname" data-form-field="Name"
class="field-input display-4" required="required" value=""
id="lastname">
</div>
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="emailHidden" value="Email" id="emailHidden"
data-form-field="">
<input type="email" name="email" placeholder="Email" data-form-field="Email"
required="required" class="field-input display-4" value=""
id="email">
</div>
<div class="sit-col-md-12 sit-col-sm-12 field">
<input type="hidden" name="messageHidden" value="Message"
id="messageHidden"
data-form-field="">
<textarea name="message" placeholder="Message" data-form-field="Message"
class="field-input display-4" required="required" value=""
id="message"></textarea>
</div>
<div data-for=""
class="sit-col-md-12 sit-section-btn sit-pt-4 align-center sit-col-sm-12 field">
<button type="submit" class="btn btn-primary display-4">
send
</button>
</div>
</div>
</form>
The file_put_contents of the POST by clicking on submit is:
a:8:{
s:15:"firstnameHidden";
s:9:"Firstname";
s:9:"firstname";
s:8:"TestName";
s:14:"lastnameHidden";
s:8:"Lastname";
s:8:"lastname";
s:12:"TestLastname";
s:11:"emailHidden";
s:5:"Email";
s:5:"email";
s:13:"test#test.com";
s:13:"messageHidden";
s:7:"Message";
s:7:"message";
s:11:"testMessage";}
So does it work correctly? because as return I get the "failed!" - message from the AMP template.
I think you cannot execute PHP code on the displaying document. PHP is serving the data to show through an POST on the displaying web document as described in this post.
My solution to check if PHP is running ,and check what data was posted via form - was write everything to text file.
$serializedData = serialize($_POST);
file_put_contents('your_file_name.txt', $serializedData);
Open file and see what data was pass. If you want display message after form was submitted and pass some date like name or email. You need to use amp-mustache. And and return JSON form PHP to pass it back to your AMP page.
For example in your PHP file:
header('Content-Type: application/json');
echo json_encode(array( 'name' => $_POST['name]' ));
Of course don't forget to put appropriate CORS headers in PHP or you .htaccess file.

Registration form won't display input

I am making a registration form and have made some simple php to process the email as a test:
Hello <?php echo $_POST["email"]; ?>
I am using the post method, and although the atom editor was displaying the Hello, it doesn't display the email.
And in case anyone needs it, here is the html I am using:
<form action="welcome.php" method="post">
<div class="container">
<h1>Register</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<label for="email"><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw"
required>
<label for="psw-repeat"><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="psw-
repeat"
required>
<hr>
<p>By creating an account you agree to our <a href="#">Terms &
Privacy</a>.</p>
<button type="submit" class="registerbtn">Register</button>
</div>
<div class="container signin">
<p>Already have an account? Sign in.</p>
</div>
</form>
this code works fine, please make sure you echoing Hello <?php echo $_POST["email"]; ?> in welcome.php
if yes make sure you have php installed.
There is no error. If you place the php code provided in above in welcome.php, and you fill the form, email is displayed properly after Hello. There migh

Call php validation file with ajax call

I am trying to submit a form using Ajax, the problem is the validation file I made is not called at all (I checked it by putting a bit of js alert inside the PHP validation file). I searched the internet and tried a lot of solutions but none worked. Is there something wrong I am making in my code?
Here is my HTML:
<form method="post" id="signup">
<div class="form-group">
<input type="fname" name="fname" id="fname" value="" class="form-control" placeholder="Your First name" />
</div>
<div class="form-group">
<input type="lname" name="lname" id="lname" value="" class="form-control" placeholder="Your Last name" />
</div>
<div class="form-group">
<input type="email" name="email" id="email" value="" class="form-control" placeholder="Your Email" />
</div>
<div class="form-group">
<input type="password" name="password" id="password" value="" class="form-control" placeholder="Your Password" />
</div>
<div class="form-group submit-btn">
<input id="signupsumbit" type="submit" name="submit" value="Sign Up" class="btn btn-success btn-lg"/>
</div>
</form>
And here is the ajax call:
$(function (){
$('#signup').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
data: $('#signup').serialize(),
url: 'login.php',
success: function (data) {
alert("data is"+data);
}
});
});
});
Here is a bit of my PHP code:
include("connection.php");
echo "<script>alert(2)</script>";
if (isset($_GET['logout']) and $_GET['logout']==1 and isset($_COOKIE[id]))
{
setcookie("email", '', time()-60*60*24);
setcookie("password", '', time()-60*60*24);
setcookie("id", '', time()-60*60*24);
$message = "You have been logged out! Come back soon!";
}
if(mysqli_connect_error()) // if failed to connect to database
{
die("Could not connect to database"); //stop the whole script from runnning
}
if (isset($_POST['submit']) and $_POST['submit'] == 'Sign Up')
{
echo '<script>console.log(1)</script>'; //
$error = '';
$success = '';
// email validation
$email = $_POST['email'];
if (empty($_POST['email']))
{
$error .="<br />Please enter your email";
}
else
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$error .="<br />Please enter a valid email address";
}
}
.
.
.
.
?>
And of course, the PHP file I made validation at is called login.php.
As you may notice, I put two alerts in my PHP code, I alerted 2 to make sure that the file is called, and alerted 1 to make sure that the form was posted. Odds are, when I submit the form the normal way (with no AJAX), both of alerts work, but when I submit it with AJAX, none of them is alerted.
My Console shows this:
Any help would be appreciated. Thanks.
Testing your script, i found that the submit button is not sent to your login.php script. If you e.preventDefault() and then later trigger the submit, javascript was submitting the form but not the button. To fix your issue create a hidden field with name submit and value Sign Up and remove the name attribute from your submit button (don't need that) and it should work fine.
<form method="post" id="signup">
<input type="hidden" name="submit" value="Sign Up"> <!-- note this hidden field -->
<div class="form-group">
<input type="fname" name="fname" id="fname" value="" class="form-control" placeholder="Your First name" />
</div>
<div class="form-group">
<input type="lname" name="lname" id="lname" value="" class="form-control" placeholder="Your Last name" />
</div>
<div class="form-group">
<input type="email" name="email" id="email" value="" class="form-control" placeholder="Your Email" />
</div>
<div class="form-group">
<input type="password" name="password" id="password" value="" class="form-control" placeholder="Your Password" />
</div>
<div class="form-group submit-btn">
<input id="signupsumbit" type="submit" value="Sign Up" class="btn btn-success btn-lg"/><!-- note the removed name attribute -->
</div>
</form>

Categories