Alerting the user that the update has been successful in Javascript - javascript

I am trying to use the following javascript code to alert the user when a form has been updated.
Basically what happens is a user clicks on a list of prospective clients, they click edit, this then loads all the clients details into an editor form. Once they have finished editing the user they click the update button and the contents are posted to a php processing script, which also has various other php functions in it that perform other commands before the user is then sent back to the prospect editor where the user details are re loaded into the form again with all the edits applied, this is all done in a second or two.
Once the update has been successful it adds a token into the GET array called update=1 which is passed in the url back to the editor.
What I am trying to do is alert the user when the clients record has been successfully updated by way of an alert box in javascript.
I am using the following code but am no sure how to get the javascript to look for the update=1 in the url and only display update successful when the client has been updated, and not on the initial load when the client is getting loaded from the list of clients.
JAVASCRIPT
<script language="JavaScript">
function prospectupdated ()
{alert("Prospect Updated !")
}
</script>
HTML
<body onLoad="prospectupdated()">
As it stands at the moment it loads every time the page loads, whether the update=1 token in the url is there or not, does anyone know of any tutorials on how to do this, or knows how it do it themselves and doesn't mind sharing ? :-)
Thanks

You need to test the update:
<body <?php isset($_GET['update']) && $_GET['update'] == 1 ? print "onload='prospectupdated()'" : '' ?> >

Look here: How to retrieve GET parameters from javascript?
It will show you how to obtain the value for the update=1.
You would then have an if() statement to check if update=1. If it does, then execute the alert().

Related

Redirect user to login page from JQuery autocomplete if not logged in

When users log into my PHP website, they are automatically logged out after a period of inactivity.
I have a few input fields on the main page for searching for stuff. Input type="text" with JQuery autocomplete option on them. The JQuery autocomplete takes user input and sends it to a PHP file that searches the database and returns data in JSON format.
The original page displays the data returned in a clickable list.
Now, if users have been logged out due to inactivity, this isn't immediately obvious, unless of course they choose to reload the page. Most don't, however. So if they just start entering search words, they just get nothting, because they're not logged in.
I already have a routine to redirect users to the login page if they're not logged in. But this doesn't work in this case, because it's the JSON-generating page that is being redirected, that that doesn't really have any influence on the main page.
Also, I can't see how I can interact directly with what's being returned - it seems like JQuery does all of the data handling internally, so I can't really grab onto data returned and tell the main page to redirect in that case.
How do I achieve the desired result?
On server side ajax first check login condition and based on that give respons e like {login:false} something like that, in your jquery function check that response and if login=false then redirect using window.location.

Carry out a mysql function before exiting a page

I want the page to update the mysql database whenever the admin exits the page.
This is a timeline of events:
users submit assignments and The administrator would select a script to mark, when the admin opens the script (in a mark.php page), the page would update the database that someone is marking that particular script and when the admin exits the mark.php page, the page should automatically update the database to show nobody's marking the page.
i created a column in the database to show if someone is marking the script or not, if so it would enter the administrator's user id into the column, and when the admin exits the page it should remove it.
The function to update the database before exiting the page isn't working.
This is what i have
<script>
function exitmarking()
{
return "dsds";
<?php
$connect->executeQuery("UPDATE assignment_submit SET markedby='' WHERE assignment_submit_='$asid'");
?>
}
</script>
and the html
<body onbeforeunload="return exitmarking()"></body>
Since PHP is server-side code, the database query would always be executed before the user ever sees the page rendered - no matter how you structure the location of the PHP code in the page (within your Javascript event).
What you can do is a separate PHP page to do the UPDATE query, and then on exit, use Javascript to do an AJAX call (either native or Jquery or whatever framework you prefer) in your marking javascript function.

How to append to form "Thanks for the email!" after submit button is pressed?

I'm building a contact form for my website and I would like users to enter their information, click submit, have the information process with php and to just have a message display on the bottom of the form. Originally, I was redirecting to another page and the form was working properly, but now I just want to append a small message and can't get it to work.
This is what I have currently in my contactengine.php file for it to process all the variables and send the email.
if ($success){
THIS IS WHAT I DID BEFORE THAT WORKED BUT HAS TO REDIRECT
// print "<meta http-equiv=\"refresh\" content=\"0;URL=index_thanks.html\">"; //
//BELOW IS WHAT I WANT TO OCCUR
<script type="text/javascript">
$("#contact-area").append("Thanks for your message!")
</script>
}
You don't need JavaScript for this. JavaScript is great, but it should only be used where necessary, since it can add some brittleness to the user experience.
Here's a possible design approach:
Set up a POST form
Process the form, set a session variable to thank the user, and redirect to the form again. This switches the browser from POST to GET mode†, and is usually as simple as this:
header('Location: http://yourdomain.com/form.php');
exit();
Render the form as usual. If a session variable is set to thank the user, render the thank you message, and unset the session variable‡
I wouldn't use a <meta /> refresh here - it needs to load more data to work, and is more fiddly to implement.
† The POST-redirect approach is a common design pattern in web development. There are plenty of resources about this on the web, if you are unfamiliar with it. It fixes problems associated with rendering web content inside a POST method, particularly in relation to page refreshing and using the back/forward buttons.
‡ These temporary session variables are sometimes known as flash variables. They exist from one HTTP operation to the next, and are erased at the end of the second operation.
You cannot put JavaScript inside of your PHP code. However, I will give you an explanation of what you need to do.
On the client side, bind an event for the click, then
$('form button').click(function() {
$.post('file.php', {'object': 'property'}, function(success) {
if(success) {
$("#contact-area").append("Thanks for your message!")
}
});
});
On the server side, you will process this:
//file.php
$sentByAJAX = $_POST['object'];
return true;

