Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    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 >.<

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,350
    Thanks
    11
    Thanked 589 Times in 570 Posts
    you only submit name and age, and age isn't displayed in the response...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Yeah i forgot to change the params after i edited the form. im new to AJAX if you cant tell :P


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •