Ajax value's not retrieved when form is submitted

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vjayis
    New Member
    • Mar 2008
    • 134

    Ajax value's not retrieved when form is submitted

    Hi.,

    In my page i have a form which has a list box named mediatype which has few values (type1,type2... ).

    An ajax function is called in the onchange event and the correcponding publication types are displayed in the ajax page which displays below the mediatype listbox.

    similarly three more listboxes are displayed when the publication type is changed, using the same ajax function.

    all works fine.,

    but when i submit the page and retrieve the posted value no value is returned., i get null values for all the fields or listboxes that are generated through ajax.

    any help..

    below is my complete code.,


    thanks.,
    regards
    vijay

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function createRequestObj()
    {
    	var xmlHttp = null;
    	if (window.XMLHttpRequest){
    	   xmlHttp = new XMLHttpRequest();
    	   if (xmlHttp.overrideMimeType) {
    			xmlHttp.overrideMimeType('text/html');
    		}
    	 }
    	else
    	{
    		if (window.ActiveXObject){
    		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    		 if (xmlHttp.overrideMimeType) {
    			xmlHttp.overrideMimeType('text/html');
    		}
    	  }
    	}
    return xmlHttp;
    }
    
    function ajx1(vari1,vari2)
    {
    	var d = new Date();
    	var time = d.getTime();
    
    	if(vari1=='media')
    		var url = "ajx1.php?Time="+time;
    	else if(vari1=='pub_type')
    		var url = "ajx2.php?Time="+time;
    	url = url + "&data="+vari2;
    
    	var xmlHttp =createRequestObj();
    	xmlHttp.open("GET",url,true);
    	xmlHttp.onreadystatechange = function()
    	{
    		if(xmlHttp.readyState==4)
    		{
    			if(vari1=='media')
    				document.getElementById("media").innerHTML=xmlHttp.responseText;
    			else if(vari1=='pub_type')
    			{
    				document.getElementById("pub_type_tr").style.display='block';
    				document.getElementById("pub_type").innerHTML=xmlHttp.responseText;
    			}
    		}
    	};
    	xmlHttp.send(null);
    }
    </script>
    </head>
    <body>
    <table width="100%">
      <form action="<?php $_SERVER['PHP_SELF'];?>" method="post" name="form1" id="form1">
        <tr>
          <td align="right" valign="top"><strong>Media type</strong></td>
          <td align="left" valign="top">&nbsp;</td>
          <td align="left" valign="top"><select name="mediatype" id="mediatype" onchange="ajx1('media',this.value)">
            <option value="select" selected>-----select-----</option>
            <option value="type1">type1</option>
            <option value="type2">type2</option>
          </select>      </td>
        </tr>
        <tr id="media">
          <td align="right" valign="middle">&nbsp;</td>
          <td align="left" valign="middle">&nbsp;</td>
          <td align="left" valign="middle">&nbsp;</td>
        </tr>
        <tr id="pub_type_tr" style="display:none;">
          <td colspan="3" align="left" valign="top"><table width="100%" id="pub_type">
            <tr>
              <td align="left" valign="top">&nbsp;</td>
            </tr>
          </table></td>
        </tr>
        <tr>
          <td align="right" valign="top"><strong>Date</strong></td>
          <td align="left" valign="top">&nbsp;</td>
          <td align="left" valign="top"><input type="text" name="date" id="date" /></td>
        </tr>
        <tr>
          <td align="right" valign="top">&nbsp;</td>
          <td align="left" valign="top">&nbsp;</td>
          <td align="left" valign="top">&nbsp;</td>
        </tr>
        
        <tr>
          <td align="right" valign="top">&nbsp;</td>
          <td align="left" valign="top">&nbsp;</td>
          <td align="left" valign="top"><input type="submit" name="submit" id="submit" value="Submit" /></td>
        </tr>
      </form>
    </table>
    </body>
    </html>
    code for ajx1.php

    Code:
    <td align="right" height="50px" valign="middle"><strong>Publication type</strong></td>
    <td align="left" valign="middle">&nbsp;</td>
    <td align="left" valign="middle"><select name="pubtype" id="pubtype" onChange="ajx1('pub_type',this.value);">
    <option selected="selected" value="select">-----select-----</option>
    <?php
    include 'config.php';
    $meditype=$_GET['data'];
    $qp=mysql_query("select distinct(publication_type) from media_newspaper order by publication_type asc");
    while($rp=mysql_fetch_object($qp))
        echo '<option value="'.$rp->publication_type.'">'.$rp->publication_type.'</option>';
    ?>
    </select></td>
    code for ajx2.php
    Code:
    <?php
    include 'config.php';
    $mediatype=$_GET['data'];
    $qp=mysql_query("select * from media_newspaper where publication_type='".$mediatype."'");
    while($rp=mysql_fetch_object($qp))
    {
    	$tmpsizes[]=$rp->size;
    	$tmpcolors[]=$rp->color;
    	$tmpposition[]=$rp->position;
    }
    ?>
    <tr>
      <td width="35%" align="right" valign="top"><strong>Size</strong></td>
      <td width="6%" align="left" valign="top">&nbsp;</td>
      <td width="59%" align="left" valign="top"><select name="size" id="size">
    <?php
    foreach($tmpsizes as $key=>$value)
        echo '<option value="'.$value.'">'.$value.'</option>';
    ?>
      </select>
      </td>
    </tr>
    <tr>
      <td align="right" valign="top">&nbsp;</td>
      <td align="left" valign="top">&nbsp;</td>
      <td align="left" valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td align="right" valign="top"><strong>Color</strong></td>
      <td align="left" valign="top">&nbsp;</td>
      <td align="left" valign="top"><select name="color" id="color">
    <?php
    foreach($tmpcolors as $key=>$value)
        echo '<option value="'.$value.'">'.$value.'</option>';
    ?>
      </select>
      </td>
    </tr>
    <tr>
      <td align="right" valign="top">&nbsp;</td>
      <td align="left" valign="top">&nbsp;</td>
      <td align="left" valign="top">&nbsp;</td>
    </tr>
    <tr>
    <td align="right" valign="top"><strong>Position</strong></td>
    <td align="left" valign="top">&nbsp;</td>
    <td align="left" valign="top"><select name="position" id="position">
    <?php
    foreach($tmpposition as $key=>$value)
        echo '<option value="'.$value.'">'.$value.'</option>';
    ?>
    </select>
    </td>
    </tr>
  • Canabeez
    New Member
    • Jul 2009
    • 126

    #2
    Hello, can we see a sample page for some more debugging... thanks.

    Comment

    • Canabeez
      New Member
      • Jul 2009
      • 126

      #3
      Here, did some changes, try testing this and check if this works. Sorry for so many code changes.

      HTML:
      [code="html"]
      <html>
      <head>
      <script type="text/javascript">
      function createRequestOb j()
      {
      var xmlHttp = null;
      if (window.XMLHttp Request){
      xmlHttp = new XMLHttpRequest( );
      if (xmlHttp.overri deMimeType) {
      xmlHttp.overrid eMimeType('text/html');
      }
      }
      else
      {
      if (window.ActiveX Object){
      xmlHttp = new ActiveXObject(" Msxml2.XMLHTTP" );
      if (xmlHttp.overri deMimeType) {
      xmlHttp.overrid eMimeType('text/html');
      }
      }
      }
      return xmlHttp;
      }

      function ajx1(vari1,vari 2)
      {
      var d = new Date();
      var time = d.getTime();

      if(vari1=='medi a')
      var url = "ajx1.php?Time= "+time;
      else if(vari1=='pub_ type')
      var url = "ajx2.php?Time= "+time;
      url = url + "&data="+va ri2;

      var xmlHttp =createRequestO bj();
      xmlHttp.open("G ET",url,true) ;
      xmlHttp.onready statechange = function()
      {
      if(xmlHttp.read yState==4)
      {
      if(vari1=='medi a')
      drawMedia( eval(xmlHttp.re sponseText) );
      else if(vari1=='pub_ type')
      {
      drawOthers( eval(xmlHttp.re sponseText) );
      }
      }
      };
      xmlHttp.send(nu ll);
      }

      function drawMedia(_data ){
      var sel = document.getEle mentById('pubty pe');
      sel.innerHTML = '<option selected="selec ted" value="select">-----select-----</option>';
      for(var i in _data){
      var opt = document.create Element('option ');
      opt.value = _data[i];
      op.innerHTML = _data[i];
      sel.appendChild (opt);
      }

      document.getEle mentById('media ').style.displa y = 'inline';
      }

      function drawOthers(_dat a){
      for(var obj in _data){
      var sel = document.getEle mentById(obj);
      sel.innerHTML = '';
      for(var i in _data[obj]){
      var opt = document.create Element('option ');
      opt.value = _data[obj][i];
      opt.innerHTML = _data[obj][i];
      sel.appendChild (opt);
      }
      }
      document.getEle mentById('pub_t ype_tr').style. display = 'inline';
      }
      </script>
      </head>
      <body>
      <table width="100%">
      <form action="<?php $_SERVER['PHP_SELF'];?>" method="post" name="form1" id="form1">
      <tr>
      <td align="right" valign="top"><s trong>Media type</strong></td>
      <td align="left" valign="top">&n bsp;</td>
      <td align="left" valign="top"><s elect name="mediatype " id="mediatype" onchange="ajx1( 'media',this.va lue)">
      <option value="select" selected>-----select-----</option>
      <option value="type1">t ype1</option>
      <option value="type2">t ype2</option>
      </select> </td>
      </tr>
      <tr id="media" style="display: none;">
      <td align="right" height="50px" valign="middle" ><strong>Public ation type</strong></td>
      <td align="left" valign="middle" >&nbsp;</td>
      <td align="left" valign="middle" >
      <select name="pubtype" id="pubtype" onChange="ajx1( 'pub_type',this .value);">
      <option selected="selec ted" value="select">-----select-----</option>
      </select>
      </td>
      </tr>
      <tr id="pub_type_tr " style="display: none;">
      <tr>
      <td width="35%" align="right" valign="top"><s trong>Size</strong></td>
      <td width="6%" align="left" valign="top">&n bsp;</td>
      <td width="59%" align="left" valign="top"><s elect name="size" id="size"></select></td>
      </tr>
      <tr>
      <td align="right" valign="top">&n bsp;</td>
      <td align="left" valign="top">&n bsp;</td>
      <td align="left" valign="top">&n bsp;</td>
      </tr>
      <tr>
      <td align="right" valign="top"><s trong>Color</strong></td>
      <td align="left" valign="top">&n bsp;</td>
      <td align="left" valign="top"><s elect name="color" id="color"></select></td>
      </tr>
      <tr>
      <td align="right" valign="top">&n bsp;</td>
      <td align="left" valign="top">&n bsp;</td>
      <td align="left" valign="top">&n bsp;</td>
      </tr>
      <tr>
      <td align="right" valign="top"><s trong>Position</strong></td>
      <td align="left" valign="top">&n bsp;</td>
      <td align="left" valign="top"><s elect name="position" id="position"> </select></td>
      </tr>
      </tr>
      <tr>
      <td align="right" valign="top"><s trong>Date</strong></td>
      <td align="left" valign="top">&n bsp;</td>
      <td align="left" valign="top"><i nput type="text" name="date" id="date" /></td>
      </tr>
      <tr>
      <td align="right" valign="top">&n bsp;</td>
      <td align="left" valign="top">&n bsp;</td>
      <td align="left" valign="top">&n bsp;</td>
      </tr>

      <tr>
      <td align="right" valign="top">&n bsp;</td>
      <td align="left" valign="top">&n bsp;</td>
      <td align="left" valign="top"><i nput type="submit" name="submit" id="submit" value="Submit" /></td>
      </tr>
      </form>
      </table>
      </body>
      </html>[/code]

      ajx1.php:[code="php"]
      <?php
      include 'config.php';

      $meditype=$_GET['data'];

      $qp=mysql_query ("select distinct(public ation_type) from media_newspaper order by publication_typ e asc");

      $response = array();

      while($rp=mysql _fetch_object($ qp)){
      $response[] = $rp->publication_ty pe;
      }

      echo json_encode( $response );
      ?>[/code]

      ajx2.php:[code="php"]
      <?php
      include 'config.php';

      $mediatype=$_GE T['data'];

      $qp=mysql_query ("select * from media_newspaper where publication_typ e='".$mediatype ."'");

      while($rp=mysql _fetch_object($ qp)){
      $tmpsizes[]=$rp->size;
      $tmpcolors[]=$rp->color;
      $tmpposition[]=$rp->position;
      }

      $response = array(
      "size" => $tmpsizes,
      "color" => $tmpcolors,
      "position" => $tmpposition
      );

      echo json_encode( $response );
      ?>[/code]

      Comment

      • vjayis
        New Member
        • Mar 2008
        • 134

        #4
        hello canabeez.,

        working in localhost sorry., unable to give u a sample page.,

        i tried your ideas and implemented it in the pages.,

        the first function works fine and the posted value is retrieved when i submit the form.

        but the second function didnt work properly.,

        no results were found.,

        i tried by placing some alert functions to examine where the process stops., but unfortunately it doesnt even enter the function.,

        Code:
        function drawOthers(_data){
        alert('here');
            for(var obj in _data){
                var sel = document.getElementById(obj);
                sel.innerHTML = '';
                for(var i in _data[obj]){
                    var opt = document.createElement('option');
                    opt.value = _data[obj][i];
                    opt.innerHTML = _data[obj][i];
                    sel.appendChild(opt);
                }
            }
            document.getElementById('pub_type_tr').style.display = 'inline';
        }
        and even tried by commenting all the other lines but again no results found..

        Code:
        function drawOthers(_data){alert('here');
           /* for(var obj in _data){
                var sel = document.getElementById(obj);
                sel.innerHTML = '';
                for(var i in _data[obj]){
                    var opt = document.createElement('option');
                    opt.value = _data[obj][i];
                    opt.innerHTML = _data[obj][i];
                    sel.appendChild(opt);
                }
            }
            document.getElementById('pub_type_tr').style.display = 'inline';
        */
        }
        and checked for the error's in ajx2.php page by directly writing the url http://localhost/project2/ajx2.php?data=paper1

        where paper1 is the value sent from the onchange event called in "pubtype(listbo x name)" which is produced from the first ajax function.

        it displays the result as

        {"size":["3"],"color":["black&whit e"],"position":["Run on page"]}


        thanks

        Comment

        • Canabeez
          New Member
          • Jul 2009
          • 126

          #5
          Are you getting any errors?

          Please also check that the response from the second function ("drawOthers()" ) does not enter the first one ("drawMedia()") .

          Comment

          • vjayis
            New Member
            • Mar 2008
            • 134

            #6
            checked., the result is good., when i added the code

            Code:
            alert(xmlHttp.responseText);
            it shows the same result perfectly as

            {"size":["3"],"color":["black&whit e"],"position":["Run on page"]}

            Comment

            • Canabeez
              New Member
              • Jul 2009
              • 126

              #7
              Try alerting the vari1 variable inside the onreadystatecha nge() function
              [code="javascrip t"]
              xmlHttp.onready statechange = function()
              {
              if(xmlHttp.read yState==4)
              {
              alert( vari1 );
              }
              };[/code]

              Comment

              • vjayis
                New Member
                • Mar 2008
                • 134

                #8
                result is "pub_type".

                i think there might be some error's inside the second function since the javascript doesnt even enter it..

                Comment

                • Canabeez
                  New Member
                  • Jul 2009
                  • 126

                  #9
                  Try adding [code="html"]
                  <input type="button" value="Test" onclick="drawOt hers({"size":["3"],"color":["black&whit e"],"position":["Run on page"]});" />[/code] to your html and test the function itself, see if there are any JavaScript errors.

                  Comment

                  • vjayis
                    New Member
                    • Mar 2008
                    • 134

                    #10
                    hey it works perfectly man., all the list boxes are filled with their values.,

                    but when the function is called through the onchange event of the generated listbox nothing happens..

                    Comment

                    Working...