I was wondering how I would precess a form using ajax. All my ajax works and my form works, but my form within the ajax doesn't work.

Here is my contact.html page

<head>
<script type="text/javascript">
function loadXMLDoc(url)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",url,false);
xmlhttp.send(null);
document.getElementById('content').innerHTML=xmlhttp.responseText;
}
</script>
</head>
<body>
<div id="content">
<form action="contact.php" name=”contact_form” method="get">
<input name="your_name" type="text" id="input" class="blue">
<input name="your_email" type="text" id="input" class="blue">
<textarea name="message" id="textarea" class="blue">
</textarea>
<input type="button" value="Submit"  onClick="submit();" />
</form>
</div>
</body>

Now the script to process the form, its a very basic script but it works!:

<?
$subject="from ".$_GET['your_name'];
$headers= "From: ".$_GET['your_email']."\n";
 $headers.='Content-type: text/html; charset=iso-8859-1';
mail("mail@mail.com", $subject,  "
<html>
<head>
 <title>Contact letter</title>
</head>
<body>
<br>
  ".$_GET['message']."
</body>
</html>" , $headers);
echo ("Your message was successfully sent!");
?>

I just want the form to process with reloading the page, I just want the <div id="content"> to reload and process the script, then redirect them back to the contact page.

If that is possible thanks!!

Recommended Answers

All 5 Replies

your_name
and
your_email

textbox values will not be posted to your processing, for that you need to call the same action page and pass the variable values.

To pass the variable values to your processing form, you need to collect the textbox values (your_name and your_email) using javascript and send them through url as params to the ajax call

Hope it helps you

alright so a change of plans. I was going to use ajax but I dont have time to figure out how to set that up.

How about using my current php but at the end of the script have a url redirect that redirects the user back to the home page then send them an alert('message sent'). How would I code that into my current code?

I have never done a redirect so I am completely lost on where to start.

<?
$subject="from ".$_GET['your_name'];
$headers= "From: ".$_GET['your_email']."\n";
 $headers.='Content-type: text/html; charset=iso-8859-1';
mail("email", $subject,  "
<html>
<head>
 <title>Contact letter</title>
</head>
<body>

<br>
  ".$_GET['message']."
</body>
</html>" , $headers);
echo ("Your message was successfully sent!");
?>

Thanks!!

I figured it out!! I put this after the closing PHP tag.

<script type="text/javascript">
   /* <![CDATA[ */
      function init() {
         window.location = 'URL';
      }
      window.onload = init;
   /* ]]> */
</script>

now how would I add an alert in there?

<script type="text/javascript">
/* <![CDATA[ */
function init() {
window.location = 'URL';
}
window.onload = init;
alert('message sent!');
/* ]]> */
</script>

ok soo I guess I solved my own problem Thanks!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.