fishbaitfood Posted July 28, 2010 Share Posted July 28, 2010 Hello phpfreaks I'm a bit new to PHP, as well as this forum. I'm learning in a fast way, but there's something I can't seem to figure out, nor find it on google. I'm sure it's a simple and basic trick, but like I said, I'm just a beginner So the question is: How can I store multiple words to an array, using only 1 input form field? I guess it's with a for-loop..? So everytime you hit the 'submit' button, a new word should be added to the array. My problem is that I'm always overwriting ONE word.. This is what I've got so far.. : <?php if (isset($_POST['word'])){ $word = $_POST['word']; $word = strip_tags($word); $word = trim($word); $word = htmlentities($word); } else { $word = ""; } ?> <div id="arrays"> <p>Add a word to the array below:<br /><br /></p> <form action="index.php" method="post"> <input id="word" name="word" type="text" value="<?php echo $word; ?>" /> <br /><input class="btn" type="submit" value="Submit" /> </form> <br /> <p> <?php $array = array("apple", "banana", "lemon", $word); for ($i = 0; $i < count($array); $i++){ echo $array[$i] . " "; } ?> </p> </div> Also: I'm using PHP 5, in combination with xHTML strict 1.0. Quote Link to comment https://forums.phpfreaks.com/topic/209095-add-more-than-1-word-to-array-using-1-input-form-field/ Share on other sites More sharing options...
trq Posted July 28, 2010 Share Posted July 28, 2010 Every time you hit submit you make a new request. data does not persist over multiple requests. A few ways you can do it. The easiest being to use the $_SESSION array, this does persist. At the VERY top of your file place this.... <?php session_start(); ?> Then.... <?php // save the word from the form into a new spot within the session array $_SESSION[] = $word; // make the contents of the $_SESSION array into a string and echo it. echo implode(" ", $_SESSION); ?> Quote Link to comment https://forums.phpfreaks.com/topic/209095-add-more-than-1-word-to-array-using-1-input-form-field/#findComment-1092054 Share on other sites More sharing options...
Wolphie Posted July 28, 2010 Share Posted July 28, 2010 The reason why it isn't working is because every time the script is ran, the array gets reset. In order to store something so you can come back to it and use it later, you must use some form of storage. This could be sessions, cookies, text files, databases etc.. <?php session_start(); function sanitize($input) { if (!empty($input)) { $input = strip_tags($input); $input = htmlentities($input); return trim($input); } else { return FALSE; } } if (isset($_POST['submit'])) { $word = $_POST['word']; if (sanitize($word)) { $_SESSION['words'][] = $word; } else { echo 'Please enter a word'; } } ?> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <input type="text" name="word" /> <input type="submit" name="submit" value="submit" /> </form> Every time you enter a word and hit submit, the value will be appended to the $_SESSION['words'] array. **EDIT** thorpe got there before me. Quote Link to comment https://forums.phpfreaks.com/topic/209095-add-more-than-1-word-to-array-using-1-input-form-field/#findComment-1092055 Share on other sites More sharing options...
fishbaitfood Posted July 28, 2010 Author Share Posted July 28, 2010 Thanks both you guys! I can see that it now stores the words. But now I can't figure out how to display them? I tried: <?php for ($i=0; $i < count($_SESSION); $i++){ echo $_SESSION[$i]; } ?> but that's not working.. Quote Link to comment https://forums.phpfreaks.com/topic/209095-add-more-than-1-word-to-array-using-1-input-form-field/#findComment-1092068 Share on other sites More sharing options...
fishbaitfood Posted July 28, 2010 Author Share Posted July 28, 2010 If I do this, it only displays the last word I entered : for($i=0; $i < count($_SESSION); $i++){ echo $_SESSION['words'][$i]; } I used this with the code from Wolphie. UPDATE: I figured it out.. It should be: for($i=0; $i < count($_SESSION['words']); $i++){ echo $_SESSION['words'][$i]; } Quote Link to comment https://forums.phpfreaks.com/topic/209095-add-more-than-1-word-to-array-using-1-input-form-field/#findComment-1092137 Share on other sites More sharing options...
trq Posted July 28, 2010 Share Posted July 28, 2010 Or simply... echo implode(' ', $_SESSION['words']); Quote Link to comment https://forums.phpfreaks.com/topic/209095-add-more-than-1-word-to-array-using-1-input-form-field/#findComment-1092372 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.