How to Storing form array data using PHP

Discussion in 'PHP' started by as raj, Apr 25, 2013.

  1. #1
    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"/>&nbsp;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:
     
    as raj, Apr 25, 2013 IP
  2. gvre

    gvre Member

    Messages:
    35
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    33
    #2
    You should add [] at the end of the inputs' names.
    e.g.
    <select name="countryid" id="countryid">
    should be
    <select name="countryid[]" id="countryid">
     
    gvre, Apr 25, 2013 IP
  3. Delta9er

    Delta9er Active Member

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #3
    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.
     
    Delta9er, Apr 28, 2013 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4

    Wat.

    Javascript already makes no sense here, but jQuery? Just to get the value of a text field? Native Javascript has no future. :(
     
    nico_swd, Apr 28, 2013 IP
  5. as raj

    as raj Greenhorn

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5
    <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"/>&nbsp;&nbsp;Yes&nbsp;
                                <input type="radio" name="col_certi[<?php echo $i;?>]" value="No"/>&nbsp;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.
     
    as raj, Apr 29, 2013 IP
  6. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #6
    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:
     
    Hamidsam, Apr 29, 2013 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    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, Apr 30, 2013 IP
  8. gvre

    gvre Member

    Messages:
    35
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    33
    #8
    @deathshadow Numbers in [] are not required but it is a good practice, specially when there are disabled elements.
     
    gvre, Apr 30, 2013 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    I know, I just don't trust it without... you mentioned one of the many reasons.
     
    deathshadow, May 3, 2013 IP
  10. annaharris

    annaharris Active Member

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    51
    #10
    Try below code to store form array data using PHP:
    $_SESSION['key'] = serialize($yourArray);
     
    annaharris, May 6, 2013 IP
  11. gvre

    gvre Member

    Messages:
    35
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    33
    #11
    Did you read the question?
     
    gvre, May 6, 2013 IP
  12. Delta9er

    Delta9er Active Member

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #12
    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??
     
    Delta9er, May 12, 2013 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #13
    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.
     
    deathshadow, May 13, 2013 IP