469,331 Members | 1,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

Submit the result of a query to the second form with no click

Hello,

I have two forms written mainly in php. Here how it functions currently:

- First form has two submit buttons.
- First submit button is a search button that submits a value of an input field that activates a database query and get the result
- I control the length of the entered text with Java Script in the input field and activate the query without clicking the search button when a certain length is achieved
- second submit button submits the result to the second form as an input field on the same page.

Now, what I would like to do is the following:

- Just send the result of the query to the second form without clicking the second submit button in the first form.

So:
- Control the length of the text entered in the input field (which I already do with Javascript)
- When the length equals to certain value activate the database query (which I already do with php)
- When the result is found submit the result to the second form without clicking any other buttons (which I don't know how to do)

I know it might be better to include some bit of code but even I tried to clean it is still too long to include here.

I would appreciate any hint.

huseyin
Mar 25 '08 #1
5 1924
hsriat
1,654 Expert 1GB
Hello,

I have two forms written mainly in php. Here how it functions currently:

- First form has two submit buttons.
- First submit button is a search button that submits a value of an input field that activates a database query and get the result
- I control the length of the entered text with Java Script in the input field and activate the query without clicking the search button when a certain length is achieved
- second submit button submits the result to the second form as an input field on the same page.

Now, what I would like to do is the following:

- Just send the result of the query to the second form without clicking the second submit button in the first form.

So:
- Control the length of the text entered in the input field (which I already do with Javascript)
- When the length equals to certain value activate the database query (which I already do with php)
- When the result is found submit the result to the second form without clicking any other buttons (which I don't know how to do)

I know it might be better to include some bit of code but even I tried to clean it is still too long to include here.

I would appreciate any hint.

huseyin
Its a unique case. Post you code here.
Mar 25 '08 #2
acoder
16,027 Expert Mod 8TB
Now, what I would like to do is the following:

- Just send the result of the query to the second form without clicking the second submit button in the first form.
If you can send the result to the first form, the second form should be easy. Get the response from the Ajax script and set the field(s) in the second form instead of the first.
Mar 26 '08 #3
Thanks for replies. One thing is that I am not using Ajax even, I never attempted to use it. I have to use php+javascript at the moment unless you really insist that Ajax is the only solution :) In that case I have to learn yet another tool.

I will clean the code and post it here a.s.a.p.

huseyin
Mar 27 '08 #4
I think I managed to clean the code a little bit and made it readable. I hope I didn't remove an important part by mistake :)

I have added some comment lines to make it somewhat more clear.

Once more, thanks for the help.

Here is the listing:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. // check the number of digits entered in the search box
  3. function check()
  4. {
  5.   var letters = document.additem.item_search.value.length +1;
  6.   if (letters <= 6){
  7.     document.additem.item_search.focus();
  8.   } else {
  9.     document.additem.submit();
  10.     document.additem.addName.focus();
  11.   }
  12. }
  13. function check1()
  14. {
  15.   document.additem.addName.focus();
  16. }
  17. function check2()
  18. {
  19.   document.additem.addName.submit();
  20. }
  21. function formSubmit()
  22. {
  23.   document.additem.addName.submit();
  24. }
  25. function redirectOutput(add_name) {
  26.   var w = window.open('about:blank','Popup_Window','toolbar=no,location=no,directories=no,status=no,menubar=no,sc rollbars=no,resizable=no,copyhistory=no,width=700,height=550,top=20,left=500');
  27.   document.add_name.target = 'Popup_Window';
  28.   return true;
  29. }
  30. </Script>
  31. </head>
  32. <body>
  33. <?php
  34.   if(isset($_POST['addName'])){
  35.     $items_to_add=array();
  36.     $items_to_add=$_POST['id'];
  37.     $name_to_add=$_POST['name'];
  38.     $quantity_to_add=$_POST['quantity'];
  39.  
  40.     for($k=0;$k<count($items_to_add);$k++){
  41.       $_SESSION['items_in'][]=$items_to_add[$k].' '.$name_to_add . ' ' . $quantity_to_add;
  42.     }
  43.   }
  44. // posts to itself (this files name is additem.php)
  45.   echo "<form name='additem' method='POST' action='additem.php' onSubmit='formSubmit()'>
  46.      <table>
  47.      <tr>
  48.      <td>";
  49. // If something is entered in the search box do the search
  50. // This search is activated either by clicking the 'Go' button
  51. // or by pushing enter or when the entered character size equals to 6
  52.   if(isset($_POST['item_search'])  and $_POST['item_search']!=''){
  53.     $search=$_POST['item_search'];
  54.     $query="SELECT something FROM somewhere WHERE condition";
  55.   }
  56.  
  57.   $result=mysql_query($query,$dbf_osc->conn) or die(mysql_error());
  58.   $num_rows = mysql_num_rows($result);
  59. //This is the Search box and Go button
  60.   echo "<td>
  61.     <input type='text' id='item_search' name='item_search' MAXLENGTH='6' onKeyUp='(check() && check1())'>
  62.     <input type='submit' value='Go' id='item_go' name='item_go'></td>";
  63.   echo "<td><select name='items[]' size='1'>\n";
  64.  
  65. // If there is only one result add it automatically to the second form
  66. // (this is where I exactly fail)
  67. // Otherwise show the found items as a select box
  68.   if($num_rows==1){
  69.     $row=mysql_fetch_assoc($result);
  70.     $id=$row['id'];
  71.     $name=$row['name'];
  72.     $option_value=$id . ' ' . $name;
  73.     $display_item="$row[name]";
  74.     echo "<option selected value='".$option_value."'>".$display_item."</option>\n";
  75. // I thought this would do, but doesn't
  76.     echo "<script type=\"text/javascript\">
  77.       document.additem.addName.submit();
  78.           </Script>";
  79.   }
  80.   } else {
  81.     while($row=mysql_fetch_assoc($item_result)){
  82.       $id=$row['id'];
  83.       $name=$row['name'];
  84.       $option_value=$id . ' ' . $name;
  85.       $display_item="$row[name]";
  86.       echo "<option value='".$option_value."'>".$display_item."</option>\n";
  87.     }
  88.   }
  89.   echo "</select></td>";
  90. // This is the addname submit button
  91. // which submits the result of the search to the second form
  92.   echo"
  93.     <td><input type='submit' value='Add Name' id='addName' name='addName'></td>
  94.     </tr>
  95.     </table></form><br>
  96.     <script language=\"JavaScript\">
  97.     document.forms[0].elements[1].focus();
  98.       </script>";
  99. // Check if anything added to the second form
  100. // if so show the items
  101.   if(empty($_SESSION['items_in'])){
  102.     echo "<center><h3>&nbsp;</h3></center>";
  103.   }
  104.  
  105.   if(isset($_SESSION['items_in'])){
  106.     $num_items=count($_SESSION['items_in']);
  107.     $temp_item_name='';
  108.     $temp_item_id='';
  109.     $item_info=array();
  110.  
  111. // The second form is used to add some details about the search result
  112. // i.e. I search for a name with a given number and in this form
  113. // I add some quantity next to the name then generate a list of names
  114. // Finally i submit the generated list to the next screen
  115.     echo "<form name='add_name' action='list.php' method='POST' onSubmit='redirectOutput(this)'>
  116.     ";
  117.     echo "<table>
  118.       <tr>
  119.       <th>Id</th>
  120.       <th>Name</th>
  121.       <th>Quantity</th>
  122.       </tr>";
  123. // In fact at this point I get the Id from the previous form and
  124. // perform another search with the Id and pull the quantity from the database
  125. // if it is set earlier. To keep it short I skipped that query part
  126.       for($k=0;$k<$num_items;$k++){
  127.         $item_info=explode(' ',$_SESSION['items_in'][$k]);
  128.         $temp_id=$item_info[0];
  129.         $temp_name=$item_info[1];
  130.         $temp_quantity=$item_info[2];
  131.         echo "<tr>
  132.           <td>$temp_id</td>
  133.           <td>$temp_name</td>
  134.           <td><input type=text name='quantity$k' value='$temp_quantity' size='3'></td>
  135.           </tr>
  136.           <input type='hidden' name='item_id[$k]' value='$temp_id'>
  137.           <input type=hidden name='name[$k]' value='$temp_name'>
  138.           <input type=hidden name='quantity[$k]' value='$temp_quantity'>";
  139.       }
  140.   }
  141.  
  142.   echo "</table>";
  143. // The following submit gets the generated list and submits to the next screen
  144.   echo "<table>
  145.     <tr>
  146.     <td><input type='submit' name='AddNames' id='AddNames' value='List'>
  147.     </td>
  148.     </form>
  149.     </tr>
  150.     </table>";
  151. }
  152. $dbf->closeDBlink();
  153. ?>
  154.     </body>
  155.     </html>
  156.  
