473,465 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Ajax value's not retrieved when form is submitted

134 New Member
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head>
  4. <script type="text/javascript">
  5. function createRequestObj()
  6. {
  7.     var xmlHttp = null;
  8.     if (window.XMLHttpRequest){
  9.        xmlHttp = new XMLHttpRequest();
  10.        if (xmlHttp.overrideMimeType) {
  11.             xmlHttp.overrideMimeType('text/html');
  12.         }
  13.      }
  14.     else
  15.     {
  16.         if (window.ActiveXObject){
  17.         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  18.          if (xmlHttp.overrideMimeType) {
  19.             xmlHttp.overrideMimeType('text/html');
  20.         }
  21.       }
  22.     }
  23. return xmlHttp;
  24. }
  25.  
  26. function ajx1(vari1,vari2)
  27. {
  28.     var d = new Date();
  29.     var time = d.getTime();
  30.  
  31.     if(vari1=='media')
  32.         var url = "ajx1.php?Time="+time;
  33.     else if(vari1=='pub_type')
  34.         var url = "ajx2.php?Time="+time;
  35.     url = url + "&data="+vari2;
  36.  
  37.     var xmlHttp =createRequestObj();
  38.     xmlHttp.open("GET",url,true);
  39.     xmlHttp.onreadystatechange = function()
  40.     {
  41.         if(xmlHttp.readyState==4)
  42.         {
  43.             if(vari1=='media')
  44.                 document.getElementById("media").innerHTML=xmlHttp.responseText;
  45.             else if(vari1=='pub_type')
  46.             {
  47.                 document.getElementById("pub_type_tr").style.display='block';
  48.                 document.getElementById("pub_type").innerHTML=xmlHttp.responseText;
  49.             }
  50.         }
  51.     };
  52.     xmlHttp.send(null);
  53. }
  54. </script>
  55. </head>
  56. <body>
  57. <table width="100%">
  58.   <form action="<?php $_SERVER['PHP_SELF'];?>" method="post" name="form1" id="form1">
  59.     <tr>
  60.       <td align="right" valign="top"><strong>Media type</strong></td>
  61.       <td align="left" valign="top">&nbsp;</td>
  62.       <td align="left" valign="top"><select name="mediatype" id="mediatype" onchange="ajx1('media',this.value)">
  63.         <option value="select" selected>-----select-----</option>
  64.         <option value="type1">type1</option>
  65.         <option value="type2">type2</option>
  66.       </select>      </td>
  67.     </tr>
  68.     <tr id="media">
  69.       <td align="right" valign="middle">&nbsp;</td>
  70.       <td align="left" valign="middle">&nbsp;</td>
  71.       <td align="left" valign="middle">&nbsp;</td>
  72.     </tr>
  73.     <tr id="pub_type_tr" style="display:none;">
  74.       <td colspan="3" align="left" valign="top"><table width="100%" id="pub_type">
  75.         <tr>
  76.           <td align="left" valign="top">&nbsp;</td>
  77.         </tr>
  78.       </table></td>
  79.     </tr>
  80.     <tr>
  81.       <td align="right" valign="top"><strong>Date</strong></td>
  82.       <td align="left" valign="top">&nbsp;</td>
  83.       <td align="left" valign="top"><input type="text" name="date" id="date" /></td>
  84.     </tr>
  85.     <tr>
  86.       <td align="right" valign="top">&nbsp;</td>
  87.       <td align="left" valign="top">&nbsp;</td>
  88.       <td align="left" valign="top">&nbsp;</td>
  89.     </tr>
  90.  
  91.     <tr>
  92.       <td align="right" valign="top">&nbsp;</td>
  93.       <td align="left" valign="top">&nbsp;</td>
  94.       <td align="left" valign="top"><input type="submit" name="submit" id="submit" value="Submit" /></td>
  95.     </tr>
  96.   </form>
  97. </table>
  98. </body>
  99. </html>
  100.  
code for ajx1.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. <td align="right" height="50px" valign="middle"><strong>Publication type</strong></td>
  3. <td align="left" valign="middle">&nbsp;</td>
  4. <td align="left" valign="middle"><select name="pubtype" id="pubtype" onChange="ajx1('pub_type',this.value);">
  5. <option selected="selected" value="select">-----select-----</option>
  6. <?php
  7. include 'config.php';
  8. $meditype=$_GET['data'];
  9. $qp=mysql_query("select distinct(publication_type) from media_newspaper order by publication_type asc");
  10. while($rp=mysql_fetch_object($qp))
  11.     echo '<option value="'.$rp->publication_type.'">'.$rp->publication_type.'</option>';
  12. ?>
  13. </select></td>
  14.  
