Solved

Submitting formmail without page submit

Posted on 2012-03-29
4
483 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 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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