2

I have a huge form and in part of the form I want to insert some value to database using js. I might not be clear about how to present the question but here my needs are: suppose, I have two tables in database table1, table2. In a html form:

<select name="tab1" id="tab1">  
<?php   while($row = fetch from table 1){   ?>  
        <option value"<?=$row['name']?>" name="option1"><?=$row['name']?></option>  
<?php  }  ?>  
</select>  
<input type="file" name="file">  
<input type="button" name="button" onclick="submit_form(true,'');">

Now, I want to pass the $row['name'] value to submit_form() function in javascript. The javascript code will check the value and return it to the form to submit it. My question is since the $row['name'] from table1 is inside the while loop, I cannot pass the value to javascript. If the form was small I could have done using submit button and check $_POST('submit') type. I want to insert the $row['name'] in this form to table2 as file name associated with the name.

PicoCreator
  • 9,886
  • 7
  • 43
  • 64

3 Answers3

1

As i understand you want to pass selected value from form to submit_form() function?

function submit_form(param1, param2){

    var passedValue = document.getElementById('tab1').value;

     // here is your old submit_form() function. passedValue contains 
     // your selected $row['name']

}
rpalkin
  • 51
  • 2
  • yes indeed... I want to pass the param1, param2 to the function. Lets say param1 is is boolean value to check the form, param2 should be the value from $row['name'] from the option which is selected. All I want is to be able to send the selected option to the param2 of function submit_form() –  May 05 '12 at 12:44
  • So in this case you don't need `param2` for `submit_form()`. Use `passedValue` variable inside `submit_form()` instead of `param2`. Or if `submit_form()` used to check not only this one form you may change your button code `` and use selected value as `param2` in `submit_form()` – rpalkin May 05 '12 at 13:11
  • yes, submit_form() function is one function to check all the file submit types, so, document.getElementById('tab1').value looks nice choice. Thank you very much for your input.... –  May 05 '12 at 14:22
0

@Jhilke Dai, First of all your php code is little buggy, '=' sign must be in html not in php the correct code is

<select name="tab1" id="tab1"> 
<?php while($row = fetch from table 1) { ?> 
<option value="<? echo $row['name'] ?>" name="option1"><? echo $row['name'] ?></option> 
<?php } ?> 
</select> 
<input type="file" name="file"> <input type="button" name="button" onclick="submit_form(true,'')">
Shaikh Farooque
  • 2,620
  • 1
  • 19
  • 33
  • thanks for correcting me, yes I missed the = in html, but = in php can be used as short tag for echo like =$row['name']?> if this is what you were saying –  May 05 '12 at 12:11
0

You can use generic functions or even jQuery itenerations, to fetch form values

See the similar question answer : Get selected value/text from Select on change

function getDomValueByID( id ) {
    return document.getElementById(id).value;
}

function submit_form( a, b ) {
    var formValue = getDomValueByID( 'tab1' );
    //OR
    var jQueryFormValue = jQuery( "#tab1" ).val();
    //Do what u want here.
}

In fact several consider it a very bad idea to pass the option data over via javaScript, if its already generated on page for the following reasons

  1. Duplicate data, wasted bandwith.
  2. Less portable code, non-OOP.
  3. Harder to maintain, changes in your php code, requires changes in your javaScript code.

Also if you are really interested (this practice is sometimes frowned on). You can use the following as PHP code somewhere in the header. To pass PHP variables to JavaScript. However there are lots of better ways to do this, from JSONS to XML.

<?php optList = ['one', 'two', 'three']; ?>
<script type="text/javascript">
     //Window represents the global variable space, and doing this is really bad practice as listed above.
     window.optionList = [ <?php echo( implode(' , ', optList) );?> ];
</script>
Community
  • 1
  • 1
PicoCreator
  • 9,886
  • 7
  • 43
  • 64
  • this sounds cool... i'll try..... If you are willing to reply then I'd also like to know if it's possible to get the current value from inside the while loop to outside for other purposes. –  May 05 '12 at 12:52
  • @JhilkeDai Actually there are means of transfering data from PHP to JavaScript, and there are lots of methods related to this, from JSONS to XML. In fact its under a collective term known as AJAX. And also, welcome to stackOverflow... feel free to ask more question for the community to guide you along =P (and yup im more then willing to reply) – PicoCreator May 05 '12 at 12:54
  • @JhilkeDai Added a simple script hack, which is considered bad-practice by many... however even i sometimes use it, to test random things during development =X – PicoCreator May 05 '12 at 13:01
  • thank you very much "pico.creator" this looks nice hack to try to implement the concept. –  May 05 '12 at 14:20
  • @JhilkeDai yup, but dun get too used to it. Rely more on the original answer then the hack. =) These sort of code open cans of worms in major javascript / php website projects – PicoCreator May 05 '12 at 14:30