I want to process a GET extension in a HTML page and not a PHP page.
I have looked through the internet and not found anything.
URL = examplesite.com?id=1234
I assume this would go to the index page on the domain. As the index page is a HTML page, is there a way to get the details of the extension transferred to another link I have in the html script that emails me when someone looks at the site.
<script src="trigger.php">
</script>
This way I can customise the extension to know where the person found me. id=1234 is from twitter, id=2345 from FB etc.
Then i could place the extension onto the script to send me the email.
<script src="trigger.php?id=1234">
</script>
Is there a way to get the HTML page to process extension and pass it on in a variable of some sort.
Thanks in advance
Robert
You can do it in Javascript in the HTML. window.location.search contains the query string from the URL.
You can then use an AJAX request to send the query string to your server script.
<script>
$(document).ready(function() {
var script = 'trigger.php' + window.location.search;
$.get(script);
});
</script>
This is not possible with plain HTML. By definition, HTML is not dynamic. It can't process anything you want. However, there are three options.
Firstly, you can use JavaScript and AJAX calls to make another HTTP request to examplesite.com/processID.php (or another PHP page) which will process the request.
Another way to use JavaScript would be to use a client side API such as MailChimp to send the email directly from the users computer.
Or you could just redirect your root page for your domain examplesite.com to lead to index.php. I'm sure that's very easy to configure in mainstream servers such as Apache or Nginx. Otherwise please ask another question on Server Fault about how to set this up using your server.
If you are using a PHP hosting provider, they should also be able to help redirect the root page. If you don't have any access to PHP on your hosting provider, you're out of luck. You must only use the second option.
Do it with ajax
<form id="form1">
<input type="text" />
<button type="submit" id="sendforms">send</button>
</form>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#sendforms").click(function() {
var combinedFormData = $("#form1").serialize();
$.get(
"trigger.php",
combinedFormData
).done(function(data) {
//alert("Successfully submitted!");
$("#result").html(data);
}).fail(function () {
//alert("Error submitting forms!");
})
});
});
</script>
Related
I'm trying to create a javascript file with functions that allow me to redirect to another page. But it is not redirected. This is my code:
on my functions.js
function openUrl(url) {
window.location = url;
}
on my php page
<script>openUrl('<?=$my_url?>');</script>
<script src="./functions.js" type="text/javascript"></script>
Order matters
You call openUrl, which (if you have looked at the Console in your browser's developer tools) is throwing a ReferenceError because it isn't defined.
Then you load your functions.js which defines openUrl (but by then it is too late).
That said, if you are generating an HTML document which immediately goes to a new URL with JS then you should probably be simply issuing a 301 or 302 redirect response instead of an HTML document in the first place.
switch these two lines
<script src="./functions.js" type="text/javascript"></script>
<script>openUrl('<?=$my_url?>');</script>
I am trying to visit a website and log in automatically.
After this, I want to return whether or not this was a success or failure, but I can't get it to work. The page loads blank and nothing happens.
Note: I have deliberately greyed out the URL.
Below is my attempt,
<HTML>
<HEAD>
<script src="https://code.jquery.com/jquery-2.2.3.min.js" type="text/javascript"></script>
<TITLE>Login</TITLE>
</HEAD>
<BODY>
</BODY>
</HTML>
<script type="text/javascript">
$(document).ready(function () {
runLogin();
});
function runLogin(){
alert("start");
if (window.location.indexOf("url") > -1) {
jQuery("#j_username").val("username");
jQuery("#j_password").val("password");
jQuery("#loginForm").submit();
}
}
</script>
If you want to test your login form, use Protractor or other e2e test framework, but this way does not seem very safe.
http://angular.github.io/protractor/
It's loading a blank page because your back-end script isn't redirecting back to the page you started on. One option is to send the current webpage URL as an extra field to the login form so that it can redirect back after it has logged in. This will require you to refactor your back-end code so that it can handle this extra information (not really possible if you're using j_security_check). Another option is to make an AJAX request to your login page so that it doesn't actually redirect the page, it just submits the form quietly and then JS handles the response.
If you want to add the URL of the current page to the login request, do this:
function runLogin() {
if (window.location.indexOf("url") > -1) {
jQuery("#j_username").val("username");
jQuery("#j_password").val("password");
jQuery("#loginForm").append(
'<input type="hidden" name="url" value="'+window.location.href+'"/>'
);
jQuery("#loginForm").submit();
}
}
If you want to use AJAX to send a login request:
function runLogin() {
var $form = $("#loginForm");
$.post($form.attr("action"), $form.serialize(), function(data) {
// login successful/unsuccessful message displays
// depending on what "data" contains
});
}
how do i redirect to a welcome page from an index page using javascript?
The site is offline and on a development machine and not in a server www directory like (wamp, lamp, apache etc) . I would like to do it without using PHP, python etc cause I already know how it is done in php using header(location ... ).
The directory structure
site
|
|--img
|--css
|--index.html
|--welcome.html
|--error.html
I have already tried window.location, window.location.href etc.
Inside the script of index.html.
if (true){
self.location("welcome.html");
}
else{
window.location.href = "error.html";
}
Though Window.location is a read-only Location object, you can also
assign a DOMString to it. This means that you can work with
window.location as if it were a string in most cases: window.location
= 'http://www.example.com' is a synonym of window.location.href = 'http://www.example.com'.
Mozilla Docs -- Window.location
It is not a function but you can assign a string to it.
Just write:
window.location="error.html";
self.location="error.html"; is fine too
You could also use a meta-refresh redirect:
<meta http-equiv="refresh" content="0;URL='welcome.html" />
Or just do window.location="welcome.html" instead of self.location("welcome.html")
``This is very simple to do a page redirect using JavaScript at client side. To redirect your site visitors to a new page, you just need to add a line in your head section as follows:
<head>
<script type="text/javascript">
<!--
window.location="http://www.newlocation.com";
//-->
</script>
</head>
Thanks for all your advice.
But I was able to do it myself using window.location method. I found that the method was not the problem because I had tried it already previously.
The problem was that the redirection happened but since the function "checkLogin" was called by the onsubmit function of html element form,
it kept coming back to the same login page.
I fixed it by returning false at the end of the checkLogin() script.
But Thanks a ton for all your input.
I'm using REST to POST (from Firefox's Poster) a url:
http://[ip]/page.jsp?paramater1=whatever¶meter2=whatever
(Content Type: application/x-www-form-urlencoded)
The page.jsp contains:
<body onload="onload()">
<script>
document.forms["myform"].submit(); // just to be redundant
function onload(){
document.forms["myform"].submit(); // just to be redundant
}
</script>
<form action="SessionTestDriver" method="post" id="myform">
[form stuff]
</form>
But it doesn't seem to be submitting that form. If I manually load the page on a browser, everything works perfectly. It's just the REST call that does nothing.
Clearly I'm missing something. Advice?
SOLVED!
Got it! The main jsp page just called a servlet on submit. I tried that servlet directly in the REST url instead of the jsp page and everything worked how I wanted!
It sounds like you're making a request to a page that contains javascript, and you're concerned that the javascript on the requested page isn't running.
This is expected. When you request that page, the response is returned as a string, and that's it. The page isn't parsed, and javascript isn't evaluated. When you make an AJAX call, don't expect javascript in the page you're POSTing to to run.
(Sorry for explaining something so elementary if I've misunderstood your question.)
Not sure how HTML form and REST are being used, but you might want to make sure the document is loaded entirely first:
Try (if using jQuery)
<script>
$(document).ready( function() {
$("#myform").submit();
});
</script>
I need to send an email(silently) each time page was loaded.
I tried below code :
<html>
<head>
<script language="javascript" type="text/javascript">
var email = "h.kamrava#yahoo.com"
var subject = "test"
var body = "Hello world"
document.write("<form name="form" action=\"mailto:"+ email +"\?subject="+ subject +"\&body="+ body +"\" method=\"post\" enctype=\"text/plain\"></form>")
</script>
</head>
<body onload="document.form.submit()">
</body>
</html>
It's does't work to me! What am i doing wrong? tnx
I suggest you let the server do this, like PHP mail, rather than have JS do it. You can send a request to the server on page load to tell the server to send an email to your target. That way:
No messy, creepy looking scripts on your page.
They'll never know an email was actually sent.
"Fire and forget". Send the request to the server, and done! The server does the rest.
Your email address, written only on the server, stays hidden in the server side and avoid email harvesters and subsequently, spam.
The request that triggers it could be anything, like loading a 1x1 gif, a script, AJAX or whatever.
I don't think you can do the same through forms, especially a post form. In either case what you do won't be silent as it requires the user to send that email. mailto: simply presents the user with a way to send the email using their own client.
If you want to send an email quickly from your HTML document, use mailto: from within an anchor, then either wait for the user to click it or force it to click.
My link
If you can use PHP or some other server-side language, it'd be much easier and "silent". Check out the mail() function of PHP, which allows you to send an email to whoever from whoever.
you need to use ajax for this purpose .
for example you can use JQuery ajax , something like this :
$(document).load('ajax/mail.php?email=exp#mail.com&subject=test&body=hi')
and then in php page use mail() function to send it
EDIT: Looking at the form code, I can see that this won't actually send an email...but this would be how you would submit a form using javascript. If you want to send an email silently, you will need to post to some backend service.
Try putting everything into a load handler:
<html>
<head>
<script language="javascript" type="text/javascript">
function sendEmail(){
var email = "h.kamrava#yahoo.com"
var subject = "test"
var body = "Hello world"
document.write("<form name="form" action=\"mailto:"+ email +"\?subject="+ subject +"\&body="+ body +"\" method=\"post\" enctype=\"text/plain\"></form>");
document.form.submit();
}
</script>
</head>
<body onload="sendEmail()">
</body>
</html>
checkout this :
http://jsfiddle.net/4gGFR/
and instead of calling form post, call that <body onload="sendemail();">