Solved

Submitting formmail without page submit

Posted on 2012-03-29
4
463 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
ID: 37783426
the only solution is to use an ajax request to your server's formmail script
0
 

Author Comment

by:rivkamak
ID: 37785423
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
ID: 37785927
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
ID: 37785938
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 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