i use for loop for repeat the text box and drop down box for three times in web page... now i want to store the data in my database using array concept.. i search in internet but i not get perfect example of this program.. pls help how to store the data... <?php for($i=1;$i<=3;$i++) { ?> PHP: <tr> <td >Name </td> <td><input type="text" name="name value="/></td> </tr> <tr> <td> sex</td> <td><input type="radio" name="sex" value="Yes"/>Yes <input type="radio" name="sex" value="No"/> No </td> <td>File Upload</td> <td><input type="file" name="your photo" value=""/></td> </tr> <tr> <td >Country</td> <td><select name="countryid" id="countryid"> <option value="">Select</option> </select> </td> </tr> HTML: <?php } ?> PHP:
You should add [] at the end of the inputs' names. e.g. <select name="countryid" id="countryid"> should be <select name="countryid[]" id="countryid">
You can also use jQuery to get the values to store in the database, ex: <input type="text" id="abc" value="" /> <script type="text/javascript"> var input_value = jQuery('#abc').val(); </script> You can post that to another page or method to be saved to your database.
Wat. Javascript already makes no sense here, but jQuery? Just to get the value of a text field? Native Javascript has no future.
<table align="center" style="color:#6699CC;font-size:20px;"> <?php for($i=1;$i<=4;$i++) { ?> <tr> <td>Name</td> <td><input type="text" name="col_name[<?php echo $i;?>]" value=""/></td> </tr> <tr> <td >Complete Address (Road Name, City, State, Country, Zip)</td> <td><textarea name="add[<?php echo $i;?>]" value=""></textarea></td> </tr> <td> Date of birth(Ex.: mm-dd-yyyy)</td> <td><input type="text" name="dob[<?php echo $i;?>]" id="dob<?php echo $i ;?>" value=""/> </tr> <tr> <td>Sex</td> <td><input type="radio" name="col_certi[<?php echo $i;?>]" value="Yes"/> Yes <input type="radio" name="col_certi[<?php echo $i;?>]" value="No"/> No </td> <tr> <td >Country</td> <td><select name="countryid[<?php echo $i;?>]"/> <option value="">Select</option> </select> </td> </tr> <tr> <td>File Upload</td> <td><input type="file" name="col_certi_file[<?php echo $i;?>]" value="<?php echo $fir_col_certi_file;?>"/></td> </tr> <tr><td align="center" colspan="4"><hr color="#6699CC" width="90%"/></td></tr> <?php } ?> </table> HTML: and i write the code for save the col_name data only using php foreach($col_name as $colname) $updatecol="update education set col_name='$colname' where employee_id=".$ employee_id; if(!mysql_query($updatecol)) here the $colname last value only store in the database.. i want to store 4 value in database.. how to do it.
You need two loop for processing form data. The first for column (or form inputs) and another one for input indexes. $fields = array('col_name', 'add', 'dob', 'countryid', 'col_certi_file'); foreach ($fields as $field) foreach ($_POST[$field] as $key => $value) $updatecol="update education set $field='$value' where employee_id=".$employee_id; PHP:
1) What makes those a table in the first place? Where are your labels? If you can't build the markup properly, you probably shouldn't be throwing PHP at it yet. 2) you can't have spaces in NAME attributes 3) if value is empty you don't have to say value... 4) wondering if half the respondants so far understood the question. gvre is kind-of on the right track, but it would be easier to just make an array of their names, and iterate that array on the read. If you put numbers in [] in the name attribute, it will magically turn into an array server-side in PHP. outputting the form: for ($i=0; $i<3; $i++) { echo ' <label for="form_name_',$i,'">Name</label> <input type="text" name="name[',$i,']" id="form_name_',$i,'" /> <br /> <fieldset> <legend>Sex</legend> <input type="radio" name="sex[',$i,']" id="form_sexYes_',$i,'" value="Yes" /> <label for="form_sexYes_',$i,'">Yes</label> <input type="radio" name="sex[',$i,']" id="form_sexNo_',$i,'" value="No" /> <label for="form_sexNo_',$i,'">No</label> </fieldset> <label for="form_yourPhoto_',$i,'">File Upload</label> <input type="file" name="yourPhoto[',$i,']" id="form_yourPhoto_',$i,'" /> <br /> <label for="form_countryId_',$i,'">Country</label> <select name="countryId[',$i,']" id="form_countryId_',$i,'" > <option value="">Select</option> </select> <br />'; } Code (markup): Pulling the fields is easy then -- this just echos out the values running a few simple checks so you don't fill up your error log. $fields = array('name','sex','yourPhoto','countryId'); for ($i=0; $i<3; $i++) { foreach ($fields as $fieldName) { echo $fieldName,': ',( isset($_POST[$fieldName][$i]) ? ( empty($_POST[$fieldName][$i]) ? '*** EMPTY ***' : htmlspecialchars($_POST[$fieldName][$i]) ) : '*** NOT SET ***' ),'<br />'; } echo '<hr />'; } Code (markup): Hope this helps.
@deathshadow Numbers in [] are not required but it is a good practice, specially when there are disabled elements.
Wat x2.. Why not? You either do a post with php or you post with jQuery/Javascript, how else can you get the values into the database??
PHP isn't a "either" -- you'd still need it scripting or not -- and it might even take MORE php to support... Its not like Javascript (and by extension jQuery) has direct access to the database in the first place (nor should it given what a security train wreck that would be), so what on earth purpose would it even serve here apart from code bloat caused by ignorance, ineptitude, or just sleazing out pages any old way? Though honestly, that can be said of 99% of the garbage people throw jQuery at for nothing useful to actual visitors and that serves no real purpose apart from stroking some scriptttard's... ego.