CMellor Posted April 15, 2006 Share Posted April 15, 2006 Hi. If I could just draw your attention to this snippet of code:[code]<tr><td class="preres" style="background: #CCC"><fieldset><legend>Who's in your stable?</legend><form action="" method="post">Select a name for your stable.<br /><input type="text" name="team_name" /><br />Your selections must be on the same brand show as you.<br /><?php for($i = 0; $i < $_POST['number']; $i++) { echo('<input type="text" name="stable_mate[$i]" /><br />'); } ?><input type="submit" name="submit_stable" value="Request"/></form></fieldset></td></tr><?php } } elseif($_POST['submit_stable']) { ?><tr><td class="preres" style="background: #CCC"><span class="tag_error"><?=$_POST['stable_mate']?></span></td></tr>[/code]Before this part of the code works, you select a number between 3 and 5 and depending on the number chosen, that's how many input boxes are displayed. I used the for() function to do that. To do this though, I did find it from a PHP book I own, that's why in the input's name, it says [$i] after the name.What I can't figure out is how to display the text I input on the x input boxes, so if I selected 3 input boxes, filled them in, and clicked Submit, I'd want it to display those 3 input values.If anyone knows how I can do this, I'd be grateful. Thanks for your time. I look forward to your responces.Chris. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 15, 2006 Share Posted April 15, 2006 First, change this:[code]<?phpfor($i = 0; $i < $_POST['number']; $i++) { echo('<input type="text" name="stable_mate[$i]" /><br />'); } ?>[/code]to:[code]<?phpfor($i = 0; $i < $_POST['number']; $i++) echo '<input type="text" name="stable_mate[' . $i . ']" /><br />';?>[/code]Then in your processing piece:[code]<?phpforeach ($_POST['stable_mate'] as $val) if (trim(stripslashes($val)) != '') echo htmlentities(trim(stripslashes($val))) . '<br />';?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
CMellor Posted April 15, 2006 Author Share Posted April 15, 2006 That's awesome, thanks. Quote Link to comment Share on other sites More sharing options...
CMellor Posted April 15, 2006 Author Share Posted April 15, 2006 Follwing on from this...I want to add the values of the inputs into the database, but when I execute it, it add's each input value as a row, and doesn't list them in the same row. Here's the code:[code]<?php } } elseif($_POST['submit_stable']) { ?><tr><td class="preres" style="background: #CCC"><p>Thanks. Your chosen stable name is <strong><?=$_POST['team_name']?></strong>.</p><p>And you have chosen:</p><?php foreach($_POST['stable_mate'] as $members) {mysql_query("INSERT INTO $dbname.request_stable(leader, members, group_name)VALUES('".$_SESSION['name']."', '$members', '".$_POST['team_name']."')");echo('- ' . $members . '<br />'); } ?><br />to be in your stable.</td></tr><?php } else { ?>[/code]I thought it would of listed the values in the same row, but no, it lists each value as a row. Can anyone tell me what I might be doing wrong?Thanks. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 15, 2006 Share Posted April 15, 2006 That's because you're doing one insert for each value.There are two ways of inserting them all together.[list][*]Insert them as CSV (comma seperated values) in a string:[code]<?php$members = implode(', ',$_POST['stable_mates'];mysql_query("INSERT INTO $dbname.request_stable(leader, members, group_name)VALUES('".$_SESSION['name']."', '$members', '".$_POST['team_name']."')");echo 'You have chosen ' . $members . ' to be in your stable';?>[/code][*]Insert a serialized array containing the values:[code]<?php$members = $_POST['stable_mates'];$tmp = serialize($members);mysql_query("INSERT INTO $dbname.request_stable(leader, members, group_name)VALUES('".$_SESSION['name']."', '$members', '".$_POST['team_name']."')");echo 'You have chosen ' . implode(', '.$members) . ' to be in your stable';?>[/code][/list]Ken Quote Link to comment Share on other sites More sharing options...
CMellor Posted April 15, 2006 Author Share Posted April 15, 2006 Is any of the code you provided their a bit wrong, 'cause it looks a bit wrong, mostly just the implode() function, I'm getting Bad Argument errors when doing it with the code above.Do I add the implode() function inbetween the foreach() function? like so...[code]<?php foreach($_POST['stable_mate'] as $members) {/* INSERT IT IN HERE ??? */mysql_query("INSERT INTO $dbname.request_stable(leader, members, group_name)VALUES('".$_SESSION['name']."', '$members', '".$_POST['team_name']."')");echo('- ' . $members . '<br />'); } ?>[/code]Sorry, just don't seem to understand it, maybe I need to take a break, lol.Thanks for your help though, I really appriciate it. Quote Link to comment Share on other sites More sharing options...
CMellor Posted April 16, 2006 Author Share Posted April 16, 2006 Hey, sorry to bump this, but I really can't figure it out, even with the example code, am just getting errors. It's probably gonna be something really simple, and i'll end up kicking myself for it, lol, usually the case. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 16, 2006 Share Posted April 16, 2006 Remove the foreach(). It is not necessary.Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.