code for ajx2.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include 'config.php';
  3. $mediatype=$_GET['data'];
  4. $qp=mysql_query("select * from media_newspaper where publication_type='".$mediatype."'");
  5. while($rp=mysql_fetch_object($qp))
  6. {
  7.     $tmpsizes[]=$rp->size;
  8.     $tmpcolors[]=$rp->color;
  9.     $tmpposition[]=$rp->position;
  10. }
  11. ?>
  12. <tr>
  13.   <td width="35%" align="right" valign="top"><strong>Size</strong></td>
  14.   <td width="6%" align="left" valign="top">&nbsp;</td>
  15.   <td width="59%" align="left" valign="top"><select name="size" id="size">
  16. <?php
  17. foreach($tmpsizes as $key=>$value)
  18.     echo '<option value="'.$value.'">'.$value.'</option>';
  19. ?>
  20.   </select>
  21.   </td>
  22. </tr>
  23. <tr>
  24.   <td align="right" valign="top">&nbsp;</td>
  25.   <td align="left" valign="top">&nbsp;</td>
  26.   <td align="left" valign="top">&nbsp;</td>
  27. </tr>
  28. <tr>
  29.   <td align="right" valign="top"><strong>Color</strong></td>
  30.   <td align="left" valign="top">&nbsp;</td>
  31.   <td align="left" valign="top"><select name="color" id="color">
  32. <?php
  33. foreach($tmpcolors as $key=>$value)
  34.     echo '<option value="'.$value.'">'.$value.'</option>';
  35. ?>
  36.   </select>
  37.   </td>
  38. </tr>
  39. <tr>
  40.   <td align="right" valign="top">&nbsp;</td>
  41.   <td align="left" valign="top">&nbsp;</td>
  42.   <td align="left" valign="top">&nbsp;</td>
  43. </tr>
  44. <tr>
  45. <td align="right" valign="top"><strong>Position</strong></td>
  46. <td align="left" valign="top">&nbsp;</td>
  47. <td align="left" valign="top"><select name="position" id="position">
  48. <?php
  49. foreach($tmpposition as $key=>$value)
  50.     echo '<option value="'.$value.'">'.$value.'</option>';
  51. ?>
  52. </select>
  53. </td>
  54. </tr>
  55.  
Sep 13 '10 #1
9 1268
Canabeez
126 New Member
Hello, can we see a sample page for some more debugging... thanks.
Sep 13 '10 #2
Canabeez
126 New Member
Here, did some changes, try testing this and check if this works. Sorry for so many code changes.

