473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2049
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

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

Similar topics

5
by: lsarg | last post by:
i've been trying forever to figure out a way to use a regular text link in place of a submit button at the bottom of this. can't get it. i'm just starting to learn php, so i'm stuck. any help at...
15
by: Mattia | last post by:
Hi! I have a <form> that can be submitted thruogh three buttons and I need to tell witch one was pressed when the form was submitted. I now do it with 3 <input type="submit" name="..."...
10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
4
by: houstoncity2004 | last post by:
Hi, I need help to JavaScript. I am new to this, and am trying to the page online ASAP. I have two submit buttons on one form, and each button will have to do different things. I named both...
2
by: Gayathri | last post by:
Please find the pasted html, <html> <script language="JavaScript" src="cal.js"></script><!-- Date only with year scrolling --> </head> <BODY onLoad="showDetails()"> <script...
5
by: ningjun.wang | last post by:
How can I submit a form to multiple web sites when user click the submit button? Something like the following: myform.action = url_of_server1; myform.submit(); myform.action = url_of_server2;...
1
by: ansc1 | last post by:
Hello, I'm new to using php coding. I need help with the following: 1. There is a submit button on the form and is saves information to my database. After clicking on "Save Measurement" it...
2
by: sakat | last post by:
<?php $dbhost = 'unix.lsbu.ac.uk'; $dbuser = 'lrc3'; $dbpass = 'dietdft_'; $dbname = 'lrc3'; // This is an example open a db $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error...
2
by: sbettadpur | last post by:
Hi everybody, Hi iam strugling with more than one submit buttons with in one form here is my code <form method="post" action="Offer.php" name='issueFrm' onSubmit="return fullOfferfields();">...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.