tags:

views:

31

answers:

2

hi, I am looking for a way to get a response in a form of a javascript alert after a form has been submitted using a php script. I guess ajax should do this but Im not an Ajax guy yet. A simple sample code would help a lot. Thanks for reading

+2  A: 

In your PHP code after successfully saving/processing data, write/echo the following inside <body> tag. This will show an alert when rendered on client's browser.

<script language="javascript" type="text/javascript" > alert('This is what an alert message looks like.'); </script>

Ankit Jain
+1  A: 

If you want to venture into ajax and jquery - grab a copy of the jquery core and then do something like the following:

(Now with a full example. You will also need jquery.form.js plug in)

<html>
<body>
<script type="text/Javascript" src="jquery-1.2.4.min.js"></script>
<script type="text/Javascript" src="jquery.form.js"></script>
<script type="text/Javascript">
$(document).ready(function(){
    $("#SUBMIT_BUTTON").click(function()
    {   
        var options = {
        url:        'processForm.php', 
        success:    function(){
            alert('success');   
        },
        error:    function() { 
            alert('failure');
        }}; 

        $('#MYFORM').ajaxSubmit(options);
        return false;
    }
)});
</script>

<form id="MYFORM" method="post">
    <input type="text" name="testing">
    <input type="button" value="click me" id="SUBMIT_BUTTON">
</form>
</body>
</html>
PIM
Well, this is exactly something like this Ive been looking for. Beside, I can't figure out what's wrong, I couldn't get it work. I guess I need more explanation concerning the action of the form, should be left blank?
Selom
I tried something simpler and different, I guess will come back to the ajax the next time.Thanks
Selom
Selom -- I posted a full version of the code, see if that helps you out any.
PIM
Selom - you need to download the jquery scripts (and use the correct path/to/jquery.js in your script includes...
Crayon Violent
it worked just like a charm lol. thanks a lot.
Selom