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
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ajax POST Method Problems

    Hi there,

    I'm new to ajax so i could have this all wrong.

    I'm trying to pass a js var to a php var using the ajax post method.

    My Code:

    [code]

    <html>
    <head>

    <?php

    if (!isset($_POST['height'])) {

    ?>


    <script type="text/javascript">
    <!--
    onload=function() {

    var divh = document.getElementById('top').offsetHeight;

    var http = new XMLHttpRequest();

    var url = "indexajax.php";
    var params = "height=divh";
    http.open("POST", url, true);

    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");

    http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
    alert(http.responseText);
    }
    }
    http.send(params);

    }
    //-->
    </script>

    <?php
    }
    else if (isset($_POST['height'])) {
    $divheight = $_POST['height'];
    }
    ?>

    </head>
    <body>

    <div id="top" class="top" >
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent blandit venenatis purus.
    Integer massa libero, vehicula id, consequat sed, tincidunt nec, purus. Class aptent taciti
    sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Suspendisse potenti.
    Nunc vulputate magna non magna. Aenean lorem eros, adipiscing quis, semper non, dictum a,
    nunc. Curabitur ut sem. Pellentesque a est id neque hendrerit ultrices. Donec vulputate tincidunt
    turpis. Curabitur dignissim vestibulum nunc. Aliquam felis lorem, ultrices sit amet, convallis a,
    accumsan vel, ante. Proin aliquam turpis sed augue. In pellentesque, magna a pulvinar adipiscing,
    est orci adipiscing felis, sed laoreet urna magna quis neque. Proin facilisis aliquet urna.
    </div>

    <?php


    echo "The above Div is " . $divheight . "px high";

    ?>

    </body>
    </html>

    [code]

    It is the ajax i'm having problems with. I get the alert message containing all the page contents. What i want is for the div height to be passed through the POST var so i can access it with PHP.

    Thanks in advance for any help.

  • #2
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    [code]

    <html>
    <head>
    <script type="text/javascript">

    function getHTTPObject() {
    var xhr = false;
    if(window.XMLHttpRequest) {
    var xhr = new XMLHttpRequest();
    } else if(window.ActiveXObject) {
    try {
    var xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
    try {
    var xhr = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
    xhr = false;
    }
    }
    }
    return xhr;
    }

    onload=function{

    var divh = document.getElementById('top').offsetHeight;

    var request = getHTTPObject();
    if (request) {
    request.onreadystatechange = doThis;
    request.open("POST", "index.php", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.send("height=divh");
    }
    }
    </script>
    </head>
    <body>
    <div id="top">
    <p>Vivamus egestas felis at purus semper in hendrerit ligula egestas.
    Mauris tempor porttitor adipiscing. Quisque lobortis gravida augue
    vel semper.
    Nullam accumsan mauris at libero porttitor congue. Fusce sed odio sit
    amet est congue interdum.
    Cras blandit pretium tellus, in cursus mauris consequat quis.
    Aliquam magna ante, fringilla et venenatis eu, dapibus non tortor.
    Duis quis sagittis est. Sed vel sem sapien.
    In laoreet, metus ornare dapibus gravida, tortor turpis ultricies nunc,
    eget euismod felis nisi volutpat nulla.
    Praesent placerat feugiat vulputate. Sed felis diam, faucibus nec
    blandit eget, volutpat ut augue.
    Praesent rutrum viverra orci, et lacinia mi auctor eget.
    Donec luctus, lorem vitae sollicitudin adipiscing, metus sapien suscipit
    ipsum, a ornare erat sapien id diam.
    Fusce a sapien vel enim egestas dapibus eu vitae dui. Maecenas
    vulputate dui at augue ullamcorper blandit.
    Ut vitae malesuada arcu. Sed eget risus aliquam felis malesuada
    venenatis vel volutpat lectus.
    </p>
    <?php $oldheight = $_POST['height'];
    echo "This Div is " . $oldheight . "px high.";
    ?>

    </div>
    </body>
    </html>


    [code]

    Still nothing.......

  • #3
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post
    request.send("height=divh"); ... here divh is a variable... so please have it as request.send("height=" + divh);
    that should send the height to the php... the ajax call looks fine barring this...


  •  

    Posting Permissions

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