Solved

Submitting formmail without page submit

Posted on 2012-03-29
4
444 Views
Last Modified: 2012-03-30
What's the best way to have a form submit on click and send data to an email without the page refreshing?

I don't mind it being php or ajax.
0
Comment
Question by:rivkamak
  • 2
4 Comments
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
the only solution is to use an ajax request to your server's formmail script
0
 

Author Comment

by:rivkamak
Comment Utility
What am I doing wrong?
<script type="text/javascript">
function loadXMLDoc()
{

if (document.form1.name.value == "" || document.form1.phone.value == "" || document.form1.email.value == ""|| document.form1.message.value == ""  ) {
} else { 

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
	    

  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   // document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
   alert("data passed");
	}
  }
  		var url="form.php";
url=url+"?name="+ document.form1.name.value + "&phone=" + document.form1.phone.value + "&email=" +document.form1.email.value + "&message=" +  document.form1.message.value + "&recipients=devorah@cucumber.com&subject=testMessage"  ;

xmlhttp.open("GET", url ,true);
xmlhttp.send();
}
}
</script>

Open in new window

0
 
LVL 1

Accepted Solution

by:
maziqbal earned 500 total points
Comment Utility
Hi rivkamak,

I have pasted below complete working code. Make sure your form and their element have ids as specified in script.

HTML CODE + PHP CODE

HTML CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function loadXMLDoc()
{

if (document.form1.name.value == "" || document.form1.phone.value == "" || document.form1.email.value == ""|| document.form1.message.value == ""  ) {
} else {

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
          

  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
   alert("data passed");
      }
  }
              var url="form.php";
url=url+"?name="+ document.form1.name.value + "&phone=" + document.form1.phone.value + "&email=" +document.form1.email.value + "&message=" +  document.form1.message.value + "&recipients=devorah@cucumber.com&subject=testMessage";

xmlhttp.open("GET", url ,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form id="form1" name="form1">
Name: <input name="name" id="name" type="text" value="name" />
Phone: <input name="phone" id="phone" type="text" value="phone" />
Email: <input name="email" id="email" type="text" value="email" />
Message: <textarea name="message" id="message" cols="5" rows="10">hello</textarea>
<input name="sub" type="button" value="save" onclick="loadXMLDoc();" />
</form>
<div id="myDiv"></div>
</body>
</html>

PHP CODE

<?php
 $str = "<br>Name = ". $_GET['name'];
 $str .= "<br>Email = ".$_GET['email'];
 $str .= "<br>Phone = ".$_GET['phone'];
 $str .= "<br>Message = ".$_GET['message'];
 echo $str;

?>

hope this helps.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
is your problem that the browser does not send a request, or that the server does not receive a request, or that the server does not get the data from the reuest?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now