Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-01-2011, 07:50 PM   PM User | #1
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
POST data problem, not retreiving values

I have this code

index.php
PHP Code:
<script type="text/javascript" src="jquery-1.5.js"></script>
<script type="text/javascript">

function ajaxpost()
{
     $("#myForm").fadeOut(100);
    $("#myForm").hide();
    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) {
              if ( $('#myForm').is(':hidden') ) {
                $("#result").fadeIn();
                } 
            document.getElementById("result").innerHTML=xmlhttp.responseText
        }
      }
var namevalue=encodeURIComponent(document.getElementById("name").value)
var agevalue=encodeURIComponent(document.getElementById("age").value)
var parameters="name="+namevalue+"&age="+agevalue
xmlhttp.open("POST", "basicform.php", true)
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlhttp.send(parameters)
}
</script>
</head>

<body>
<form method="POST" action="" id="myForm">
Your name: <input type="text" id="name" name="name" size="25" /> <br />
Your email: <input type="text" id="email" name="email" size="25" /> <br />
Your age: <input type="text" id="age" name="age" size="25" /> <br />
<textarea id="message" name="message" rows="5" cols="20"></textarea>
<input type="button" value="submit" onClick="ajaxpost()" />
</form>

<div id="result" style="display:none;"> </div>
</body>
</html> 
basicform.php
PHP Code:
<?php

$to 
$_POST['email'];
$subject $_POST['title'];
$message $_POST['message'];
$from $_POST['name'];
$headers "From: ".$from;
mail($to,$subject,$message,$headers);
echo 
"Mail Sent.<br />To: ".$to."<br />Subject: ".$subject."<br />Message: ".$message."<br />From: ".$from."<br />Header: ".$headers."";

?>
When i try it it only get the value for "name". What am i doing wrong? Ive looked all over the code and i cant see an error. Chance are its something stupidly obvious i've missed -.-.

EDIT: Nvm noticed it wasnt in the params var >.<
tomharto is offline   Reply With Quote
Old 06-01-2011, 08:42 PM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
you only submit name and age, and age isn't displayed in the response...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 06-01-2011, 08:55 PM   PM User | #3
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Yeah i forgot to change the params after i edited the form. im new to AJAX if you cant tell :P
tomharto is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:16 PM.


Advertisement
Log in to turn off these ads.