HTML:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function createRequestObj()
  5. {
  6.     var xmlHttp = null;
  7.     if (window.XMLHttpRequest){
  8.        xmlHttp = new XMLHttpRequest();
  9.        if (xmlHttp.overrideMimeType) {
  10.             xmlHttp.overrideMimeType('text/html');
  11.         }
  12.      }
  13.     else
  14.     {
  15.         if (window.ActiveXObject){
  16.         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  17.          if (xmlHttp.overrideMimeType) {
  18.             xmlHttp.overrideMimeType('text/html');
  19.         }
  20.       }
  21.     }
  22. return xmlHttp;
  23. }
  24.  
  25. function ajx1(vari1,vari2)
  26. {
  27.     var d = new Date();
  28.     var time = d.getTime();
  29.  
  30.     if(vari1=='media')
  31.         var url = "ajx1.php?Time="+time;
  32.     else if(vari1=='pub_type')
  33.         var url = "ajx2.php?Time="+time;
  34.     url = url + "&data="+vari2;
  35.  
  36.     var xmlHttp =createRequestObj();
  37.     xmlHttp.open("GET",url,true);
  38.     xmlHttp.onreadystatechange = function()
  39.     {
  40.         if(xmlHttp.readyState==4)
  41.         {
  42.             if(vari1=='media')
  43.                 drawMedia( eval(xmlHttp.responseText) );
  44.             else if(vari1=='pub_type')
  45.             {
  46.                 drawOthers( eval(xmlHttp.responseText) );
  47.             }
  48.         }
  49.     };
  50.     xmlHttp.send(null);
  51. }
  52.  
  53. function drawMedia(_data){
  54.     var sel = document.getElementById('pubtype');
  55.     sel.innerHTML = '<option selected="selected" value="select">-----select-----</option>';
  56.     for(var i in _data){
  57.         var opt = document.createElement('option');
  58.         opt.value = _data[i];
  59.         op.innerHTML = _data[i];
  60.         sel.appendChild(opt);
  61.     }
  62.  
  63.     document.getElementById('media').style.display = 'inline';
  64. }
  65.  
  66. function drawOthers(_data){
  67.     for(var obj in _data){
  68.         var sel = document.getElementById(obj);
  69.         sel.innerHTML = '';
  70.         for(var i in _data[obj]){
  71.             var opt = document.createElement('option');
  72.             opt.value = _data[obj][i];
  73.             opt.innerHTML = _data[obj][i];
  74.             sel.appendChild(opt);
  75.         }
  76.     }
  77.     document.getElementById('pub_type_tr').style.display = 'inline';
  78. }
  79. </script>
  80. </head>
  81. <body>
  82. <table width="100%">
  83.   <form action="<?php $_SERVER['PHP_SELF'];?>" method="post" name="form1" id="form1">
  84.     <tr>
  85.       <td align="right" valign="top"><strong>Media type</strong></td>
  86.       <td align="left" valign="top">&nbsp;</td>
  87.       <td align="left" valign="top"><select name="mediatype" id="mediatype" onchange="ajx1('media',this.value)">
  88.         <option value="select" selected>-----select-----</option>
  89.         <option value="type1">type1</option>
  90.         <option value="type2">type2</option>
  91.       </select>      </td>
  92.     </tr>
  93.     <tr id="media" style="display:none;">
  94.           <td align="right" height="50px" valign="middle"><strong>Publication type</strong></td>
  95.         <td align="left" valign="middle">&nbsp;</td>
  96.         <td align="left" valign="middle">
  97.             <select name="pubtype" id="pubtype" onChange="ajx1('pub_type',this.value);">
  98.                 <option selected="selected" value="select">-----select-----</option>
  99.             </select>
  100.         </td>
  101.     </tr>
  102.     <tr id="pub_type_tr" style="display:none;">
  103.           <tr>
  104.           <td width="35%" align="right" valign="top"><strong>Size</strong></td>
  105.           <td width="6%" align="left" valign="top">&nbsp;</td>
  106.           <td width="59%" align="left" valign="top"><select name="size" id="size"></select></td>
  107.         </tr>
  108.         <tr>
  109.           <td align="right" valign="top">&nbsp;</td>
  110.           <td align="left" valign="top">&nbsp;</td>
  111.           <td align="left" valign="top">&nbsp;</td>
  112.         </tr>
  113.         <tr>
  114.           <td align="right" valign="top"><strong>Color</strong></td>
  115.           <td align="left" valign="top">&nbsp;</td>
  116.           <td align="left" valign="top"><select name="color" id="color"></select></td>
  117.         </tr>
  118.         <tr>
  119.           <td align="right" valign="top">&nbsp;</td>
  120.           <td align="left" valign="top">&nbsp;</td>
  121.           <td align="left" valign="top">&nbsp;</td>
  122.         </tr>
  123.         <tr>
  124.             <td align="right" valign="top"><strong>Position</strong></td>
  125.             <td align="left" valign="top">&nbsp;</td>
  126.             <td align="left" valign="top"><select name="position" id="position"></select></td>
  127.         </tr>
  128.     </tr>
  129.     <tr>
  130.       <td align="right" valign="top"><strong>Date</strong></td>
  131.       <td align="left" valign="top">&nbsp;</td>
  132.       <td align="left" valign="top"><input type="text" name="date" id="date" /></td>
  133.     </tr>
  134.     <tr>
  135.       <td align="right" valign="top">&nbsp;</td>
  136.       <td align="left" valign="top">&nbsp;</td>
  137.       <td align="left" valign="top">&nbsp;</td>
  138.     </tr>
  139.  
  140.     <tr>
  141.       <td align="right" valign="top">&nbsp;</td>
  142.       <td align="left" valign="top">&nbsp;</td>
  143.       <td align="left" valign="top"><input type="submit" name="submit" id="submit" value="Submit" /></td>
  144.     </tr>
  145.   </form>
  146. </table>
  147. </body>
  148. </html>
ajx1.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include 'config.php';
  3.  
  4. $meditype=$_GET['data'];
  5.  
  6. $qp=mysql_query("select distinct(publication_type) from media_newspaper order by publication_type asc");
  7.  
  8. $response = array();
  9.  
  10. while($rp=mysql_fetch_object($qp)){
  11.     $response[] = $rp->publication_type;
  12. }
  13.  
  14. echo json_encode( $response );
  15. ?>
ajx2.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include 'config.php';
  3.  
  4. $mediatype=$_GET['data'];
  5.  
  6. $qp=mysql_query("select * from media_newspaper where publication_type='".$mediatype."'");
  7.  
  8. while($rp=mysql_fetch_object($qp)){
  9.     $tmpsizes[]=$rp->size;
  10.     $tmpcolors[]=$rp->color;
  11.     $tmpposition[]=$rp->position;
  12. }
  13.  
  14. $response = array(
  15.     "size" => $tmpsizes,
  16.     "color" => $tmpcolors,
  17.     "position" => $tmpposition
  18. );
  19.  
  20. echo json_encode( $response );
  21. ?>
