jQuery Form Plugin Ignored when submitting form - html

I created an html form which submits data to my backend when submitted. The backend then redirects the webpage and shows a json message. I would like to remain on the same page and capture that json message to show to the user. I tried doing this using AJAX forms (https://jquery-form.github.io/form/) however upon clicking the submit button, the page still redirects and the script is kind of ignored.
Script included in the head after the CSS:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.min.js" integrity="sha384-FzT3vTVGXqf7wRfy8k4BiyzvbNfeYjK+frTVqZeNDFl8woCbF0CYG6g2fMEFFo/i" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script>
// wait for the DOM to be loaded
$(function() {
// bind 'myForm' and provide a simple callback function
$('#signup-form').ajaxForm(function() {
alert("Thank you for your comment!");
});
});
</script>
The form:
<form id="signup-form" action="https://example-placeholder.com/api/auth/register" method="POST">
<div class="form-group row">
<div class="col">
<input type="email" class="form-control" id="username" name="username" placeholder="Email" required>
</div>
</div>
<div class="form-group row">
<div class="col">
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
</div>
</div>
<div class="form-group row">
<div class="col">
<button type="submit" class="btn btn-custom btn-block" id="register-button">Register</button>
</div>
</div>
<label id="submit-label" style="display: none;">Check your inbox to activate your account!</label>
</form>
Response returned by backend:
return response()->json([
'message' => 'Successfully created user!'
], 201);
Errors returned by Google Chrome:
UPDATE (To fix CORS Error):
Added the following three lines before RewriteEngine On in the .htaccess file of my backend:
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

Related

reCaptcha in OctoberCMS SmallContactForm does not work

so I tried to implement reCaptcha v2 into my form in OctoberCMS. For that the SmallContactForm plugin by Janvince offers a settings area, where the secret key and the site key can be stored. It also implements the needed scripts. In the end, my code looks like this
<form method="POST" action="LINK" accept-charset="UTF-8" class="form-group" id="scf-form-id-contactForm" enctype="multipart/form-data"><input name="_handler" type="hidden" value="contactForm::onFormSend"><input name="_session_key" type="hidden" value="sessionkey"><input name="_token" type="hidden" value="token">
<div class="form-group"><label class="control-label required" for="contactForm-name" >Name</label><input id="contactForm-name" class="form-control" name="name" type="text" required=""></div>
<div class="form-group"><label class="control-label required" for="contactForm-email" >Email</label><input id="contactForm-email" class="form-control" name="email" type="email" required=""></div>
<div class="form-group"><label class="control-label " for="contactForm-betreff" >Betreff</label><input id="contactForm-betreff" class="form-control" name="betreff" type="text"></div>
<div class="form-group"><label class="control-label required" for="contactForm-nachricht" >Ihre Nachricht</label><textarea id="contactForm-nachricht" class="form-control" name="nachricht" rows="5" required=""></textarea></div>
<div id="_protect-wrapper-contactForm" class="form-group _protect-wrapper"><label class="control-label">Please clear this field</label><input type="hidden" name="_form_created" value="ididddhcfi"><input id="_protect-contactForm" name="_protect" class="_protect form-control" value="http://"></div>
<script>
document.getElementById('_protect-contactForm').setAttribute('value', '');
document.getElementById('_protect-wrapper-contactForm').style.display = 'none';
</script>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="data-sitekey"></div>
</div>
<div id="submit-wrapper-contactForm" class="form-group"><button type="submit" data-attach-loading class="oc-loader bg-innotechblue text-white font-bold rounded px-4 py-2">Send</button></div>
</form>
<script src='https://www.google.com/recaptcha/api.js' async defer></script>
The function behind the scenes:
if( Settings::getTranslated('add_google_recaptcha') ) {
try {
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".Settings::get('google_recaptcha_secret_key')."&response=".post('g-recaptcha-response')."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
} catch(\Exception $e) {
Log::error($e->getMessage());
$errors[] = e(trans('janvince.smallcontactform::lang.settings.antispam.google_recaptcha_error_msg_placeholder'));
}
I already double checked if I switched site key and secret key. I also tried to implement the script manually over and behind the form, but I always get an error:
Google reCAPTCHA validation error!
also this error is displayed twice, but I'm not sure if thats important. The Im not a robot button is rendered fine, and I works as well, but when I send the form, I get the error.
I hope someone can help me! thanks in advance!
So the I looked up the Errors in the CMS and got this:
file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0
Turns out my server did not allow url fopen. After changing that, everything worked. I also wrote the author of the plugin. He changed the source code, so it should work without the allow_url_fopen parameter.

with a standard html form post, no body

I have this form:
<form action="/signup" method="post">
<div class="field">
<label class="label">{{ message }}</label>
</div>
<div class="field">
<label class="label">Name</label>
<div class="control">
<input class="input" type="text" placeholder="Text input" value="joe">
</div>
</div>
<div class="field is-grouped">
<div class="control">
<button class="button is-link" type="submit">Submit</button>
</div>
<div class="control">
<button class="button is-link is-light">Cancel</button>
</div>
</div>
</form>
the oak server is saying i don't have a request body.
chrome dev tools is saying i do not have any Form Data.
Can anyone see why this form would post without form data?.
Request URL: https://localhost:8000/signup
Request Method: POST
Status Code: 200 OK
Remote Address: [::1]:8000
Referrer Policy: strict-origin-when-cross-origin
Specify the name attribute for your <input> element so itself and the value associated with it can be sent as name-value pairs.
Try defining <input> element like this instead:
<input name= "text-input" class="input" type="text" placeholder="Text input" value="joe">
so the request would look like this when the input would be, for e.g "myText" (shortened for demonstration purposes):
Request URL: https://localhost:8000/signup
Request Method: POST
text-input=myText
The MDN docs provide some insight on this too, you might want to visit for clarity.

Not going to specific URL this.router.navigate() in AngularJs

I am making a registration form in angularJS and I use the bootstrap template for design.
This is my form and path to the form page is http://localhost:4200/signup
<form method="post" (submit)='register(username.value,email.value,password.value)'>
<div class="form-group">
<label for="name">Full name</label>
<input type="text" id="name" #username class="form-control" placeholder="Enter your name">
</div>
<div class="form-group">
<label for="remail">Email address</label>
<input type="email" id="remail" #email class="form-control" placeholder="Enter email">
</div>
<div class="form-group">
<label for="rpassword">Password</label>
<input type="password" #password class="form-control" id="rpassword" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
But the problem is that it when I click submit button or hit enter, It automatically refresh the page and shows Cannot POST /signup.
As this is my component.ts file
register(name,email,password) {
this.userService.registerUser(name, email, password).subscribe(data => {
if (data.success) {
console.log('reaches');
this.router.navigate(['user']);
} else {
console.log('error');
}
});
}
It is trying to go to the /signup path in post method but in component.ts file I give the path to '/user'
How can it automatically go-to /signup? And how to stop that.
Your form is trying to post data to a server. It sounds like you don't have a server which can handle the post, so you get the error.
To fix the problem, you need a server which can handle the form post.

Transfer the Pop-up message to the specific location

Can Anyone solve this problem?pop-up message
I have this code below
<body>
<div class="contact-form-wrap responsive">
<!--- pop-up message start --->
<div class="status alert alert-success contact-status"></div>
<!--- pop-up message end --->
<form id="main-contact-form4" class="contact-form" name="contact-form" method="post" action="app.php" role="form">
<legend style="padding-bottom: 20px; color: #708090;">Please provide us your information.</legend>
<!-- Name Filed Starts -->
<div class="col-sm-6">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" class="form-control" name="name" id="name" required="required" placeholder="Full Name">
</div>
</div>
</div>
<!-- Name Filed Ends -->
<!---------------- Pop-up Message here ---------------->
<!-- Button starts -->
<div class="col-sm-12">
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">Submit</button>
</div>
</div>
<!-- Button Ends -->
</form>
</div>
</div>
</body>
I like to move my pop-up message on the top of the button. so every time the button will press the pop-up message will notice immediately.
here is my js
$(".contact-form").submit(function() {
var rd = this;
var url = "app.php";
$.ajax({
type: "POST",
url: url,
data: $(".contact-form").serialize(),
success: function(data) {
$(rd).prev().text(data.message).fadeIn();
}
});
return false;
});
The problem with your code is that you're selecting the popup message based on it's location in the DOM. You need to select it on a proper selector instead. Since it has a class called contact-status, let's use that.
Change:
$(rd).prev().text(data.message).fadeIn();
to
$('.contact-status').text(data.message).fadeIn();
It's a bad idea to select elements from their location in the DOM since you sometimes (like now) want to move them. Using proper selectors (like id's or classes) will keep your code working regardless where in the DOM the element is.

Strange error html form

I'm using this code for a form in HTML:
<div class="login-wrapper">
<form>
<div class="popup-header">
<span class="text-semibold"><i class="fa fa-sign-in"></i> Logging in</span>
</div>
<div class="well">
<div class="form-group has-feedback">
<label>Username</label>
<input type="text" name="user" class="form-control" placeholder="e.g. andre#mail.de">
<i class="icon-users form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label>Password</label>
<input type="password" name="password" class="form-control" placeholder="Password">
<i class="icon-lock form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label>reCaptcha</label>
<div class="g-recaptcha" data-sitekey="..."></div>
</div>
<div class="form-actions text-right">
<input type="submit" id="loginbutton" name="loginbutton" value="Login" class="btn btn-primary">
</div>
</div>
</form>
</div>
<!-- /login wrapper -->
However, when I press the submit button, it does nothing but giving me a very strange url in my browser's address bar:
http://localhost/?user=&password=&g-recaptcha-response=&loginbutton=Login
Whenever I fill out fields, it kind of puts the content into the URL:
http://localhost/?user=peter%40griffin.com&password=somepass&g-recaptcha-response=&loginbutton=Login
The intended PHP code which should be run when pressing the button won't even run or load, since this HTML stuff apparently screws things up. I don't know what I have done the wrong way. Any suggestions?
In order for the form to submit somewhere else, you need to set the form elements action parameter.
<form action="some_file.php">
Alternatively, you can take the query string and append it directly to the file path to test your script.
http://localhost/some_file.php?user=peter%40griffin.com&password=somepass&g-recaptcha-response=&loginbutton=Login
Inside of some_file.php, you would then pull out each of the variables like
$user = $_GET['user'];
$password = $_GET['password'];
The very strange url is actually the result of a GET request.
The parameters are separated by an & so you have:
user=peter%40griffin.com&password=somepass&g-recaptcha-response=
"User" is the attribute name of your input and "peter%40griffin.com" is the value
First you need to send your form to an action using the attribute action="save.php", for example an pass the parameters using the method="POST", so the user can't see the values in the URL.
<form action="save.php" method="post">