SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    ajax to pass variables to php?

    how can I make ajax pass variables to php, I been trying to use $POST but itīs not working for me, I have this script

    Code JavaScript:
    function get_school_data()
    {
    var xmlhttp;
    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)
        {
        document.getElementById("test").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("POST","select2.php",true);
    xmlhttp.send();
    }

    Then I have this select field

    Code HTML4Strict:
    <select id="country" name="country" onchange="get_school_data()">
    <?php
    mysql_select_db("coatlcalli", $con);
     
    $ccode = mysql_query("SELECT * FROM country");
    //$rs = mysql_query("SELECT enname FROM country");
     
    while($row = mysql_fetch_array($ccode))
    {
      echo "<option value=\"".$row['ccode']."\">".$row['enname']."\n ";
    }
    ?>
    </select>
    <p id="test"></p>

    and the select2.php

    Code PHP:
    <?php
    $ccode=$_POST['country'];
    ?>

    What I am trying to achieve is to send the value of country to php and have it stored in a variable so I can then select a different table in the database, but when I do it the way I am, it does not work, as it gives me an undefined variable

    Any help is apreciated

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Googling for "ajax post variables" gave me this example as first result: Using POST method in XMLHTTPRequest(Ajax)


Bookmarks

Posting Permissions

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