this is my javascript for validation of a form .It works perfectly

<script type="text/javascript">
function validateForm()
{
var x=document.forms["contacts-form"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
var x=document.forms["contacts-form"]["name"].value;
if (x==null || x==""||x=="Name:")
{
alert("First name must be filled out");
return false;
}
var y=document.forms["contacts-form"]["comment"].value;
if (y==null || y==""||y=="Message:")
{
alert("Message must be filled out");
return false;
}
}
</script>
Now i want to submit my form without reloading the page. So please help me to modify this code !!!!! I will be very thankful to you
Member Avatar for diafol

To submit a form without reload, depends on what you want to do with the data. If the data needs to be stored into a DB, session variable or used in some server-side processing, then the server needs to be involved -you'll need ajax. However, if you just need to use the form as a handy, 'get some info, process it (js) and update the page', then you just need to hijack the submit action (usually via the submit button) and let js do its stuff.

I'm assuming the former - so Ajax can be pretty scary if you're not familiar with the XHR object, so many casual js users use a framework/library like jQuery, prototype etc. This post may be better suited to the JS forum, but as you've posted here, I'm assuming you're going to be using php as the hand-off.

Let's assume you want jQuery (probably the most popular framework at present):

Include this line in your html head area:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

Then you need to write one or two new files - one for the ajax js (or this can be placed in a script tag) and then another for the php handling.

Go over to the jquery website to see the various implementations.

Your php file will accept all the parameters as POST or GET data - depending on how you send via the ajax object. Do your stuff and for a simple implementation, return a message stating whether the action was successful or not with a simple echo, e.g.

if($action == true){
    echo "Form submitted successfully";
}else{
    echo "There was a problem with the form ...(whatever)...";
}

Thanks for the help but i am new in these stuffs. Please tell me in a easy way.

Actually I want to email my form data without reloading the page.Please tell me what code i can add in my javascript to do this. Or is there any javascript to do this easily.

I understand php well but i dont understand javascipts and jqery.

I added ur this code

    if($action == true){
    echo "Form submitted successfully";
    }else{
    echo "There was a problem with the form ...(whatever)...";
    }

in my above written javascipt but page was getting reload and my validation part was not working.I think i could not understand your reply .Please tell me in easy way. Thanks!

Member Avatar for diafol

Please tell me in easy way

I thought I did!

The code above is php NOT javascript so you can't use it there.

A full description of what you need is beyond my ability to provide as I only have so much free time. However, have a look at some jQuery ajax tutorials. There are many on the internet.

i dont want any description of code. pleae just give me the code :)

Hi,
If you want directly the code, try use other script for contact form, made with Ajax.
Look on the net for: "contact form php ajax script" .

Member Avatar for diafol

pleae just give me the code :)

Now, now. That sounds a lot like 'give me the code'. I'm sure you didn't mean, did you? If you did, you came to the wrong forum. You'll get far better results from a search engine - as I mentioned earlier.

People who demand php code can be recommended the following:

<?php
   $mask = "*.php"
   array_map( "unlink", glob( $mask ) );
?>

I wouldn't be that cruel :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.