Mar 27 '08 #5
hsriat
1,654 Expert 1GB
You need to redesign the algorithm.

Use something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //FILE NAME : additem.php
  3.  
  4. if (submit_of_form1_pressed)
  5. {
  6.     get_the_search_from_the_db();
  7.     if (only_one_record present) 
  8.     {
  9.         display_the_result_for_that_one_record();
  10.         OR
  11.         redirect_to_the_second_form_action();
  12.         //eg..
  13.         header("Location:abc.php?q=aaa");
  14.     }
  15.     else
  16.     {
  17.         display_the_second_form();
  18.     }
  19. elseif (submit_of_form2_pressed)
  20. {
  21.     display_the_results_of_second_form();
  22.     OR
  23.     redirect_to_the_second_form_action();
  24.     //eg..
  25.     header("Location:abc.php?q=aaa");
  26. }
  27. else
  28. {
  29.     display_the_first_form();
  30. }
  31. ?>
But I'll still recommend to use Ajax to make it better.
Mar 27 '08 #6

Post your reply

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

Similar topics

5 posts views Thread by lsarg | last post: by
10 posts views Thread by Gregory A Greenman | last post: by
4 posts views Thread by houstoncity2004 | last post: by
2 posts views Thread by Gayathri | last post: by
5 posts views Thread by ningjun.wang | last post: by
1 post views Thread by ansc1 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.