glassfish Posted October 22, 2014 Share Posted October 22, 2014 (edited) Code: <?php /* * HASHTAGS */ if(isset($_POST['hashtag0'])) { $hashtag0 = $_POST['hashtag0']; } if(isset($_POST['hashtag1'])) { $hashtag1 = $_POST['hashtag1']; } if(isset($_POST['hashtag2'])) { $hashtag2 = $_POST['hashtag2']; } if(isset($_POST['hashtag3'])) { $hashtag3 = $_POST['hashtag3']; } if(isset($_POST['hashtag4'])) { $hashtag4 = $_POST['hashtag4']; } if(isset($_POST['hashtag5'])) { $hashtag5 = $_POST['hashtag5']; } if(isset($_POST['hashtag6'])) { $hashtag6 = $_POST['hashtag6']; } if(isset($_POST['hashtag7'])) { $hashtag7 = $_POST['hashtag7']; } if(isset($_POST['hashtag8'])) { $hashtag8 = $_POST['hashtag8']; } if(isset($_POST['hashtag9'])) { $hashtag9 = $_POST['hashtag9']; } if(isset($_POST['hashtag10'])) { $hashtag10 = $_POST['hashtag10']; } if(isset($_POST['hashtag11'])) { $hashtag11 = $_POST['hashtag11']; } if(isset($_POST['hashtag12'])) { $hashtag12 = $_POST['hashtag12']; } ?> <?php // Stores the hashtags taken from the form inside an array. $hashtags = array($hashtag0, $hashtag1, $hashtag2, $hashtag3, $hashtag4, $hashtag5, $hashtag6, $hashtag7, $hashtag8, $hashtag9, $hashtag10, $hashtag11, $hashtag12 ); ?> Basically, the script checks if the "$_POST['']" variable from the form is set and then it stores the "given" "values" from the form inside an array. I tried it with this: <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <?php for($i = 0; $i < 12; $i++){ echo "<input type='text' name='hashtag" . $i . "' />"; } ?> <input type="submit" name="submit" /> </form> <?php if(isset($_POST['submit'])){ for($i = 0; $i < 12; $i++) { if(isset($_POST['hashtag$i'])){ $hashtags = array($_POST['hashtag$i']); } } print_r($hashtags); } ?> With this example I get the following notice: Notice: Undefined variable: hashtags in C:\xampp\htdocs\hashtags\index.php on line 19 It points to "print_r()". I would appreciate the suggestions. Edited October 22, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/291983-how-to-set-this-up-with-a-for-loop/ Share on other sites More sharing options...
Solution ginerjm Posted October 22, 2014 Solution Share Posted October 22, 2014 PHP variables do not get interpreted when not wrapped in double quotes. So the post var you are checking in the loop is 'hashtag$i' which (if you had error checking turned on ) would give you an error message itself. If you use double quotes then you would check 'hashtag1', 'hashtag2' , etc. Quote Link to comment https://forums.phpfreaks.com/topic/291983-how-to-set-this-up-with-a-for-loop/#findComment-1494387 Share on other sites More sharing options...
glassfish Posted October 22, 2014 Author Share Posted October 22, 2014 Addendum: Okey I have tried it with this. <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <?php for($i = 0; $i < 12; $i++){ echo "<input type='text' name='hashtag" . $i . "' />"; } ?> <input type="submit" name="submit" /> </form> <?php if(isset($_POST['submit'])){ $hashtags = array(); for($i = 0; $i < 12; $i++) { if(!empty($_POST["hashtag$i"])){ $hashtags[] = $_POST["hashtag$i"]; } } print_r($hashtags); } ?> This works. Though, I have to specifically use "!empty", any reasons why? If I use "isset" I get this: Array ( [0] => test [1] => test [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => ) Thanks for the answer! Quote Link to comment https://forums.phpfreaks.com/topic/291983-how-to-set-this-up-with-a-for-loop/#findComment-1494390 Share on other sites More sharing options...
Jacques1 Posted October 22, 2014 Share Posted October 22, 2014 No, no, no. You do not use numbered variables or names at all. This is very, very poor design. You want a list instead. Unfortunately, PHP is rather quirky when it comes to processing multiple form parameters of the same kind. You need a special syntax for this: <input type="text" name="foo[]"> Note the brackets. This turns $_POST['foo'] into an array. Quote Link to comment https://forums.phpfreaks.com/topic/291983-how-to-set-this-up-with-a-for-loop/#findComment-1494391 Share on other sites More sharing options...
glassfish Posted October 22, 2014 Author Share Posted October 22, 2014 (edited) Jacques1, thanks for the suggestion, would I then have to use "keys" for the different values? $_POST['foo'][0], $_POST['foo'][1], $_POST['foo'][2] etc. EDIT: I tried it out, your suggestion is working great! Edited October 22, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/291983-how-to-set-this-up-with-a-for-loop/#findComment-1494393 Share on other sites More sharing options...
glassfish Posted October 22, 2014 Author Share Posted October 22, 2014 (edited) Addendum: <?php if(isset($_POST['submit'])){ if(isset($_POST['hashtag'])){ $hashtags = $_POST['hashtag']; } print_r($hashtags); } ?> Both, "isset" and "!empty" is printing the following: I would appreciate suggestions on this one: Array ( [0] => test [1] => test [2] => test [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => ) Note: With this I am using "hashtag[]" in the form. Edited October 22, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/291983-how-to-set-this-up-with-a-for-loop/#findComment-1494394 Share on other sites More sharing options...
glassfish Posted October 22, 2014 Author Share Posted October 22, 2014 Addendum: I have solved it like this: <?php if(isset($_POST['submit'])){ $hashtags = array(); for($i = 0; $i < count($_POST['hashtag']); $i++) { if (!empty($_POST['hashtag'][$i])) { $hashtags[] = $_POST['hashtag'][$i]; } } print_r($hashtags); } ?> Thanks for the answers. Quote Link to comment https://forums.phpfreaks.com/topic/291983-how-to-set-this-up-with-a-for-loop/#findComment-1494398 Share on other sites More sharing options...
Jacques1 Posted October 22, 2014 Share Posted October 22, 2014 PHP basics -- The foreach loop Or simply use array_filter(). Quote Link to comment https://forums.phpfreaks.com/topic/291983-how-to-set-this-up-with-a-for-loop/#findComment-1494400 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.