Sep 13 '10 #3
vjayis
134 New Member
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.,

Expand|Select|Wrap|Line Numbers
  1.  
  2. function drawOthers(_data){
  3. alert('here');
  4.     for(var obj in _data){
  5.         var sel = document.getElementById(obj);
  6.         sel.innerHTML = '';
  7.         for(var i in _data[obj]){
  8.             var opt = document.createElement('option');
  9.             opt.value = _data[obj][i];
  10.             opt.innerHTML = _data[obj][i];
  11.             sel.appendChild(opt);
  12.         }
  13.     }
  14.     document.getElementById('pub_type_tr').style.display = 'inline';
  15. }
  16.  
and even tried by commenting all the other lines but again no results found..

Expand|Select|Wrap|Line Numbers
  1.  
  2. function drawOthers(_data){alert('here');
  3.    /* for(var obj in _data){
  4.         var sel = document.getElementById(obj);
  5.         sel.innerHTML = '';
  6.         for(var i in _data[obj]){
  7.             var opt = document.createElement('option');
  8.             opt.value = _data[obj][i];
  9.             opt.innerHTML = _data[obj][i];
  10.             sel.appendChild(opt);
  11.         }
  12.     }
  13.     document.getElementById('pub_type_tr').style.display = 'inline';
  14. */
  15. }
  16.  
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(listbox name)" which is produced from the first ajax function.

it displays the result as

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


thanks
Sep 14 '10 #4
Canabeez
126 New Member
Are you getting any errors?

Please also check that the response from the second function ("drawOthers()") does not enter the first one ("drawMedia()").
Sep 14 '10 #5
vjayis
134 New Member
checked., the result is good., when i added the code

Expand|Select|Wrap|Line Numbers
  1. alert(xmlHttp.responseText);
  2.  
it shows the same result perfectly as

{"size":["3"],"color":["black&white"],"position":["Run on page"]}
Sep 16 '10 #6
Canabeez
126 New Member
Try alerting the vari1 variable inside the onreadystatechange() function
Expand|Select|Wrap|Line Numbers
  1.     xmlHttp.onreadystatechange = function()
  2.     {
  3.         if(xmlHttp.readyState==4)
  4.         {
  5.             alert( vari1 );
  6.         }
  7.     };
Sep 16 '10 #7
vjayis
134 New Member
result is "pub_type".

i think there might be some error's inside the second function since the javascript doesnt even enter it..
Sep 16 '10 #8
Canabeez
126 New Member
Try adding
Expand|Select|Wrap|Line Numbers
  1. <input type="button" value="Test" onclick="drawOthers({"size":["3"],"color":["black&white"],"position":["Run on page"]});" />
to your html and test the function itself, see if there are any JavaScript errors.
Sep 16 '10 #9
vjayis
134 New Member
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..
Sep 16 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jacob | last post by:
I have a page Add_data.asp which accepts a number of user-entered variables and then inserts them into a database. I have another page, Loc_tree.asp which when accessed, returns a tree of...
5
by: Ron Vecchi | last post by:
I know the math I need to perform on width and height to keep an aspect ratio but where and how would I implement keeping a set aspect ratio on a form when a user resizes it. Override OnResize?...
4
by: tom | last post by:
Hi Experts, I want to pass the selectedDate value from my calender form to another web form or a web user control. Could you please show me how to do this? Thanks in advance.
4
by: testuser | last post by:
works fine in FF, not in IE form <form action = go.php method = get> <input type='checkbox' name='id' value='1'> <input type='checkbox' name='id' value='2'> <input type='checkbox' name='id'...
4
by: vunet.us | last post by:
Hi all, I am converting my app to AJAX-based. I have a form that submits some data including images. When I use AJAX XmlHttpRequest I am unable to submit the form with...
1
by: registerhere | last post by:
Hi, I was working with acrojavascript when i came acroos it. I have a pdf form. A global variable t=1 is initialized. Just before the form is closed, it calls a function dat increments its...
8
by: jd2007 | last post by:
Why the Ajax code below in ajax.js is causing my form not to work ? ajax.js: var a=0; var b=0; var c=0; var d=0; var e=0; var f=0;
0
by: nicky77 | last post by:
This is a long shot, but I have no idea why my navbar is being temperamental in IE6. Everything looks ok on page load, but when you fill out the online form and click Submit, the navbar either...
5
by: Rob | last post by:
I have a control (Button) on a Parent form which opens a Windows form... all I want to do is pass a value from the child form back to the parent... it should be so simple... i.e....
17
vikas251074
by: vikas251074 | last post by:
I am storing value by the following <td>Name of Person</td> <td><input type="text" width="100px" name="vname" value=<%=vname%>> </td> When I enter 'Vikas Kumar' and press enter key only...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.