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 12 of 12
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Ajax Post Issue !

    Hi
    its not posting var to php have a look and let me know why ?

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
    http-equiv="content-type">
      <title></title>
    </head>
    <body>
    <script type="text/javascript">

    function changename() {
    var ajaxobject;
    if (window.XMLHttpRequest){
      ajaxobject=new XMLHttpRequest();
      }

    else if (window.ActiveXObject){
      ajaxobject=new ActiveXObject("Microsoft.XMLHTTP");
    } else {
      alert("Your browser does not support Ajax!");
      }

    ajaxobject.onreadystatechange=function() {
    if(ajaxobject.readyState==4  && ajaxobject.status == 200) {

    var ajaxdisplay = document.getElementById('ajaxDiv') ;
    var ajaxtextarea = document.getElementById('ajaxtext') ;
    ajaxtextarea.innerHTML  =  ajaxdisplay.innerHTML ;


      }
    }
    ajaxobject.open("POST","ajax.php",true);
    ajaxobject.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    ajaxobject.send("ajax="+document.getElementById("ajaxDiv").innerHTML);

    }

    </script>

    <div id="ajaxDiv" >My Div Text</div> <br>
    <textarea id="ajaxtext"  ></textarea>
    <input name="Submit" id="Submit" value="Submit" onclick="changename()" type="button">
    </body>

    </html>


    <?php

    if(isset($_POST['ajax'])) {
        
    $text $_POST['ajax'];
        echo 
    "  <br>{$text}";

    }
    ?>
    Last edited by simzam; 12-26-2010 at 01:07 PM.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,302
    Thanks
    13
    Thanked 345 Times in 341 Posts
    is that one file or two files?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    1. You haven't used responseText to allow the Javascript to display the callback text
    2. if that is one file all the html above the PHP code will also be sent in the callback.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #4
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    yes all in 1 file how to make it work where to put responseText

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Try this out.

    PHP Code:
    <?php
    if(isset($_POST['ajax'])) {
        
    $text htmlentities(stripslashes($_POST['ajax']));
        print 
    " <br>{$text}";
    } else {
    print <<<EOD
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
    http-equiv="content-type">
      <title>Untitled Document</title>
    <script type="text/javascript">
    function changename() {
      var ajaxobject,
          ajaxdisplay = document.getElementById('ajaxDiv'),
          ajaxtextarea = document.getElementById('ajaxtext');
      if(window.XMLHttpRequest) {
        ajaxobject = new XMLHttpRequest();
      } else if(window.ActiveXObject) {
        ajaxobject = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        alert("Your browser does not support Ajax!");
      }
      ajaxobject.onreadystatechange=function() {
        if(ajaxobject.readyState == 4 && ajaxobject.status == 200) {
          ajaxtextarea.innerHTML = ajaxobject.responseText;
        }
      }
      ajaxobject.open("POST","ajax.php",true);
      ajaxobject.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      ajaxobject.send("ajax="+ajaxdisplay.innerHTML);
    }
    </script>
    </head>
    <body>
    <div id="ajaxDiv">My Div Text</div><br>
    <textarea id="ajaxtext"></textarea>
    <input name="Submit" id="Submit" value="Submit" onclick="changename();" type="button">
    </body>
    </html>
    EOD;
    }
    ?>
    Last edited by DJCMBear; 12-26-2010 at 06:18 PM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #6
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    its working ! thanks
    can we use
    window.onload = changename();
    to call function ty !

    i tried this to call my Ajax script but it wouldn't work out

  • #7
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    You can not use this as it will run the function before the onload
    Code:
    window.onload = changename();
    you have to use this
    Code:
    window.onload = changename;
    or this
    Code:
    window.onload = function(){changename();};
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #8
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    can i get
    ajatext as variable
    like
    myvar = 'ajatext' ;
    this use to get id how to use get in variable thanks
    res = http.responseText;
    document.getElementById('ajaxtext').innerHTML = res;

  • #9
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    If I understand correctly you want the id name to be stored in a var and then use it in the document.getElementById function.

    Code:
    var myvar = 'ajaxtext',
        res   = http.responseText;
    document.getElementById(myvar).innerHTML = res;
    
    // OR you can just do this
    var myvar = document.getElementById('ajaxtext'),
        res   = http.responseText;
    myvar.innerHTML = res;
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #10
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    and in php can i get this

    myphpvar = myvar;
    i want to document.getElementById content to php var

  • #11
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Oh you want to send the word 'ajaxtext' over to the PHP code that's why you are setting it as a variable ok just do this.

    Code:
    http.send('myvar='+myvar);
    And in PHP you would use this.
    PHP Code:
    $myphpvar $_POST['myvar']; 
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #12
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    when ever i typed somthing and submit it didnt post anything what is wrong with my ajax script do i need to change getElementById to some form.element ?


    PHP Code:
    <?php
    print <<<EOD
    <html>
    <head>
      <title> Insert !</title>
    </head>
    <body>
    EOD;


    if ((isset(
    $_GET['myajax'])) &&($_GET['myajax']=="true")){

    print <<<EOD
    <script type="text/javascript">
    function doHttpRequest() {
      http.open("POST", "ajaxpost.php", true);
      http.onreadystatechange = getHttpRes;
      var params = "ajax1="+ encodeURI(document.getElementById("ajaxtext").value);
      http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http.setRequestHeader("Content-length", params.length);
      http.setRequestHeader("Connection", "close");
      http.send(params);
    }

    function getHttpRes(){
      if (http.readyState == 4 && http.status == 200) {
        var myvar = document.getElementById('mydiv'),
        res = http.responseText;
                 myvar.innerHTML = res;
      }
    }

    function getXHTTP( ) {
      var xhttp;
       try {
          xhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e2) {
            try {
              xhttp = new XMLHttpRequest();
            } catch (e3) {
              xhttp = false;
            }
          }
        }
      return xhttp;
    }
    var http = getXHTTP();
    window.onload = doHttpRequest;
    </script>
    <div id='mydiv'></div><br>
    EOD;
    }

    if ((isset(
    $_GET['myajaxform'])) &&($_GET['myajaxform']=="true")){

    print <<<EOD

    <script type="text/javascript">
    window.onload = submitform;
    function submitform()
    {
        document.forms["myform"].submit();
    }
    </script>
    EOD;
    }

    print <<<EOD

    <form id="myform" action="?myajax=true"  method="post" >
    <textarea rows='3'  cols='40' id='ajaxtext' name ='ajaxtext'></textarea><br>
    </form>
    <br> <a href = "?myajaxform=true" >Submit </a>



    </body>
    </html>
    EOD;

    ?>


  •  

    Posting Permissions

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