Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Submitting formmail without page submit

Posted on 2012-03-29
4
Medium Priority
?
493 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

609 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