Submit form, process info with script, and display results on the same page

I am basically trying to do the same as this person:
jQuery - submit form via AJAX and put results page into a div...?
This is, submit a form and display a message on the same page after a successful submission.
However, my case is slightly different. I am using a script that allows me to e-mail links to download files, and this script controlls the success or failure messages. In adittion, I am using a CMS, Joomla.
So, the way this normally works is like this:
You fill out a form on a page and press send. After submission, you see an html page with a thank you message. If anything goes wrong, you see a failure message. If everything goes well you receive an email with a link to download the files.
To be able to do this one must do the following:
1) Add the following line on top of the page (call the script):
<?php require_once("myscript.php"); ?>
2) Add the form
The form's action must be the script file:
<form action="myscript.php" method="post">
3) Tell the script the parameters for the e-mail: which files, expire time for links, and very importantly the address of the return url, which can be the one with the thank you message. The other parameters are not important really.
<?php emailfunction('myfile.zip',15,1,'thankyou.html',0); ?>
What I want to do is to be able to show a message on the same page after submission, as the example mentioned at the beginning of this post. Ideally, hide the form and show the message. However, I haven't managed to do it.
I think my problem is that I don't know how to:
tell the server to send back only the portion of the HTML that you
want to use to populate the DIV (which is the more efficient way), or
pick out the specific html from the server response that you want,
e.g. $("#someDiv").html($(html).find('#results'));
As the author of the answer in the previous example indicates.
When something goes wrong, I get sent to the address specified inside the script like this:
$ErrorTemplate="/home/server/public_html/errortemplate.html";
If I delete the thankyou.html address from here:
<?php emailfunction('myfile.zip',15,1,'thankyou.html',0); ?>
I get sent to the address specified in the action parameter of the form.
So what I think I am missing, but I have no idea how to fix, is an answer from the script that can then be processed by the jQuery. I also tried this:
http://support.webvanta.com/support_article/615726-submitting-forms-with-ajax
But it didn't work either.
I have been in touch with the developer of the script but, even though he's a really nice guy, he says he doesn't know anything about jQuery nor Ajax, so he cannot really help me.
You can see the form I am using here:
http://perrerac.org/musica/chile/congreso/397-congreso-congreso
If you check the header you'll find a file: perrerac.org/downloader/ajaxsubmitform.js
Which is an attempt to replicate the example in the post aforementioned.
Any help is much appreciated!
Here's an example of submitting with ajax and getting part of returned full page and using just that snippet of html to insert in current page
This assumes that joomla doesn't do some redirect to unwanted page as a result of processing via ajax.
/* using very generic selector for form, should use more unique one to that form*/
$('form').submit(function(event){
/* prevent normal browser submit*/
event.preventDefault();
/* do some validation, return if invalid*/
/* collect the data to send*/
var data=$(this).serialize();
/* using shortcut method $.post, can use $.ajax for access to all options and more flexibility if needed*/
$.post('path/to/action', data,function( responseHtml){
/* create a jQuery object out of returned page for finding html needed*/
var $newPage= $(responseHtml);
/* find id=results div in new page, insert in id=someDiv*/
$("#someDiv").html( $newPage.find('#results'));
/* if need to run any code on results html, do it here, it exists now in page*/
}).error(function(){
alert('Ooooops');
});
});

Redirect to previous view and inject a javascript alert

I need to do the following:
I have a textbox, which appears in every page of the site, that allows to subscribe to a newsletter. This I've done already and the user is redirected to previous view after subscription.
I'd like to add a javascript alert to the page the user is returned to, something like "Thanks for subscribing". How can this be done?
Thanks in advance.
EDIT: Propably it's not clear from the post tags. I'm using ASP.NET MVC 2 Preview 1
If you are doing a HTTP redirect, then the page you will render needs to be passed some information so that it knows to include the javascript to open the alert box (adding an optional element to the page might be a nicer way to do this).
That information needs to be stored either in a browser cookie, or in a session store (which is keyed from a browser cookie). You can remove this once you've rendered your message, so that it is only shown the first time you visit that page after the redirect.

Categories