SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    submit form with multiple values for variable.

    Can somebody please explain to me in simple beginners terms how I can submit a form with more than one value for each variable to my database?

    $tarifid and $tarifname can have multiple values which I am using to generate the table, and I need to insert corresponding values for 'pitch' and 'adult' entered by the user to the database, and want to do it with one submit button, one form if possible without getting too complicated.

    Code PHP:
    echo "<form action='updatecampingpricelist.php' method='get' name='FormName'>";	
     
     
    	echo "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";
     
    	echo "<tr><td>Tarif id</td>";
    	echo "<td>Tarif name</td>";
    	echo "<td>Pitch</td>";
    	echo "<td>Adult</td></tr>";
     
     
     
    $sql = @mysql_query("select tarifid, tarifname from campingtarif");
     
    	while ($campingtarif = mysql_fetch_array($sql))	{
    		$tarifid = $campingtarif['tarifid'];
    		$tarifname = $campingtarif['tarifname'];
     
    	echo "<tr valign = 'center'>\n";
     
    echo "<td align = 'center'>" . $tarifid . "</td>\n";
    echo "<td align = 'center'>" . $tarifname . "</td>\n";
    echo "<td align = 'center'><input type='text' name='pitch'></td>\n";
    echo "<td align = 'center'><input type='text' name='adult'></td>\n";
     
    }
    echo "</table>";
     
    echo "<p><input type='submit' value='submit'></p>";	
     
    echo '</form>';

  2. #2
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it pitch and adult that have multiple values that a user would input into the field? Can you include the relevant tables you are using.

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pitch and adult are entered by user against values for $tarifid and $tarifname which are already set.
    Tables involved are
    Code MySQL:
    describe campingtarif
    tarifid int not null primary key auto_increment
    tarifname  text not null

    select all to get $tarifid and $tarifname

    Code MySQL:
    describe campingprices
    tarifid int not null primary key
    seasonid int not null
    pitch decimal(7,2) not null
    adult decimal(7,2) not null

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    got interupted and never finished that!
    To help clarify the aim is to create new price list for items $pitch and $adult for predefined services ($tarifid and $tarifname) available between specified dates ($season). $tarifid, $tarifname and $season all set on an earlier page.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php supports a specific request variable naming syntax which it will parse into arrays for you. It works a lot like native php arrays.

    http://www.php.net/manual/en/faq.htm...aq.html.arrays

    Code:
    <input name="data[0][pitch]">
    <input name="data[0][adult]">
    
    <input name="data[1][pitch]">
    <input name="data[1][adult]">
    
    <input name="data[2][pitch]">
    <input name="data[2][adult]">
    php will provide you with a multidimensional array
    PHP Code:
    print_r($_POST['data']);

    foreach (
    $_POST['data'] as $associated_fields) {
        echo 
    $associated_fields['pitch'] . $associated_fields['adult'] . "\n";


  6. #6
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats a big help.

    Table now altered like this using $tarifid as index

    Code PHP:
    echo "<td align = 'center'>" . $tarifid . "</td>\n";
    echo "<td align = 'center'>" . $tarifname . "</td>\n";
    echo "<td align = 'center'><input type='text' name='data[$tarifid][pitch]' size='8'></td>\n";
    echo "<td align = 'center'><input type='text' name='data[$tarifid][adult]'8'></td>\

    Gives this array with sample values inserted

    Array ( [1] => Array ( [pitch] => 20 [adult] => 12 ) [2] => Array ( [pitch] => 18 [adult] => 15 ) [4] => Array ( [pitch] => 25 [adult] => 50 ) )
    But how do I now get $tarifid out of this to include in echo?

    Code PHP:
    echo $tarifid . $associated_fields['pitch'] . $associated_fields['adult'] . "\n";

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <input name="data[0][tarifid]" value="475">
    You could use type=hidden if desired


    Or, if tarifid is unique, and is a value that is suitable to use as a php array key, you could use it as one. eg 475

    Code:
    <input name="data[475][pitch]">
    <input name="data[475][adult]">
    PHP Code:
    foreach ($_POST['data'] as $tarifid => $associated_fields

  8. #8
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that.

    Using type = hidden seems to work ok.

    But as a beginner to php I'm still having a bit of a problem getting my head around arrays - creating them and then sorting results and identifying specific items. For example, in the array created originally how would I output

    array[2] pitch>=18
    for further processing on its own?

    Purely academic in this instance but the sort of thing I'm struggling with.

    Any advice / worthwhile reading at a level I can understand appreciated.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •