i have written a javascript function thats posts a form and redirect to home page . Im using window.location.replace to get to home page. but instead of replacing the url the function is appending the url in front of current url. Whats the problem?
$('#submit_fourth').click(function () {
// send data to server to save in table dbo.aspnet_Users =================================================
var firstname = $("#firstname").val();
var lastname = $("#lastname").val();
var username = $("#username").val();
var password = $("#password").val();
var email = $("#email").val();
$.post("/Account/Register", { FirstName: firstname, LastName: lastname, UserName: username, Password: password, ConfirmPassword: password, Email: email });
//send information to server
alert('Congratulations! Your account has been sucessfully created.');
//get back to the login screen
window.location.replace("dgsmart/Central/Login");
current url is 184.180.25.240/dgsmart/account/register after register button click it becomes 184.180.25.240/dgsmart/account/central/login
i want url lyk this 184.180.25.240/dgsmart/central/login
I assume you are using a form onsubmit event to call some function, if so you have to prevent the default behavior by return false; (for example)
window.location.replace("absolute-address"); /*window.location.replace('http://example.com/#' + initialPage); */
return false; //prevent the default behavior of the submit event
You can also using this code to set your ideal url...
<script>
location.href='RegisterNew.php';
</script>
Related
I have figured out a workaround to this problem using Javascript, but would like to know why this is happening, and to figure out a possible solution using PHP.
On my registration page (register.php) I use jQuery's preventDefault() on the submit button, but if I call header("Location: /index.php"); upon successful registration, my index page is loaded on top of my register page. The URL in my browser still says register.php as well.
Redirecting with Javascript solves the problem, but why is this happening with PHP? All other functionality of my registration script works perfectly, including other places where, upon error, I use header() to redirect users to my home page (i.e. when users try to navigate directly to .../_registerAccount.php);
jQuery:
/// <reference path="jquery-3.3.1.min.js" />
$(document).ready(function() {
$("form").submit(function(event) {
event.preventDefault();
var username = $("#register-username").val();
var email = $("#register-email").val();
var password = $("#register-password").val();
var confirmPassword = $("#register-confirm-password").val();
var submit = $("#register-submit").val();
$(".form-message").load("../shared/_registerAccount.php", {
username: username,
email: email,
password: password,
confirmPassword: confirmPassword,
submit: submit
});
});
});
PHP:
else
{
$errorEmpty = $errorUsername = $errorEmail = $errorPassword = $errorConfirmPassword = false;
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($statement, "sss", $username, $email, $hashedPassword);
mysqli_stmt_execute($statement);
session_start();
$_SESSION['register-success'] = 'You have successfully registered! Please verify your email before logging in.';
$registrationSuccessful = true;
//My Javascript workaround
//exit('<script type="text/javascript">location.assign("../index.php")</script>Home');
header('Location: ../index.php');
exit();
}
I'm trying to log in into gmail account using javascript, and I have a problem:
after I insert my email and press 'next', the page redirect me to new url asking for my password. my question is, How can I monitor the current url and know when it changes? Im trying to use page.onUrlChanged but it doesn't work
test.open(url, function(status) {
test.page.evaluate(function (email, password) {
document.getElementById('Email').value = email;
document.getElementById('next').click();
test.page.onUrlChanged = function(targetUrl) {
console.log(targetUrl);
}
}, email, password);});
you can use phatomjs for log URL change..below is simple code for that
var webPage = require('webpage');
var page = webPage.create();
page.onUrlChanged = function(targetUrl) {
console.log('New URL: ' + targetUrl);
};
find more about phantomjs
http://phantomjs.org/api/webpage/handler/on-url-changed.html
or
You can use
window.onblur = function() { console.log('blur'); }
I use below code to submit the form AND get registered in aweber email list (addlead.pl is just a registration script).
Here is what i want to accomplish:
User submits a form - it registers him in aweber email list (using two of many form fields) as it woud be signup form, then user gets redirected to normal form action url with posted information from the form (all fields)
$('#redeemform').submit(function() {
var nameVal = $(this).find('input[name="custname"]').val();
var emailVal = $(this).find('input[name="custemail"]').val();
$.post('http://www.aweber.com/scripts/addlead.pl', {
meta_web_form_id: '1234',
meta_split_id: '',
listname: 'listname',
redirect: '',
meta_adtracking: 'newsletter',
meta_message: '1',
meta_required: 'name,email',
meta_tooltip: '',
email: emailVal,
name: nameVal
});
alert("thank you"); //<<magic line
return true;
});
Code works but only with magic line - alert "thank you" - without this line it woud only submit to default form action not registering to aweber.
I've figured out that if i try submitting form (return true) and in the same time send those POST requests like this - site will refresh too fast and ingnore one of the requests.
Question is how do i do it without alert / some fixed delay in this line. Is there some kind of fancy command for it ?
Absolutely BEST solution is to let your form request call weber using CURL or similar on the server
since you cannot Ajax to another domain, you need to be more inventive if you are to run this on the client
So in the submission event we
Change the target to hiddenframe2
submit the aweber form to hiddenframe1
let the main form submit to hiddenframe2
Now you need in the RESULT of your main form return something like
<script>top.location.replace("thankyou.html");</script>
assuming your form sends the request to the same server the html comes from
and have
$('#redeemform').on("submit",function() {
$(this).prop("target","hiddenframe2");
if (!$("#hiddenframe1")) {
$("<iframe/>",{"id":"hiddenframe","name":"hiddenframe1"})
.css("display","none")
.appendTo("body");
}
if (!$("#hiddenframe2")) {
$("<iframe/>",{"id":"hiddenframe","name":"hiddenframe2"})
.css("display","none")
.appendTo("body");
}
var nameVal = $(this).find('input[name="custname"]').val();
var emailVal = $(this).find('input[name="custemail"]').val();
$("<form>",{"action":"http://www.aweber.com/scripts/addlead.pl",
"target":"hiddenFrame1"})
.append("<input/>",{meta_web_form_id: '1234'})
.append("<input/>",{meta_split_id: ''})
.append("<input/>",{listname: 'listname'})
.append("<input/>",{redirect: ''})
.append("<input/>",{meta_adtracking: 'newsletter'})
.append("<input/>",{meta_message: '1'})
.append("<input/>",{meta_required: 'name,email'})
.append("<input/>",{meta_tooltip: ''})
.append("<input/>",{email: emailVal})
.append("<input/>",{name: nameVal})
.submit();
});
Here is what COULD have done had you been able to Ajax to aweber, which you cannot because of cross domain scripting. If they support JSONP/CORS you may be able to do it anyway
$('#redeemformButton').on("click",function() {
var $form = $('#redeemform');
var nameVal = $form.find('input[name="custname"]').val();
var emailVal = $form.find('input[name="custemail"]').val();
$.post('http://www.aweber.com/scripts/addlead.pl', {
meta_web_form_id: '1234',
meta_split_id: '',
listname: 'listname',
redirect: '',
meta_adtracking: 'newsletter',
meta_message: '1',
meta_required: 'name,email',
meta_tooltip: '',
email: emailVal,
name: nameVal
},function() {
$form.submit();
});
});
and have a
<input type="button" id="redeemformButton" value="Sign up and submit" />
I'm new to CasperJS and I'm having problems in logging in into this site http://weibo.com/login.php
here's what I've tried
this.fill('form#contact-form', {
'username': 'test#test.com',
'password': 'anypassword',
}, true);
I can't used that since it has no form.
so I tried a different method using sendKeys.
this.sendKeys('.W_input ', 'tinkerbell#gmail.com');
Now my problem in here is that the input text has no ID in it, only a CLASS and both username and password have the same CLASS in it. how can i type into that textbox using only that class? or is it possible to use sendKeys using XPath?
casper supports CSS3 selectors (tuts+ has a decent rundown of the top 30 you should memorize) so you could do something like:
this.sendKeys('input[name=username]', 'tinkerbell#gmail.com');
I use querySelector function to set username and password with success:
var casper = require("casper").create();
casper.start('http://www.weibo.com', function() {
this.evaluate(function(username, password) {
document.querySelector('input[node-type="username"]').value = username;
document.querySelector('input[node-type="password"]').value = password;
document.querySelector('.W_btn_g:eq(1)').click();
}, 'your_username', 'your_password');
});
casper.then(function() {
this.wait(50000, function() {
this.echo("50s");
});
});
casper.then(function() {
this.capture('weibo.png');
});
casper.run();
This is simple casper js script to fill login form
assuming it has mentioned id's for each field
casper.evaluate(function(username, password) {
document.querySelector('input#user_email').value = username;
document.querySelector('input#user_password').value = password;
document.querySelector('input#login-button').click();
}, 'email#email.com', 'password');
I am using meteor along with the accounts-password package. I'm rolling my own login and password changing/resetting UI and want to know...
How can I customize the password reset link in the reset password email sent as a result of Accounts.resetPassword?
Currently it in the form like so: /#/reset-password/<id>'. Since I am using meteor router, I would like to send in the form '/reset-password/<id>'so I can catch it with the route '/reset-password/:id'.
Late to the party ...
Instead of changing the whole text, you can just change the url with:
Meteor.startup(function() {
Accounts.urls.resetPassword = function(token) {
return Meteor.absoluteUrl('reset-password/' + token);
};
});
It has changed a little bit:
You have to use
Accounts.emailTemplates.resetPassword.text
For the url you can simply replace the hashbang instead of parsing the token from the url. As an example (in coffeescript):
Meteor.startup(() ->
Accounts.emailTemplates.resetPassword.text = (user, url) ->
url = url.replace('#/', '')
return "Click this link to reset your password: " + url
)
ES6
Meteor.startup(() =>
Accounts.emailTemplates.resetPassword.text = function(user, url) {
url = url.replace('#/', '');
return `Click this link to reset your password: ${url}`;
}
);
See the section on email templates in the Meteor docs:
resetPassword: An Object with two fields:
resetPassword.subject: A Function that takes a user object and returns a String for the subject line of a reset password email.
resetPassword.text: A Function that takes a user object and a url, and returns the body text for a reset password email.
You can customise which url is passed to the reset password email method:
Accounts.resetPassword.text = function(user, url) {
return "Click this link to reset your password: /reset-password/" + myId;
}