mikejs Posted October 27, 2009 Share Posted October 27, 2009 Hi I have a form with several input boxes I want to take the input into a string $str = $_POST["box ++ box1 ++ box2 ++ box7 ++ box8"]; this is my table <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <p> <input type="text" name="box" value="" > Box1<br /> <input type="text" name="box1" value="" /> Box2<br /> <input type="text" name="box2" value="" /> Box3<br /> <label> <select name="box7" id="box7"> <option value="1">Red</option> <option value="2">Blue</option> <option value="3">Green</option> </select> </label> Color<br /> <label> <select name="box8" id="box8"> <option value="1">8px</option> <option value="2">10px</option> <option value="3">14px</option> </select> </label> Font size<br /> <input type="submit" name="submit" value="Send"> </p> </form> <?php Any sugestions ? I would then like to take the $str and split it back into its individual ids using explode function maybe thanks M Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/ Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 You can't do it like that. I think what you want to do is something like this: You're able to specify an array as the name for the elements in a form, then $_POST['fieldname'] would be an array containing the information from all the inputs. You can then do whatever you want with that. ex ... <input type="text" name="fieldname[]" /> <input type="text" name="fieldname[]" /> ... <?php $array = $_POST['fieldname']; // Array('first input value', 'second input value'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/#findComment-945749 Share on other sites More sharing options...
knsito Posted October 27, 2009 Share Posted October 27, 2009 $str = $_POST['box'].' ++ '. $_POST['box1'].' ++ '. $_POST['box2'].' ++ '. $_POST['box7'].' ++ '. $_POST['box8']; etc Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/#findComment-945757 Share on other sites More sharing options...
mikejs Posted October 27, 2009 Author Share Posted October 27, 2009 Thanks for the reply I have <input type="text" name="fname[]" value="" > Box1<br /> <input type="text" name="fname[]" value="" /> Box2<br /> <input type="text" name="fname[]" value="" /> Box3<br /> <input type="text" name="fname[]" value="" /> Box4<br /> <input type="text" name="fname[]" value="" /> Box5<br /> <input type="text" name="fname[]" value="" /> Box6<br /> with $data = $_POST['fname']; if I echo $data I should get (value,value,value, etc ? Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/#findComment-945765 Share on other sites More sharing options...
KevinM1 Posted October 27, 2009 Share Posted October 27, 2009 Thanks for the reply I have <input type="text" name="fname[]" value="" > Box1<br /> <input type="text" name="fname[]" value="" /> Box2<br /> <input type="text" name="fname[]" value="" /> Box3<br /> <input type="text" name="fname[]" value="" /> Box4<br /> <input type="text" name="fname[]" value="" /> Box5<br /> <input type="text" name="fname[]" value="" /> Box6<br /> with $data = $_POST['fname']; if I echo $data I should get (value,value,value, etc ? You can't echo it directly as $_POST['fname'] is an array. So, you'll need to loop through it: foreach ($_POST['fname'] as $value) { echo "$value "; } Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/#findComment-945767 Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 To see the contents of the array (for debugging or whatever) use print_r. If you want to just display the contents of the array as a string with a comma as the delimiter you can use implode() ex ... echo implode(', ', $data); Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/#findComment-945770 Share on other sites More sharing options...
KevinM1 Posted October 27, 2009 Share Posted October 27, 2009 Ah, yes, implode. I always forget about that one. Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/#findComment-945773 Share on other sites More sharing options...
mikejs Posted October 27, 2009 Author Share Posted October 27, 2009 To see the contents of the array (for debugging or whatever) use print_r. If you want to just display the contents of the array as a string with a comma as the delimiter you can use implode() ex ... echo implode(', ', $data); Hi I tried that I get Warning: implode() [function.implode]: Invalid arguments passed ?? Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/#findComment-945790 Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 Are you sure $data is an array? Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/#findComment-945798 Share on other sites More sharing options...
mikejs Posted October 27, 2009 Author Share Posted October 27, 2009 Are you sure $data is an array? I have the following if(isset($_POST['submit'])) { $str = $_POST['fname'].' ++ '.$_POST['fname2'].' ++ '.$_POST['fname3'].' ++ '.$_POST['fname4'].' ++ '.$_POST['fname5'].' ++ '.$_POST['fname6'].' ++ '.$_POST['fname7'].' ++ '.$_POST['fname8']; do_it($str); } ?> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> So in my case I have $str as my array Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/#findComment-945804 Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 Can you post the code in context please? In that example implode() isn't being used. And $str isn't even an array unless do_it() takes $str be reference and changes it into an array? I can't really be much of any help unless you post the actual code in question. Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/#findComment-945806 Share on other sites More sharing options...
mikejs Posted October 28, 2009 Author Share Posted October 28, 2009 Hi sorry its a string not an array I kind of have it working now all the inputs are stored in $str as a string how can I add a / charecter in between each group box 1 www box 2 eee current output wwweee I want the output to be www/eee/ etc at the reciving end I want the $str to be put back into varibles that i can use to populate a table Quote Link to comment https://forums.phpfreaks.com/topic/179255-help-with-strings-and-arrays/#findComment-945903 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.