?
Solved

Submitting formmail without page submit

Posted on 2012-03-29
4
Medium Priority
?
488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

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